Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

If ever you've been working with a build.xml file or some Jelly script and found yourself a little restricted by all those pointy brackets, or found it a bit wierd using XML as a scripting language and wanted something a little cleaner and more straight forward, then maybe Ant scripting with Groovy might be what you're after.

Groovy has a helper class called AntBuilder which makes the scripting of Ant tasks really easy; allowing a real scripting language to be used for programming constructs (variables, methods, loops, logical branching, classes etc). It still looks like a neat concise version of Ant's XML without all those pointy brackets; though you can mix and match this markup inside your script. Ant itself is a collection of jar files. By adding them to your classpath, you can easily use them within Groovy as is. We believe using AntBuilder leads to more concise and readily understood syntax.

Below are some examples (most taken from Groovy's own AntBuilder tests) which demonstrate:

  • the use of Ant inside Groovy using the AntBuilder DSL notation
  • a demo of iterating through an Ant FileSet using fileScanner
  • that normal variables can be used to pass state into the Ant tasks and that Groovy code can be embedded anywhere in the markup.
Code Block
def ant = new AntBuilder()

// lets just call one task

// here is an example of a block of Ant inside GroovyMarkup
ant.sequential {
    echo("inside sequential")
    myDir = "target/AntTest/"
    copy(todir:myDir) {
        fileset(dir:"src/test") {

// now lets do some normal Groovy again
file = new File("target/AntTest/groovy/util/AntTest.groovy")
assert file.exists()
Code Block
def ant = new AntBuilder()

// lets create a scanner of filesets
scanner = ant.fileScanner {
    fileset(dir:"src/test") {

// now lets iterate over
def found = false
for (f in scanner) {
    println("Found file $f")
    found = true
    assert f instanceof File
assert found
Code Block
def ant = new AntBuilder()

ant.junit {
Code Block
def ant = new AntBuilder()

value = ant.path {
    fileset(dir:"xdocs") {

assert value != null

println "Found path of type ${}"
println value
Code Block
def ant = new AntBuilder()
def taskContainer = ant.parallel(){ // "Parallel" serves as a sample TaskContainer
    ant.echo()                      // "Echo" without message to keep tests silent
// not very elegant, but the easiest way to get the ant internals...
assert taskContainer.dump() =~ /nestedTasks=\[\w+\]/

Compiling and running a Java file:

Code Block
def ant = new AntBuilder()
ant.echo(file:'', '''
class Temp { public static void main(String[] args) { System.out.println("Hello"); }}
ant.javac(srcdir:'.', includes:'', fork:'true')'.', classname:'Temp', fork:'true')
// =>
//    [javac] Compiling 1 source file
//     [java] Hello
//     [echo] Done

Sniffing around ...

Code Block
def ant = new AntBuilder()
SpoofTaskContainer.spoof.length = 0
def PATH = 'task.path'
['spoofcontainer':'SpoofTaskContainer', 'spoof':'SpoofTask'].each{ pair ->
    ant.taskdef(name:pair.key, classname:'groovy.util.'+pair.value, classpathref:PATH)
expectedSpoof =
    "SpoofTaskContainer ctor\n"+
    "SpoofTask ctor\n"+
    "in addTask\n"+
    "begin SpoofTaskContainer execute\n"+
    "begin SpoofTask execute\n"+
    "end SpoofTask execute\n"+
    "end SpoofTaskContainer execute\n"
assertEquals expectedSpoof, SpoofTaskContainer.spoof.toString()

Using the joint compiler

Here is a small build file which uses the joint compiler to compile Groovy and Java source files together, and put them in WEB-INF/classes:

Code Block
def ant = new AntBuilder().sequential {
	webinf = "deploy/WEB-INF"
	taskdef name: "groovyc", classname: "org.codehaus.groovy.ant.Groovyc"
	groovyc srcdir: "src", destdir: "${webinf}/classes", {
		classpath {
			fileset dir: "${webinf}/lib", {
		    	include name: "*.jar"
			pathelement path: "${webinf}/classes"
		javac source: "1.5", target: "1.5", debug: "on"