Previously i have published a post about using for loop in Ant to read a text file line by line.
The <for> task requires a list attribute containing a comma separated string. Here is an example.
<echo message="The first five letters of the alphabet are:"/> <for list="a,b,c,d,e" param="letter"> <sequential> <echo>Letter @{letter}</echo> </sequential> </for>
But how about if we want to iterate with a specific range? I did it by using Javascript in Ant.
<project name="AntLoopExample" default="run" basedir="."> <!-- Load the ant contrib lib --> <taskdef resource="net/sf/antcontrib/antlib.xml"> <classpath> <pathelement location="${basedir}/ant-lib/ant-contrib-1.0b3.jar"/> </classpath> </taskdef> <target name="run"> <property name="loop.start" value="4"/> <property name="loop.end" value="7"/> <script language="javascript"> <![CDATA[ var start = AntLoopExample.getProperty("loop.start"); var end = AntLoopExample.getProperty("loop.end"); var list = start; for (i = parseInt(start)+1; i <= end; i++) { // Construct the list list += "," + i.toString(); } AntLoopExample.setProperty("loop.list", list); ]]> </script> <!-- Iterate the list --> <for list="${loop.list}" param="letter"> <sequential> <echo>Letter @{letter}</echo> </sequential> </for> </target> </project>
Done =)
Reference:
Can use ant to run JavaScript! Amazing! 😀
LikeLike
You can even run ruby and groovy too.
But i haven’t tried others~ XD
Reference: Ant – Scriptdef
LikeLike