guillec’s blog

Groovy + Ant = GANT

Posted in Gant, Groovy, Technology, Tutorial by guillec on December 3, 2008

gantVenkat Subramaniam has a theory about XML. He says:

Like humans, XML starts out cute when it’s small and gets annoying when it becomes larger.

If you have the same sentiment as Venkat, you will appreciate Gant. Gant is a powerful tool for scripting Ant task by writing Groovy code instead of XML. Below you will find samples from a Gant script and a explanation on what the code does. I wont go through the entire script but you can get it here.

Notice: The script calls a property file that you will need to create. The property file specifies locations for different directories. If you downloaded the script and you want to run it, make sure you change the file name to build.gant. To run the script, navigate to the location of the file and type gant.

In a nutshell, you write a Gant script by:

1. Declaring a target:

target ( ‘nameOfTarget’ : ‘ Description of Target ‘ ) {

}

2. Adding tasks inside the target:

targer ( ‘sampleTarget’ : ‘This is a sample target’) {

ant.doSomething(more code)

ant.doSomething(more code)

}

Lets start examining and explaining the script:

First two lines of the script:

Ant.property(file: ‘build.properties’)
def antProperty = Ant.project.properties

These are the first two lines of the script and they are simply saying, “Hey! I have a file called build.properties that I will be using during this process and I will refer to it with the word antProperty. So whenever you see the word antProperty, I want you to look inside the build.properties file for the value”. This is the file that you will need to create in order to build your project.

First target definition in the script:

target( ‘build’ : ‘This target build the project’) {
ant.echo(‘Hello I am starting the build process now please be patient’)
depends(resources)
depends(webcontent)
depends(compile)
depends(war)
}

This is the first target of many for the script. This section of code simply creates a target called build. The first task in this target is to print out a line. This is done with the echo command, ant.echo. Just like Ant, it simply prints out the statement declared inside the quotes. Finally we are saying, “Hey! This target depends on the completion of other targets called resources, webcontent, compile and war!”

First path definition in the script:

target( ‘resources’ : ‘Grabs resources such as the property files’) {
ant.echo(“I am grabbing all the resources”)
ant.mkdir(dir: antProperty.’dumpPropertiesHere’)
ant.copy(todir: antProperty.’dumpPropertiesHere’) {
path(refid: ‘properties’)
}
}
def properties = ant.path(id: ‘properties’) {
fileset(dir: antProperty.’getPropertiesFiles’) {
include(name: ‘*.properties’)
}
}

This piece of code has another target and a extra path definition. The resources target is going to copy some files into a specified location. Again, the first line simply prints the string inside the quotes.

The second line is simply creating a directory with the ant.mkdir task. The name for the directory is specified by calling the properties file.

The third line is going to copy files into the directory we created with the ant.mkdir task. The ant.copy(todir: antProperty.’dumpPropertiesHere’) is saying, “Yoooo machine, dump some stuff in here! You can find the files I need in a place called properties

Finally we define where this properties location is. We accomplish this by using the ant.path task. In the first line we give it an id. The fileset task is simply saying “The files that I want are in here and please include all files ending in .propeties“.

These tasks that I just described above cover about 70% of the code that you will find in my script. The following tasks will describe other important actions you will need to build your project.

Compiling your code:

ant.javac(srcdir: antProperty.’getSrc’, destdir: antProperty.’setClassesHere’) {
classpath() {
path(refid: ‘buildCP’)
}
}
}

The task ant.javac is the command you use to compile your Java code. In this task you are passing two parameters, the first parameter tells the system where to find the source code you want to compile, while the second parameter tells the application where to set the classes after they are compiled.

The classpath() task is basically just telling the system where you find the classes it will need in order to compile your code.

Creating a war file:

target ( ‘war’ : ‘Creates the .war’) {
ant.echo(‘Starting a war!’)
ant.jar(destfile: antProperty.’gant.war.file’, basedir: antProperty.’buildWarWith’, includes: ‘**’)
}

Here we call the ant.jar task in order to create the war file. The first parameter is the name of the .war file. The second parameter points the system to the location where it will find all the files that it needs. Finally, we tell the system to include all the files in that base directory.

setDefaultTarget(build)

Finally setDefaultTarget tells the system which target to start with.

As you probably noticed, one of the advantages of using Gant over basic Ant is the expressiveness you can achieve by using Groovy instead of XML. I originaly wrote this Gant script to begin my training in Groovy syntax, after a couple of minutes I found myself mostly copying and pasting the code since I had so many targets. So even thougth I think this is a good way of learning the basics of Groovy syntax the biggest benefit was being able to use this script on other projects and not having to look at XML. Please let me know know if you have any questions or comments about the script, or if you have any feedback on how I could make it better.

Advertisements
Tagged with: , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: