ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Alan Peng" <>
Subject RE: Looping in ANT
Date Mon, 12 Nov 2001 16:25:02 GMT
Hi Manik,

I have developed an Iterate task that allows you to iterate (loop). I've
submitted it to the project, but haven't got a reply yet...

Here is the email that I sent to the development group.

Ant does not support iteration or loop mechanisms that allows you to repeat
certain processes. Without such a mechanism, repeat processes have to be
hard coded, one entry in the build script per occurrence. This is less than
desirable because as it requires the modification of the build script each
time the project changes.

An iteration Java task has been developed which better supports the repeat
processes in the build.


Note: We had to use the standard Ant package names because the custom task
requires access to certain protected functions in classes in the Ant

This new task repeatedly calls a target passing different values from a
delimited string.

For example:
<project name="Ant_Extensions" default="main" basedir=".">
   <taskdef name="iterate" classname="">

   <target name="main">
     <iterate target="hello" itemList="1,3,5" id="iterate.value" >

   <target name="hello">
     <echo message="${iterate.value}" />

The above example will call the hello target three times, each time passing
a value from the item list. In this case the hello target will echo 1, then
3 and then 5.

A more useful example is the ability to compile multiple source directories
into multiple jar files.

For example:
 <target name="build" depends="init">

   <!-- iterate through the ${build.modules} variable, compiling each module
specified -->
   <iterate target="javac" itemList="myModule,myModule/mySubModule"

 <target name="javac" depends="checkSource, cleanBuild, prepareBuild"

   <javac  srcdir="${iterate.module}/src" destdir="${iterate.module}/build">
     <include name="**\/*.java"/>

   <!-- create a jar file for each module-->
   <mkdir dir="${iterate.module}/lib"/>

   <jar jarfile="${iterate.module}/lib/classes.jar">
     <fileset dir="${iterate.module}/build"/>

 The above example does the following:

.               Compiles the myModule/src directory into
.               Compiles the myModule/mySubModule/src directory into

I have included the source, classes and a example build script that calls
the new task.


-----Original Message-----
From: Manik Surtani []
Sent: 12 November 2001 15:04
Subject: Looping in ANT

Hello, all.

I have noticed a problem with ant, and that is performing loops.

If I want a set of tasks to repeat several times based on a set of
properties, the only way I can do this is using the <script> tags, and
perform the loops in javascript.

Also, it is not possible to use the <script> tags to envelope a set of
tasks to loop through, such as:

<script language="javascript">
for (i=0; i<10; i++)

<echo message="This is my task to be repeated" />

<script language="javascript">

Instead, I have to create and execute the task in Javascript, which
really takes away from the whole ANT XML build script thing.

My current J2EE build script runs almost everything in a loop, so this
means almost the entire build.xml is just Javascript!!

Is there an alternate way to do this?

Also, who is the maintainer of the <script> tasks?  I am keen to know if
such functionality (as described above) could be possible - I am
volunteering to help develop the <script> task, but I need to get more
famniliar with the task first.


Manik Surtani
Chief Technology Officer
Silk Road Limited

Telephone: 07786 702 706

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message