Quantcast
Viewing all articles
Browse latest Browse all 26

Apache Ant – Iteration within a specific number range

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>

 

Check it out.
Image may be NSFW.
Clik here to view.
ant-loop-example-2

 

Done =)

Reference:


Filed under: Ant Tagged: Ant, Javascript Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 26

Trending Articles