How to add a string to a property in ant?

I am using ANT 1.7.0

I would like to create a goal that, when called, will add text to the string (stored in the property).

eg:

<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 cannot overwrite the property value in one target and read the changed value in another target.

How to add a string to a property in ant?

+6
source share
3 answers

You cannot change the value of a property in Ant.

You can use the Ant Contrib variable task (see http://ant-contrib.sourceforge.net/tasks/tasks/variable_task.html ) that 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> 
+11
source

Typically, the properties in ant are immutable after installation. With Ant addon Flaka you can change or overwrite existing properties - even custom properties (those properties that are set using the command line -Dkey = value), i.e. create a macro definition and use it as follows:

 <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> 

exit

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

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

+1
source

If you want to add a row to an existing property value, follow these steps.

  • We need to load the properties file, which we need to change in it.
  • Get the existing property value from a file in the temp parameter using the ANT property task.
  • Then follow the normal process of changing a property value.

1 Property file 1 2 line to add 3 ANT Script 4 Final value of the property

For reference: Wordpress Link

0
source

Source: https://habr.com/ru/post/908014/


All Articles