Search code examples
antant-contrib

Break or Continue Implemented?


Does the for/foreach loops in ant-contrib support the equivalent of a "break" or "continue" statement? As far as I can tell, they do not.

Are there any viable work-arounds?

Thanks -T


Solution

  • There is no easy way to implement this behavior, but maybe the following suggestion will help.

    • use the the for task (i.e. not the foreach)
    • set the keepgoing attribute to true
    • use the fail task with a condition so that the items that need to be skipped will fail.

    you can obtain something like a break by defining a property myBreakProperty whenever you detect that you need to break

     <for list="a,b,c,d,e" param="letter" keepgoing="true">
        <sequential>
           <if>
             <equals arg1="@{letter}" arg2="c" />
           <then>
             <property name="myBreakProperty" value="nevermind the value"/>
           </then>
         </if>
         <fail if="myBreakProperty"/>
         <echo>Letter @{letter}</echo>
        </sequential>
     </for>
    

    The output will be: Letter a Letter b


    To obtain something like a continue :

     <for list="a,b,c,d,e" param="letter" keepgoing="true">
        <sequential>
           <if>
             <equals arg1="@{letter}" arg2="c" />
           <then>
             <fail/>
           </then>
         </if>
         <echo>Letter @{letter}</echo>
        </sequential>
     </for>
    

    The output will be: Letter a Letter b Letter d Letter e