Home Fabio Cortesi Stefan Jäger

Welcome to the Joint-Venture Blog from Fabio Cortesi and Stefan Jäger.

 

 
 

 

There are some kind of tasks, which Ant won’t do. One of them is the loop. There is no simple way to implement a dynamic for or while loop in Ant. What do i mean with dynamic? Let me make an example. Ant reads out a number of iterations from a property file (we call it the number x) and should perform a loop x-times to call another Ant task. And this can only hardly be done with Ant.

 

 

image But fortunately, there is Groovy. Groovy is a scripting language, which runs in a Java Virtual Machine. And because of that, it can be easily integrated with Ant.

 

 

 

 

 

 

 

  1. Download the latest release of Groovy at http://groovy.codehaus.org/Download
  2. Inside the Groovy binary, there is a folder called embeddable. In this folder, there is a file called groovy-all-X.X.X.jar. Take it and copy it near your build file.
  3. Create a task definition in Ant
    1
    2
    3
    4
    
    <taskdef 
    name="groovy" 
    classpath="./groovy-all-X.X.X.jar" 
    classname="org.codehaus.groovy.ant.Groovy" />
  4. Use Groovy somewhere inside an Ant task:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
      <groovy>
       int i = 1
       int numberOfIterations = properties["numberOfIterations"].toInteger()
       while (i &lt;= numberOfIterations) {
        properties["currentIteration"] = i
        ant.antcall(target:"execute.a.task")
        i++
       }
      </groovy>

Isn’t that groovy?

 

Groovy integrated in Ant offers many possibilities, which can be read here http://groovy.codehaus.org/The+groovy+Ant+Task. I used for my little script the variable ant, an instance of the current Ant project and the properties map, which holds all properties inside Ant.

 

 

 

 

 
  1. Very cool!

    The next step of assimilation is using gant (http://gant.codehaus.org/) as a replacement for ant alltogether ;-)

    Cheers
    j

    Comment by Jonas — 14.5.2009 @ 07:55

Leave a comment