Home > Software Development > Gradle for Building Java Applications

Gradle for Building Java Applications

November 3rd, 2014 Leave a comment Go to comments

Building software is a very important part of software development cycle. In the C/C++ age, we used make and continue to use it today. When it came to Java age, two important build tools emerged: Ant and Maven. The former is very similar to make, but instead of script it uses XML. Both make and Ant describe HOW to build software in steps. The Maven takes a quite different approach – it describes WHAT the built software is using XML, and leaves the how to the Maven and its plugins.

Both Ant and Maven have their own pros and cons, and some developers either love or hate either of them. From my own experience, Ant is more flexible and has more control over customizing the steps; Maven is great in defining dependencies and easy to get started with normal project that follows Maven conventions. This stackoverflow discussion has summarized well in one line: “Maven is a Framework, Ant is a Toolbox.”

Time to learn how to "Google" and manage your VMware and clouds in a fast and secure

HTML5 App

To improve the build tooling, Gradle was invented. It’s built using Groovy programming language therefore no need to use XML. To tweak an existing complex build system, I spent a little time learning Gradle.

Here is a small build.gradle for my ScrapBook application, where I write all sort of small samples to try out various APIs, algorithms. It was originally a Netbeans project with which I just click “Run File” to execute any Java file with main() defined. Switching to Gradle does not make sense for this project but serves well the purpose of learning.

apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'application'
 
mainClassName = "scrapbook.ScrapBook"
 
group = 'org.doublecloud.example'
version = '1.0-SNAPSHOT'
 
description = 'a hello world example'
 
sourceCompatibility = 1.5
targetCompatibility = 1.5
 
sourceSets {
    main {
        java {
            srcDir 'src'
        }
        resources {
            srcDir 'src'
        }
    }
}
 
jar
{
    manifest {
        attributes "Main-Class": "scrapbook.ScrapBook"
   }
}
 
repositories {
    mavenCentral()
    maven { url "http://repository.springsource.com/maven/bundles/release" }
}
 
dependencies {
    compile 'com.vmware.vim:vijava-eam:5.5.2'
    compile 'com.google.guava:guava:17.0'
    testCompile group: 'junit', name: 'junit', version:'3.8.1'
}

Note that this is not the simplest build script, but has a little customization because my Netbeans project does not follow Maven file structure so I have to explicitly provide the source and resource location. Also, I want to build a self-executable jar so the manifest is needed to specify the main class per Java spec.

After the build.gradle is ready, I can run the following commands. All the tasks can be listed from the first command, and I can try any one of the command. These commands are not independent and some of them like build implicitly include other tasks.

$ gradle tasks
 
$ gradle clean
 
$ gradlew tasks --stacktrace
 
$ gradlew compileJava
 
$ gradlew build
 
$ gradlew assemble
 
$ gradlew jar
 
$ gradlew test
 
$ gradlew run
 
$ gradlew distZip
 
$ gradlew installApp
Categories: Software Development Tags: , ,
  1. November 6th, 2014 at 15:45 | #1

    very informative and helping… thank you.

  1. No trackbacks yet.