0

Hi all this is my code for target calling.

<target name="abc">
  <var name="x" value="10"/>
  <antcall target="def"/>
  <!--Again Access The value of x here and also change it here-->
</target>

<target name="def">
  <!--Access The value of x here and also change it here-->
</target>

and also i want to access this X in other build file,is there any way

skaffman
  • 398,947
  • 96
  • 818
  • 769
user1390517
  • 249
  • 2
  • 10
  • 23
  • What is `var`? I don't recognise any Ant task by that name. – skaffman May 15 '12 at 08:40
  • it is variable task in ant and is used for creating variables in ant – user1390517 May 15 '12 at 08:41
  • @skaffman it is from [ant-contrib](http://ant-contrib.sourceforge.net/tasks/tasks/variable_task.html) – oers May 15 '12 at 08:41
  • Just a friendly reminder: you can [accept answers that helped you](http://meta.stackexchange.com/a/5235/160062) on your previous questions. You haven't accepted a single answer. – oers May 15 '12 at 09:19

2 Answers2

2

This is not possible with ant. In an properties are immutable and cannot be reset. The var task from ant contrib can be used to override values, but should be used sparingly.

You could use a temporary file to achieve what you want. But probably you are trying something weird, which can be solved in a different way.
This would also work across buildfiles if they have access to the property file.

<target name="abc">
  <var name="x" value="10"/>
  <antcall target="def"/>
  <!--Again Access The value of x here and also change it here-->
  <var unset="true" file="myproperty.properties" /> <!-- read variable from property file-->
</target>


<target name="def">
  <echo file="myproperty.properties" append="false">x=12</echo> <!-- create a new propertyfile-->
</target>
oers
  • 18,436
  • 13
  • 66
  • 75
  • 1
    `echoproperties` and `loadproperties` ant task may be used to store and load file with temp prop values. `prefix` attribute of `loadproperties` sometimes helps to overcome issue of property immutability. – Vadzim May 15 '12 at 10:05
  • @Vadzim this prefix stuff sounds more hacky than my var approach :) but echoproperties is a good hint – oers May 15 '12 at 10:09
1

For the sake of justice, there is a hack that allows to alter ant's immutable properties without any additional libs (since java 6):

<scriptdef name="propertyreset" language="javascript"
    description="Allows to assing @{property} new value">
    <attribute name="name"/>
    <attribute name="value"/>
        project.setProperty(attributes.get("name"), attributes.get("value"));
</scriptdef>

Usage:

<target name="abc">
    <property name="x" value="10"/>
    <antcall target="def"/>
</target>

<target name="def">
    <propertyreset name="x" value="11"/>
</target>

As @oers mentioned, this should be used with care after all canonical approaches proved not to fit.

It is difficult to suggest further without knowing the goal behind the question.

Vadzim
  • 24,954
  • 11
  • 143
  • 151
  • will this "survive" the return from antcall, i.e. is x = 11 after the antcall? This is what op asked. – oers May 15 '12 at 10:11
  • This is just a complementary partial answer. I guess similar scripting approach can even be used to access parent project's props. The question is not very clean btw. – Vadzim May 15 '12 at 10:20