string 如何将字符串附加到 ant 中的属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9207139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I append a string to a property in ant?
提问by orshachar
I'm using ANT 1.7.0
我正在使用 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?
如何将字符串附加到 ant 中的属性?
回答by Cyril Sochor
You can't change value of property in Ant.
您无法更改 Ant 中的属性值。
You may use Ant Contrib variable task (see http://ant-contrib.sourceforge.net/tasks/tasks/variable_task.html) which provide mutable properties.
您可以使用提供可变属性的Ant Contrib 变量任务(参见http://ant-contrib.sourceforge.net/tasks/tasks/variable_task.html)。
<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>
回答by Rebse
Normally properties in ant are immutable once set.
With Ant addon Flakayou 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 :
通常,ant 中的属性一旦设置就不会改变。使用Ant 插件 Flaka,您可以更改或覆盖现有属性 - 甚至是用户属性(通过命令行 -Dkey=value 设置的那些属性),即创建一个宏定义并像这样使用它:
<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.
或者,您可以使用一些脚本语言(Groovy、Javascript、JRuby ..)并使用 ant api :project.setProperty(String name, String value)
来覆盖属性。
回答by JAveedMeandad
If suppose you want to append a string in existing property value follow below steps.
如果您想在现有属性值中附加一个字符串,请按照以下步骤操作。
- We need to load the property file which we need to change a value in it.
- Get an existing property value from the file in temp property by using ANT property task.
- Then do the normal process of changing the Property value.
- 我们需要加载我们需要更改其中的值的属性文件。
- 使用 ANT 属性任务从临时属性中的文件中获取现有属性值。
- 然后做改变Property 值的正常过程。
1
Property file 12
string to append3
ANT Script4
Final Property value
For Reference:Wordpress Link
供参考:Wordpress 链接