Groovy recipes for sweeter Ant builds

An incremental, 'under the radar' approach to Groovy adoption

If you want to incorporate Groovy into an existing Java application development process, then you'll need to convince your company's leadership that it's a good idea. One path to adoption is to demonstrate the benefits of using Groovy for an existing process such as nightly builds. Once your team has experienced Groovy's efficiency, they'll likely be more willing to integrate it into new application code. In this Java tip, Groovy developer Chris Bedford presents working recipes for integrating Groovy into your Ant-based Java build process.

Groovy is increasingly popular among Java developers seeking to build more functionality with less code. If you are among the recently hooked, you would probably like to use your new programming language for paid projects. But there are hurdles to adopting any new technology, and most decision makers will not want to take on the overhead unless they're sure it will pay off. Leveraging Groovy for your release engineering processes is one way to demonstrate its strengths without pushing too hard on the way things have always been done. Once your team has experienced the efficiencies of Groovy, they're more likely to support widespread adoption. In fact, they probably won't want to go back to building without it!

This article presents a series of practical build recipes that you can use to integrate Groovy into your release engineering processes. Altogether, they map out an incremental approach to Groovy adoption. First I'll introduce a Groovy build.xml script that invokes methods in Java classes, which is a must for writing tests in Groovy. After that we'll look at a few scripts for handling more complex build scenarios, including direct embeds of Groovy code and callouts to external Groovy scripts. Finally, I'll show you how to specify inter-task dependencies with Gant, which is a Groovy-based variant of Ant.

Setting up the Groovy integration project: Ant and Java

Ant is the "old reliable" of build technologies, and it's useful to know even if your shop uses Maven or something more exotic like Gradle. You can always drop into Ant to procedurally specify tasks that are tricky to do within your framework of choice. (For instance, Maven provides the maven-antrun-plugin for this purpose.)

The sample code for this article includes a series of test cases. In order to work with the code, you will need to have Java 1.5 or greater installed in your development environment. You'll also need a non-ancient version of Ant (at least version 1.6 or higher). Make sure that both your JAVA_HOME and ANT_HOME are set, and that ant and javac are in your classpath. These setup steps should be familiar to anyone who regularly uses Ant. (See the Resources section for an Ant tutorial with further setup instructions.)

Download and run the project code

To start out, download the article source code, unzip it, then cd to the basic.groovy.test.project directory. You'll see the following directory structure:

Figure 1. A test/src/groovy subdirectory containing SampleTest.groovy

The test class in Figure 1 invokes methods on the class under test, Sample.java, which is defined in the project's src/java subdirectory. Now, invoke the build script for the project (see Listing 3) via the command, ant clean test.

You should see output similar to Listing 1, which indicates that the test has run but is initially failing:

 

Listing 1. Test failure

[junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0.247 sec
    [junit] Test com.lackey.SampleTest FAILED
        ...
    [echo] JUnit Report: output.dir/test/reports/index.<WBR>html

1 2 3 4 5 Page 1