orshachar
orshachar

Reputation: 5037

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

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

Answers (3)

JAveedMeandad
JAveedMeandad

Reputation: 1

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

Upvotes: 0

Rebse
Rebse

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

Cyril Sochor
Cyril Sochor

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

Related Questions