Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

string

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?

like image 948
orshachar Avatar asked Feb 09 '12 07:02

orshachar


People also ask

How do you add a text to a string?

You concatenate strings by using the + operator.

How do you set an ant property?

The <property> task is used to set the Ant properties. The property value is immutable, once the value is set you cannot change it. To set a property to a specific value you use Name/value assignment. To set a property to a location you use Name/location assignment.

How do I override property value in ant?

Ant Properties are set once and then can never be overridden. That's why setting any property on the command line via a -Dproperty=value will always override anything you've set in the file; the property is set and then nothing can override it. This way: Anything set at the command line takes precedence over build.


2 Answers

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>
like image 183
Cyril Sochor Avatar answered Oct 24 '22 04:10

Cyril Sochor


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.

like image 44
Rebse Avatar answered Oct 24 '22 05:10

Rebse