Gradle for Building Java Applications

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.”

Lost VMs or Containers? Too Many Consoles? Too Slow GUI? 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
This entry was posted in Software Development and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. Posted November 6, 2014 at 3:45 pm | Permalink

    very informative and helping… thank you.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  • NEED HELP?


    My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__ doublecloud.org.

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.