Methods & Tools Software Development Magazine

Software Development Magazine - Programming, Software Testing, Project Management, Jobs

Gradle

Evgeny Goldin, http://evgeny-goldin.com/

Gradle is a new and revolutionary build tool, based on the Groovy programming language. It is very different from existing tools like Ant and Maven in that it provides an extremely powerful capability to develop build applications using Groovy code and a compelling Groovy DSL. This allows to easily develop a non-standard build for any project, according to its needs, or to fall back to a more traditional convention-over-configuration approach also fully supported by the tool.

Web Site: http://gradle.org/

Version Tested: 1.0-milestone-3 on Windows 7 and Mac OS X 10.7.1, Java 1.6.0_26 x86/x64

License & Pricing: Open Source (Apache license), Free

Documentation:

Support:

Installation

Install Gradle by downloading the corresponding distribution from http://gradle.org/downloads.html: "bin" if you only need to run Gradle and "all" if you wish to download its sources, documentation, and examples as well. After unpacking the archive, point GRADLE_HOME environment variable to the folder where distribution was unpacked and add "%GRADLE_HOME%/bin" ("$GRADLE_HOME/bin") to your system PATH.

Gradle is a multi-platform tool. It runs on every operating system on which Java can be installed. Make sure you have JAVA_HOME environment variable defined properly and you can execute the "java -version" command.

After adding Gradle’s "bin" folder to the PATH environment variable, you can now run the "gradle -–version" command to make sure the setup process was successful:

Additional installation instructions can be found at http://gradle.org/installation.html.

Groovy DSL

Gradle build file is normally called "build.gradle" and, as mentioned earlier, it contains Groovy code. If you're not familiar with the Groovy programming language, both the documentation available online at http://groovy.codehaus.org/Documentation and Manning's "Groovy in Action, second edition" (http://www.manning.com/koenig2/) are excellent resources for you to get started. However, usually you won't find much requirement for coding the build logic in Groovy since Gradle provides a great deal of sensible convention-over-configuration defaults and a powerful DSL (Domain-Specific Language) for this purpose. Following conventions, you only have to specify absolutely necessary data. The Gradle DSL allows you to use simple configuration structures, covered at http://gradle.org/current/docs/dsl/index.html.

Even though Groovy code can be used anywhere within Gradle, DSL is the preferable way of specifying build behavior. By using the reusable code blocks provided by Gradle authors, the DSL makes the build processes less error-prone and much easier to comprehend.

Sample "build.gradle" with Groovy DSL file is provided below:

apply plugin: 'groovy'
apply plugin: 'maven'
group = 'com.test'
version = '0.1'
groovyVersion = '1.8.1'
springVersion = '3.0.6.RELEASE'
repositories { mavenCentral() }
dependencies {
	groovy "org.codehaus.groovy:groovy-all:$groovyVersion"
	compile "org.springframework:spring-context:$springVersion",
			'org.slf4j:slf4j-api:1.6.2'
	runtime 'org.slf4j:slf4j-log4j12:1.6.2'
	testCompile "org.springframework:spring-test:$springVersion",
			'org.spockframework:spock-core:0.5-groovy-1.8'
	testRuntime 'log4j:log4j:1.2.16'
}

This examples allows any basic Groovy project to be compiled, tested, packed and then installed to a local Maven repository by running "gradle build install" (Gradle–Maven integration is covered in the next section).

apply plugin: 'groovy'
apply plugin: 'maven'

loads two Gradle plugins: the plugin for compiling Groovy sources and the plugin for deploying project artifacts to a local or remote Maven repository.

groovyVersion = '1.8.1'
springVersion = '3.0.6.RELEASE'

specifies which Groovy and Spring versions, respectively, will be used. It helps to keep some of the third-party library versions outside of the actual code. As a result, it becomes much easier to update version numbers later on.

repositories {
mavenCentral() }

defines that the Maven Central repository (http://search.maven.org/) should be used for retrieving all external dependencies required, as specified by the

dependencies {
...
}

section. It uses scopes that may be familiar to you from Maven: "compile" for compile-time dependencies and "runtime" for runtime ones. Unlike Maven, Gradle introduces new scopes: "testCompile" and "testRuntime" to split compile and runtime test dependencies as well. In fact, Gradle allows build developers and plugin authors to define any number of dependencies scopes, as demonstrated in the example above where new "groovy" scope is defined by the Groovy plugin.

Gradle Model

The Gradle model is what makes Gradle so powerful and flexible. All projects and tasks executed at run-time can be accessed within Gradle code through their APIs. Unlike other build systems where tool run-time "internals" are normally hidden from the build developer, Gradle exposes the build developer to everything pertaining to the build structure, running tasks and their dependencies, property values, and conventions used. In addition, Gradle doesn't enforce any specific ways of managing a software project or creating build artifacts; its defaults are similar to those of Maven, but while Maven makes it hard or impossible to step aside from any of its predefined rules, then Gradle makes it an easy task. Being able to customize all aspects of the build behavior, and to use the Groovy programming language together with a powerful DSL covering most of developer needs, makes Gradle a true one-of-a-kind build tool.

Maven, Ivy, and Artifactory integration

Gradle can integrate with Maven, Ivy, and Artifactory by performing the following actions:

  • Retrieve third-party dependencies from remote Maven, Ivy, or Artifactory repositories.
  • Deploy build artifacts to local Maven repositories.
  • Deploy build artifacts to remote Maven, Ivy, or Artifactory repositories.
  • Convert Maven POM files to Gradle builds with the maven2gradle script.

Summary

The Java ecosystem has developed rapidly and aggressively over the last decade. However, traditional build tools were not fast enough to realize how project complexities can grow and make any of the previous assumptions about build processes outdated and largely irrelevant. Gradle comes to the rescue at a very right moment in time when build processes involve much more than simply creating a packaged artifact. Today, companies deal with demands of continuous delivery, continuous integration, and continuous testing which in turn, may require high-performing and sophisticated build tools, capable of adapting to any business and technological need.

If you wish to know more about Gradle, download my "10 Cool Facts about Gradle" presentation providing a more introductory material about Gradle, or visit a Wiki page at http://evgeny-goldin.com/wiki/Gradle, listing major Gradle features followed by links to relevant documentation and additional online resources.


More Java Continuous Integration Content


Click here to view the complete list of tools reviews

This article was originally published in the Fall 2011 issue of Methods & Tools