An Introduction to Gradle

Reading over the web about the Spring 3.2 new features, I discovered that Spring has been migrated their build scripts from Ant+Ivy to Gradle. All the build scripts that creates and manage the Spring modules were converted into Gradle. This decision put me to read about Gradle. And there is when I decided to add Gradle to my sample application Tnt Book Store, in order to do some “real things”.

Gradle took many ideas from Maven and Ant. But it is not XML based. It is a declarative DSL (Domain Specific Language) based on Groovy, for building and manage applications. It provides a plugin concept like Maven, so it makes common tasks pretty easy with very few lines of code. And if you need more complex builds, you can do it in Groovy, instead of XML. This is probably the main difference: use the Groovy programing language to make complex builds, which will be easier to maintain than XML.

Lets start first by building and running the Tnt Book Store application using Gradle:

  1. Download Gradle.
  2. Add the Gradle bin directory to your PATH environment variable.
  3. Make sure you have the JAVA_HOME environment variable set. Gradle needs it.
  4. Download the source code of the Tnt Book Store application, using svn:
    svn checkout http://tnt-book-store.googlecode.com/svn/trunk/ tnt-book-store-read-only
    
  5. Go to the root directory where the source code is located, the build.gradle file should be there.
  6. Then, just type: gradle -Dspring.profiles.active=dev jettyRunWar
  7. After finishing, gradle will shows you the URL where you can navigate and use the application.

Here we have compiled, retrieved dependencies from the maven public repository, created the war file and deployed it in Jetty (the application use the embedded derby database, and it gets populated with sample data at startup). Below is the build.gradle file that is required for doing this.

apply plugin: 'jetty'

sourceSets {
    main {
        java {
            srcDir 'src'
            srcDir 'db'
        }
        resources {
            srcDir 'resources'
        }
    }
	test {
        java {
            srcDir 'test'
        }
        resources {
            srcDir 'test-resources'
        }
    }
}

war {
	from 'WebContent'
}

repositories {
	mavenCentral()
}

dependencies {
	testCompile 'junit:junit:4.8.2'
	testCompile 'org.springframework:spring-test:3.1.0.RELEASE'

	compile 'joda-time:joda-time:2.0'
	compile ('org.springframework:spring-context:3.1.0.RELEASE') {
		exclude group: 'javax.ejb'
		exclude group: 'javax.jms'
		exclude group: 'com.bea.wlplatform'
	}
	compile 'org.springframework:spring-webmvc:3.1.0.RELEASE'
	compile ('org.springframework:spring-orm:3.1.0.RELEASE') {
		exclude group: 'com.oracle'
		exclude group: 'com.oracle.toplink'
		exclude group: 'org.apache.ibatis'
	}
	compile 'hsqldb:hsqldb:1.8.0.10'
	compile ('org.hibernate:hibernate-core:3.6.7.Final') {
		exclude group: 'antlr'
		exclude group: 'org.jboss.javaee'
	}
	compile 'org.hibernate:hibernate-validator:4.2.0.Final'
	compile 'org.apache.derby:derby:10.9.1.0'
	compile ('com.google.guava:guava:r09') {
		exclude group: 'com.google.code.findbugs'
	}
	compile 'commons-lang:commons-lang:2.6'
	compile 'commons-httpclient:commons-httpclient:3.1'

	compile 'org.codehaus.jackson:jackson-mapper-asl:1.4.2'
	compile 'org.codehaus.jackson:jackson-core-asl:1.4.2'
	compile 'org.mortbay.jetty:servlet-api-2.5:6.1H.14.1'
	compile 'javassist:javassist:3.4.GA'
	compile 'antlr:antlr:2.7.6'
}

The file is divided in four sections. The first section is the plugin definition. In this case, I’m using the jetty plugin. But since the jetty plugin inherit from the war plugin and the war plugin inherits from the java plugin, then I get everything just specifying one plugin.
Then, I have to specify the java source folders names. Gradle needs to know where to locate the source code within your project. This is only necessary because I’m not using the maven naming convention for java source folders. Then, the same for the source files from your web application, which in this case is WebContent. And finally, the declaration of the dependencies. That is pretty much all for building, bundling and deploying a java web application (and I did not write XML :)).

Posted in Dev&Ops Tagged with: ,