Search code examples
ant-contrib

how to do for loop based on a limit in Ant


In Ant-contrib, we can do something like:

<for list="a,b,c,d" param="instence">

But if I don't have a list, I only have a limit, e.g. limit=4.

Is there a way to do for loop based on limit, like:

<for limit="4" param="index">

Solution

  • The Ant addon Flaka provides some solutions for your problem.

    1) Use a while loop

    the test attribute evaluates some EL expression,
    which maybe a simple counter, f.e.

    <project name="demo" xmlns:fl="antlib:it.haefelinger.flaka">
    
     <!-- some counter -->
     <fl:let>
      countdown = 3
     </fl:let>
    
     <fl:while test=" countdown >= 0 ">
      <fl:echo>
       #{countdown > 0 ? countdown : 'bang!' }..
      </fl:echo>
      <fl:let>
       countdown = countdown - 1
      </fl:let>
     </fl:while>
    
    </project>
    

    output:

      [fl:echo] 3..
      [fl:echo] 2..
      [fl:echo] 1..
      [fl:echo] bang!..
    

    or some other expression, f.e. checking for existence of a file, means looping until some file exists :

     <fl:while test=" !'/home/rosebud/stop'.tofile.isfile ">
      <fl:echo>
       still working..
      </fl:echo>
     </fl:while>
    

    2) Use a for loop with break or continue :

    <project name="demo" xmlns:fl="antlib:it.haefelinger.flaka">
    
     <fl:for var="i" in=" list(1,2,3,4,5,6) ">
        <fl:echo>i = #{i}</fl:echo>
        <fl:break test=" i == 3 " />
     </fl:for>
    
      <fl:for var="i" in=" list(1,2,3,4,5,6) ">
        <fl:continue test=" i > 3 " />
        <fl:echo>i = #{i}</fl:echo>
     </fl:for>
    
    </project>
    

    which gives the same result :

      [fl:echo] i = 1
      [fl:echo] i = 2
      [fl:echo] i = 3
    

    See more details in the Flaka manual