Nick Carroll

Metabolising caffeine into code

Archive for the ‘gant’ tag

You are a tool if you aren’t using Gant

without comments

If you are still writing your build scripts in XML then you my friend are a tool (the slang associated with the word “tool” is also implied :p). You are doing far too much heavy lifting and wasting everybody’s time and money by maintaining your build scripts in XML.

My recommendation is to use a build tool like Gant, which is a groovy based build tool that wraps Ant tasks and more. All you need is the standalone version of Gant, which includes all dependencies (eg groovy, ivy, maven, etc). You’ll need to use at least version 1.4, as there is a bug with 1.3 that prevents you from using the Ivy tool set.

The following is a simple Gant script that shows you how to manage your project’s dependencies using Gant’s Ivy integration. The script creates a lib directory and two sub-directories. The sub-directories will contain Jars for the application and for testing respectively. The script depends on having internet access, as the Jars will be downloaded from a public repositiory (ibiblio) and stored in your local repository. Your local repository acts as a cache, so you don’t have to keep downloading the dependencies every time you run your build script in a continuous integration system like Cruise.

libDirectory = 'lib'

includeTargets << gant.targets.Clean
cleanDirectory << libDirectory
cleanPattern << [ '**/*~' , '**/*.bak' ]

includeTool << gant.tools.Ivy

target ( retrieveDependencies : 'Retrieves library dependencies for main and test.' ) {
    depends ( clean )
    mkdir ( dir : libDirectory )
    ivy.retrieve ( organisation : 'commons-lang' , module : 'commons-lang' , revision : '2.3' , inline : 'true', pattern : libDirectory + '/main/[artifact]-[revision].[ext]' )
    ivy.retrieve ( organisation : 'junit', module : 'junit', revision : '4.5', inline : 'true', pattern : libDirectory + '/test/[artifact]-[revision].[ext]')
}

setDefaultTarget ( retrieveDependencies )

Execute the build.gant script using Gant and you will observe the Jars being downloaded and copied to the main and test directories. If you run the script again the Jars will be copied to the lib directory from your local Ivy repository (on Ubuntu this is .ivy2). The end result is the following directory structure.

build.gant
lib
    main
        commons-lang-2.3.jar
    test
        junit-4.5.jar

The equivalent script in XML will probably take ten times more effort, especially if you have to maintain your Ivy files in XML as well! Your build scripts in Gant look less cluttered and more readable than your typical XML based Ant scripts, which will make maintaining your build scripts far less painful. Plus you have the power of Groovy at your disposal for handling more complicated logic. So don't be a tool when it comes to builds. Use Gant for your build tool.

Written by Nick

December 9th, 2008 at 12:23 pm

Posted in Programming

Tagged with , ,