|
Scripting a Groovy Ant
The Groovy scripting language makes scripting Ant even easier through builders
by Kevin Jones
Posted May 19, 2004
Editor's Note: This is Part 2 of a two-part article that discusses using Ant to build code and writing scripts with Ant objects treated as Java objects using the Groovy scripting language. Part 1, "Using Ant to Build Code" (Java Pro Online, May 12, 2004), looks at the variety of coding tasks to which you can apply the Ant toolbox. Here the article concludes with a discussion of applying Ant to scripting with the Groovy scripting language.
As mentioned in the concluding portion of Part 1, "Using Ant to Build Code" (Java Pro Online, May 12, 2004), initializing the Project instance with a logger lets you see the Ant output, and this type of approach can yield very good results. However, where the Ant Toolbox comes into its own is if you are using a scripting client.
Recently I needed to write a script to install a set of software for use of DevelopMentor's Java training courses. I had originally written a set of scripts to do this task. The original scripts were written as JavaScript files, and they relied heavily on the Windows Sripting object model to perform their work. I wanted to move away from this method for two reasons: I wanted to use Java as much as possible because I was writing setups for Java classes, and I wanted the scripts to be as platform agnostic as possible so that in the future the setups could be run on a Linux platform, whereas currently it was Windows only. After looking around at the various options such as Jython and JRuby I decided to use Groovy (see Resources), which is a scripting language written by James Strachan (amongst others) that has been put forward for standardization in a JSR (see Resources).
Using Groovy, you can use the Ant objects like any other Java objects, and my first attempt at using Ant from Groovy did exactly that. Writing the previous example (from Part 1) in Groovy longhand would look something like you see in Listing 1. The createProject() helper method has been omitted for clarity but is essentially the same as the corresponding Java method.
This code looks a lot like the Java equivalent, but there are several notable differences. While the import statements need to be there, notice that none of the types have to be defined. Notice also that semicolons and, in some cases, parenthesis are optional. And finally, note that properties can simply be assigned (there is no need to call setTodir for instance).
For my needs this solution was excellent. It provided a scripting language that I could more or less pick up and use instantly and a toolbox (Ant) that could be almost all of what I needed. Be aware that this solution could be achieved with any scripting language with direct support for creating Java objects, such as Jython.
There was one final trick, however. While the Listing 1 code exhibits correct Groovy code, it is not the best way to script Ant.
Groovy Builders
Groovy has support for what are termed builders. Builders allow developers to build anything that follows a markup style. For example, Groovy supports Swing builders, XML builders, HTML builders, and Ant builders. To use Ant the Groovy way, the code would look like this:
class AntSimple{
public static main(args){
ant = new AntBuilder();
ant.copy(todir: args[1]){
fileset(dir: args[1]) {
include(name: args[2])
}
}
}
}
And yes, that really is all there is to it.
Groovy supplies the AntBuilder class, and then it's a simple matter of telling that class what to do. Builders are clever pieces of code. In this case the AntBuilder class does not have a method called copy. Each builder has a createNode() method that dynamically creates that task. The task is then initialized, and finally its perform() method is called, which makes scripting Ant with Groovy compelling.
Ant provides many useful components that can be used in your applications. In particular, Ant can be scripted very easily. Groovy makes scripting Ant even easier through its concept of builders and in particular through its AntBuilder class.
About the Author
Kevin Jones is a developer who has spent the last four years researching and teaching Java programming and most recently investigating HTTP and XML. Kevin lives in the U.K. and works for Developmentor, a training company based in the United States and Europe that specializes in technical training on Java and Microsoft platforms. Contact Kevin at .
Back to top
|