Gradle and Java

Java Application with Gradle

Below is a simple Java application that we will use Gradle to build, I will mainly focus on only the Gradle parts.


We are going to use the jar plugin which gives us the standard project layout and the additional java tasks like clean, build, testClasses, jar, etc
plugins {
    id 'java'
}
We can create a number of properties that Gradle can use as part of the build
group = 'uk.co.datadisk'
version = '0.0.1-SNAPSHOT'

sourceCompatibility = 1.8             // use the JDK version 1.8
targetCompatibility = 1.8
Use repositories to access external repos to download external resources
repositories{
    mavenCentral()        // use deleate.mavenCentral() method to use a repository handler to access Maven Central
}
Use dependencies to retrieve needed external Java packages
dependencies {
    implementation 'org.apache.commons:commons-math3:3.2'      // use normal Maven coordinates
    testImplementation	'junit:junit:4.11'                     
}

Note:
    implementation: configuration should be used to declare dependencies which are internal to the component
    compileOnly: you should declare dependencies which are only required at compile time, but not at runtime
    runtimeOnly: you should declare dependencies which are only required at runtime, and not at compile time
    
    testImplementation: this is where you should declare dependencies which are used to compile tests
    testCompileOnly: you should declare dependencies which are only required at test compile time, but not test runtime
    testRuntimeOnly: you should declare dependencies which are only required at test runtime, and not at test compile time
    
    api: you should declare dependencies which are transitively exported to consumers, for compile
    
Use the jar task to create a jar file, we have a number of options like manifest, basename and many more
jar {
    manifest {
        attributes 'Implementation-Title': 'Gradle Jar File Example',
                'Implementation-Version': version,
                'Created-By' : "datadisk",
                'Main-Class': 'uk.co.datdisk.App'
    }
    baseName = "$project.name-all"
    from {project.configurations.runtimeClasspath.collect {File file -> project.zipTree(file) } }
}

When you run build gradle compiles and creates jar files in the build directory, this is similar to Maven target directory


One nice feature is that Gradle creates reports (out of the box), if you go to the build/reports you will see a index.html file which links to a number of html files regarding the build.


When you run the build phase a number of tasks are actually executed, you can see below that a total of 11 tasks ran. Gradle is also clever enough to know if parts of the project dont need rebuilding, you can see in the right-hand screenshot parts of the project are already UP-TO-DATE and thus nothing is done and thus time is saved.

Web Application Example

Again I am just going to cover an example web application Gradle build file and will only cover what I have not covered above already.


Use the plugins to add java and war tasks
plugins {
    id 'java'
    id 'war'
}
Create some extention proerties that will be used later (might be better in gradle.properties file)
ext.tomcatHome = 'C:/software/Apache Software Foundation/apache-tomcat-9.0.1'
ext.tomcatWebapps = "$tomcatHome/webapps"
Use war task to create a war file using name specified
war { archiveName = 'mywebapp.war'}
create own task called deployToTomcat, use the copy class
task deployToTomcat(type: Copy, dependsOn: 'war'){         // we depend on the war file
    from war
    into "$tomcatWebapps"
}
some dependencies we can exclude and others we only need durin compile time.
dependencies {
    implementation ('org.springframework:spring-webmvc:4.0.3.RELEASE') {
        exclude group: 'commons-logging', module: 'commons-logging'         // we can exclude parts
    }
    implementation 'org.slf4j:jcl-over-slf4j:1.7.25'
    implementation 'org.slf4j:slf4j-log4j12:1.7.25'
    implementation 'javax.inject:javax.inject:1'
    implementation 'org.apache.commons:commons-math3:3.2'
    implementation 'jstl:jstl:1.2'
    compileOnly "javax.servlet:javax.servlet-api:3.0.1"
    testImplementation	'junit:junit:4.11'
}