ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ben Stringer <...@burbong.com>
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>

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

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

</project>


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

increment:
     [echo] N = 3  BN=0

remove.statefile:

check.condition:

increment:
     [echo] N = 3  BN=1

check.condition:

increment:
     [echo] N = 3  BN=2

check.condition:

increment:
     [echo] N = 3  BN=3

check.condition:

BUILD SUCCESSFUL
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: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message