Use Groovy inside Ant

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.

Groovy 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
  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
    <taskdef name="groovy"
    classname="org.codehaus.groovy.ant.Groovy" />
  4. Use Groovy somewhere inside an Ant task:
    int i = 1
    int numberOfIterations = properties["numberOfIterations"].toInteger()
    while (i &lt; = numberOfIterations) {
    properties["currentIteration"] = i

Isn’t that groovy?

Groovy integrated in Ant offers many possibilities, which can be read here 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.

About the author



Recent Posts