Friday, 20 June 2008

Refactoring GANT

Its quite easy to build complicated GANT scripts - and not much has been written about how to refactor them.

Its not so obvious that you can call other targets directly, and with a bit of care you can re-use some of the built in grails targets like testApp.

The first step is to include anohter script like this:

includeTargets << new File("${basedir}/scripts/OtherScript.groovy")

Note: this does have implications in that you can overwrite variables and targets by including (and it does this silently without warning).

You can then reference another target from your target like this:

target(runMigration: "Run the Migration Tests") {
testMigration()
testApp()
}

There are some caveats however - one being that the useful "testApp" target (provided by grails) has a built in System.exit which causes your entire script to fail. You can get around this by hooking the exit event and storing the error code instead of exiting like this:

target('exit':"override exit") { code ->
// Save the exit code.
testAppExitCode = code
}


While all this does help - there is still a slight "whiff" to gant programming, however some of these tricks can help keep your gant scripts tidier.

Tim

No comments: