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: '', dest: 'blog.html']
// is the same as
ant.get(src: '', dest: 'blog.html']

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