9

I'm using ANT 1.7.0

I'd like to create a target that on call, will append text to a string (saved in property).

for example:

<property name="str.text" value="" />

<target name="append.to.property" >
  <property name="temp.text" value="${str.text}${new.text}" />
  <property name="str.text" value="${temp.text}" />
</target>

The problem is that I can't overwrite the property value in one target and read the changed value in another target.

How do I append a string to a property in ant?

oers
  • 18,436
  • 13
  • 66
  • 75
orshachar
  • 4,837
  • 14
  • 45
  • 68
  • 1
    possible duplicate of [How to over-write the property in ant?](http://stackoverflow.com/questions/1866729/how-to-over-write-the-property-in-ant) – skaffman Feb 09 '12 at 07:51

3 Answers3

15

You can't change value of property in Ant.

You may use Ant Contrib variable task (see http://ant-contrib.sourceforge.net/tasks/tasks/variable_task.html) which provide mutable properties.

<property name="str.text" value="A" />
<property name="new.text" value="B"/>

<target name="append.to.property" >
  <var name="temp.text" value="${str.text}${new.text}" />
  <var name="str.text" value="${temp.text}" />
</target>

<target name="some.target" depends="append.to.property">
  <echo message=${str.text}/>
</target>
Cyril Sochor
  • 692
  • 6
  • 9
1

Normally properties in ant are immutable once set. With Ant addon Flaka you may change or overwrite exisiting properties - even userproperties (those properties set via commandline -Dkey=value), i.e. create a macrodef and use it like that :

<project name="demo" xmlns:fl="antlib:it.haefelinger.flaka">

 <property name="foo" value="bar"/>

 <macrodef name="createproperty">
    <attribute name="outproperty"/>
    <attribute name="input"/>
    <sequential>
     <fl:let> @{outproperty} ::= '@{input}'</fl:let>
    </sequential>
 </macrodef>

 <!-- create new property -->
 <createproperty input="${foo}bar" outproperty="fooo"/>

    <echo>$${fooo} => ${fooo}</echo>

    <echo>1. $${foo} => ${foo}</echo> 

 <!-- overwrite existing property -->
 <createproperty input="foo${foo}" outproperty="foo"/>

    <echo>2. $${foo} => ${foo}</echo>

</project>

output

 [echo] ${fooo} => barbar
 [echo] 1. ${foo} => bar
 [echo] 2. ${foo} => foobar

alternatively you may use some scripting language (Groovy, Javascript, JRuby..) and use ant api :
project.setProperty(String name, String value) to overwrite a property.

Rebse
  • 10,307
  • 2
  • 38
  • 66
0

If suppose you want to append a string in existing property value follow below steps.

  1. We need to load the property file which we need to change a value in it.
  2. Get an existing property value from the file in temp property by using ANT property task.
  3. Then do the normal process of changing the Property value.

1Property file 1 2string to append 3ANT Script 4Final Property value

For Reference:Wordpress Link