ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ben Stringer <>
Subject Re: Implementing a loop in ANT
Date Thu, 08 Jun 2006 08:19:25 GMT
On Wed, 2006-06-07 at 11:17 -0700, Guru Balse wrote:
> I am sure this question has been asked before, and I could not see any 
> reasonable answer in the archives.  How can I implement a loop in ANT 
> without using scripts?  For example, if I want to call a certain target 
> N times, how can I do it? 
> Using ant-contrib is OK.  Of course, something like <for 
> list="1,2,3,4,5,6,...,N"> would work but I want N to be a property that 
> can be specified in the command line.
> Thanks in advance for your suggestions.

Here is a rather contrived solution using <buildnumber/> for state and
<ant> for recursion. Not sure how much use it will be in real world use,
but it was fun to write :)

<project default="increment" name="Increment Test"  basedir=".">

  <target name="check.condition" unless="increment.stop">
    <ant antfile="build.xml" target="increment" inheritAll="no"/>

  <target name="remove.statefile" if="increment.stop">
    <delete file="build.number"/>

  <target name="increment">
    <echo>N = ${N}  BN=${build.number}</echo>
    <condition property="increment.stop">
      <equals arg1="${N}" arg2="${build.number}"/>
    <antcall target="remove.statefile"/>
    <antcall target="check.condition"/>


ant -f increment.xml -DN=3
Buildfile: increment.xml

     [echo] N = 3  BN=0



     [echo] N = 3  BN=1


     [echo] N = 3  BN=2


     [echo] N = 3  BN=3


Total time: 1 second

There is a bug - the removal of the "build.number" file only works in
cases where N=0, and I have not yet worked out why. So remove
build.number manually each time if you are playing with this buildfile.

Cheers, Ben

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

View raw message