Java developers spend countless hours writing build scripts using Apache Ant. Ant is a venerable tool in the Java world, having originated as part of the Tomcat project and venturing out into the big bad world as a stand-alone project in the dim days of early 2003.
Ant is supported by all major IDEs (indeed, it is fair to say that the Netbeans IDE has probably taken the ant build script to new heights of complexity and flexibility) and provdes a platform that has made possible the creation of countless plugins.
Still, Ant has its limitations. Writing XML-based build scripts is a task that gets old…fast! In addition, the XML-based build language makes it hard to express simple things like iteration or conditional execution.
Enter gant. Gant is the groovy build tool that augments plain old Ant by supplying a clean and powerful groovy-based DSL.
A simple example (incrementing numbered ‘log’ files) is:
dirLogs = 'logs'
target ( increment : 'Renumber (increment) the files in the logs directory' ) {
def files = []
new File(dirLogs).eachFile() { files << it.name }
files.sort().reverse().each {
index = Integer.valueOf(it - 'log.')
source = "${dirLogs}/${it}".toString()
dest = "${dirLogs}/log.${index + 1}".toString()
new File(source).renameTo(new File(dest))
}
}
def printLogDir = {
new File(dirLogs).eachFile() { ant.echo " ${it.name}" }
}
target ( finish : 'finishing stuff' ) {
ant.echo '...done.'
printLogDir()
}
target ( start : 'startup stuff' ) {
ant.echo 'starting...'
printLogDir()
}
target ( bumpLogs : 'Log Rotation' ) {
start()
increment()
finish()
}
setDefaultTarget ( bumpLogs )
Note the total absence of the dreaded XML. Note also the degree of sophistication here: this script is actually using a fair bit of Groovy ‘goodness’: collections, closures, etc. this task would not be so easy in plain Ant.
Gant is becoming increasingly well supported in the various IDEs and also in tools such as hudson. In addition (and very much in keeping with the Groovy world-view) you don’t have to throw anything you already have away: all those nice external/third-party Ant tasks still work happily, all those ‘legacy’ Ant build scripts you have can keep on being used alongside the shiny new gant scripts you will be creating from now on…
If you are a Java developer, do yourself a favour: find out more about gant.
Personally, I don’t want to write another build.xml file ever again; I’ll be sticking with build.gant from now on!
