Reputation: 5037
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?
Upvotes: 9
Views: 26477
Reputation: 1
If suppose you want to append a string in existing property value follow below steps.
1
Property file 1
2
string to append
3
ANT Script
4
Final Property value
For Reference:Wordpress Link
Upvotes: 0
Reputation: 10377
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.
Upvotes: 1
Reputation: 702
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>
Upvotes: 16