You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by broken connection <br...@gmail.com> on 2007/07/03 15:57:37 UTC

Help with iteration in for loop

Hi Friends,
Is there any way to control iteration in ant. I want to put an if-else
condition in the for loop to control the action depending upon the iteration
count.I tried this:

build.xml
<project name="test" default="test_iteration">
 <taskdef resource="net/sf/antcontrib/antlib.xml" />
  <property name="prop_file" value="value.properties"/>

 <target name="test_iteration">
 <property file="${prop_file}"/>

 <for list="1,2" param="argvalue" >
    <sequential>
       <!-- use property file to increment counter -->
       <propertyfile file="value.properties">
           <entry key="count" type="int" operation="+" value="1" />
       </propertyfile>

   <echo>counter: ${count}</echo>

    </sequential>
 </for>

 </target>
</project>

value.properties file:
count=0

After I execute this,I get this as output:

C:\Documents and Settings\Desktop\test>ant
Buildfile: build.xml

test_iteration:
[propertyfile] Updating property file: C:\Documents and
Settings\hjhutty\Desktop
\test\value.properties
     [echo] counter: 0
[propertyfile] Updating property file: C:\Documents and
Settings\hjhutty\Desktop
\test\value.properties
     [echo] counter: 0

BUILD SUCCESSFUL
Total time: 1 second

value.properties=2

Can somebody explain me,why ${count} still displays 0 in the second
iteration,though the properties file has been updated.

Please help.
Thanks

Re: Help with iteration in for loop

Posted by Bill Rich <bi...@wilandra.com>.
Properties are immutable. Run the build file with -verbose and you will 
probably see that count is not being updated because it is already set. 
Once set a property will not be set a different value.

That said, I think there is a way to use ant-contrib to do this. Look at 
the math property task of ant-contrib. There you will see examples of 
using a var to do the math. In your case use a var in the loop then 
assign the value to count as the last thing you do in the task.

HTH Bill

broken connection wrote:
> Hi Friends,
> Is there any way to control iteration in ant. I want to put an if-else
> condition in the for loop to control the action depending upon the 
> iteration
> count.I tried this:
>
> build.xml
> <project name="test" default="test_iteration">
> <taskdef resource="net/sf/antcontrib/antlib.xml" />
>  <property name="prop_file" value="value.properties"/>
>
> <target name="test_iteration">
> <property file="${prop_file}"/>
>
> <for list="1,2" param="argvalue" >
>    <sequential>
>       <!-- use property file to increment counter -->
>       <propertyfile file="value.properties">
>           <entry key="count" type="int" operation="+" value="1" />
>       </propertyfile>
>
>   <echo>counter: ${count}</echo>
>
>    </sequential>
> </for>
>
> </target>
> </project>
>
> value.properties file:
> count=0
>
> After I execute this,I get this as output:
>
> C:\Documents and Settings\Desktop\test>ant
> Buildfile: build.xml
>
> test_iteration:
> [propertyfile] Updating property file: C:\Documents and
> Settings\hjhutty\Desktop
> \test\value.properties
>     [echo] counter: 0
> [propertyfile] Updating property file: C:\Documents and
> Settings\hjhutty\Desktop
> \test\value.properties
>     [echo] counter: 0
>
> BUILD SUCCESSFUL
> Total time: 1 second
>
> value.properties=2
>
> Can somebody explain me,why ${count} still displays 0 in the second
> iteration,though the properties file has been updated.
>
> Please help.
> Thanks
>