BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News An Alternative Build System: Gradle 1.0 Released

An Alternative Build System: Gradle 1.0 Released

This item in japanese

Gradleware has recently released Gradle 1.0, a build system targeted at Java, Scala and Groovy. Gradle attempts to consolidate all the good ideas from Make, Ant, Ivy, Maven, Rake, Gant, Scons, SBT, Leinengen, and Buildr and it is already used by high profile projects such as Spring and Hibernate. The capabilities it offers are similar to the popular solutions of Maven and Ant/Ivy already used by Java developers.

Some interesting features of Gradle are:

  • Build scripts are written in Groovy (instead of XML)
  • Support for builds in Java, Scala and Groovy itself.
  • Dependency management compatible with Maven repositories
  • Ant tasks are first class citizens
  • Build in support for static analysis tools (software quality)
  • GUI integration for all popular IDEs
  • The Gradle daemon for faster builds is no longer experimental
  • Programming API for embedding Gradle itself into other applications
  • Experimental C++ support

Both Ant and Maven use XML for their build configuration format. While XML is human readable and has great tool support, it cannot always represent control logic in a convenient way. Gradle overcomes this issue by adopting a full programming language (Groovy) as the build format. This brings the power of a programming language in the definition of the build. Flow conditionals, data variables and all other programming constructs can easily be incorporated into the build process.

An an example Gradle Tasks (think Ant targets) are programming constructs on their own. Here is a Gradle build file that shows this concept:

// Use the default greeting
task hello(type: GreetingTask)

// Customize the greeting
task greeting(type: GreetingTask) {
    greeting = 'greetings from GreetingTask'
}

class GreetingTask extends DefaultTask {
    def String greeting = 'hello from GreetingTask'

    @TaskAction
    def greet() {
        println greeting
    }
}

This will print:

$gradle -q hello greeting
hello from GreetingTask
greetings from GreetingTask

At first sight this feature makes Gradle a very powerful build tool with a lot of flexibility (similar to Ant). Gradle however attempts to find a sweet spot between the power of Ant and the rigidness of Maven. For the supported languages, Gradle includes respective plugins with predefined tasks for the most common operations. Activating the Java plugin in a build file will also define tasks such as clean, compile, javadoc, build, test, check etc.

Dependencies in Gradle are handled in a similar way to Maven. Gradle can use Maven repositories and before reaching version 1.0 Ivy was used for dependency management (which is itself compatible with Maven). For the 1.0 version Gradle implements its own mechanisms for dependency management. Here is an example which shows the similarities with Maven:

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
    testCompile group: 'junit', name: 'junit', version: '4.+'
}

Gradle is also compatible with existing Ant builds. Not only it can take advantage of existing Ant tasks, but it can also import a build.xml file. Gradle also offers plugins for integrating quality tools in the build process such as FindBugs, PMD, Checkstyle, JDepend, CodeNarc and even Sonar.

For performance reasons Gradle comes with a daemon that can accept build scripts while active. This improves the speed of the build since the overhead of starting (and stopping) Gradle disappears after the initial run. This technique is useful in cases where a large number of builds is executed one after the other, such as during unit testing.

Finally Gradle comes with a special wrapper that allows integrating it into the source code of project. This makes the build self contained since anyone can work with the project without an existing Gradle installation. This is actually one of the weaknesses of Maven, since a Maven project is built on environments where Maven is already installed.

For more information on Gradle see the documentation, release notes and the roadmap. If you find Maven too restrictive, Gradle may be a good alternative solution to your build system.

Rate this Article

Adoption
Style

BT