tags:

views:

605

answers:

5

Hi ,

Is there a way to re-assign the value for the ant property task? Or is there anyother task available for the same?

Regards, Priya.R

+3  A: 

ant-contrib's Variable task can do this:

<property name="x" value="6"/>
<echo>${x}</echo>   <!-- will print 6 -->
<var name="x" unset="true"/>
<property name="x" value="12"/>
<echo>${x}</echo>   <!-- will print 12 -->

Not recommended, though, it can lead to weird side-effects if parts of your Ant scripts assume immutable property values, and other parts break this assumption.

skaffman
Thanks for the explanation
Priya
The var task is especially nice for "local variables", e.g. in for loops (also a task from the excellent ant-contrib). One drawback, though, is that the var task doesn't support the "location" attribute.
akr
+1  A: 

Properties are immutable in ant.

You may be interested in the var task.

<var name="my_var" value="${my_property}" />

<echo>Addressed in the same way: ${my_var} and ${my_property}</echo>
jamesh
+1  A: 

Properties are immutable in ant. But that's not as terrible a limitation as it may seem. There's a whole class of programming languages where (most) variables are constant and yet they get stuff done   this is called "functional programming."

You can "change" values used by different tasks by deriving new, changed properties from old ones, or changing parameters when calling tasks with the subant or antcall tasks. If you're creative you can usually find a way to solve your problem.

Carl Smotricz
+1  A: 

You can't change the value of a property in ant.

If you have some ant tasks you want to run repeatedly passing in different values I recommend the macrodef task as you can run the same macro repeatedly passing in different attributes.

For example:

<macrodef name="copythings">
  <attribute name="todir"/>
  <sequential>
    <copy todir="@{todir}">
      <fileset dir="${src}">
        <exclude name='**/*svn' />
      </fileset>
    </copy>
  </sequential>
</macrodef>

<copythings todir="/path/to/target1"/>
<copythings todir="/path/to/target2"/>

Note that ${property} is used to reference properties and @{attribute} is used to reference the attributes passed to the <macrodef> task.

Dave Webb
A: 

Depending on how you want to use the modified property, you can use macrodefs.

For example, instead of writing the following:

<target name="foo">
   <echo message="${my_property}"/>
</target>

and not being able to call ant foo with another message, you could write:

<macrodef name="myecho">
    <attribute name="msg"/>
    <sequential>
        <echo message="@{msg}"/>
    </sequential>
</macrodef>

<target name="foo">
   <myecho msg="${my_property}"/>
   <property name="my_property2" value="..."/>
   <myecho msg="${my_property2}"/>
</target>
Vladimir