Search

Dark theme | Light theme

October 13, 2009

Groovy Goodness: Using AntBuilder

Groovy 1.6.5 includes Ant 1.7.1 and we can use Ant from our Groovy code with the AntBuilder class. The AntBuilder provides a nice DSL to create Ant tasks and to execute them. The following piece of code shows some samples of what we can do with the AntBuilder:

// Create AntBuilder.
def ant = new AntBuilder()

// Simple echo task.
def world = 'Groovy'
ant.echo("Hello $world")  // Output: [echo] Hello Groovy

// Ant task properties are defined as a map.
def options = [src: 'http://mrhaki.blogspot.com', dest: 'blog.html']
ant.get(options)
// is the same as
ant.get(src: 'http://mrhaki.blogspot.com', dest: 'blog.html']

// Nice builder syntax for tasks with for example filesets.
def zipfile = 'test.zip'
def current = '.'
ant.zip(destfile: zipfile) {
    fileset(dir: current) {
        include(name: '**/*.txt')
    }
}