简体   繁体   English

从自定义ant任务覆盖ant属性

[英]Overwriting ant property from custom ant task

So the overall problem is this: 因此,总体问题是这样的:

We have multiple property files 我们有多个属性文件

<property file="prop1"/>
<property file="prop2"/>

prop1 contains a property looking like: prop1包含一个类似于以下内容的属性:

mg.prop = ${mg2.prop}

prop2 contains mg2.prop prop2包含mg2.prop

mg2.prop = Hello

If they were in the same file and I queried mg.prop, I'd get "Hello" back. 如果它们在同一个文件中,并且我查询了mg.prop,我将返回“ Hello”。 Since they are in separate files this does not work (I need to load prop1 before prop2!) 由于它们位于单独的文件中,因此不起作用(我需要在prop2之前加载prop1!)

I wrote a custom ant task that does the following: 我编写了一个自定义ant任务,该任务执行以下操作:

String resolved = resolveProperty(propertyName);
getProject().setProperty(propertyName, resolved);

If I run 如果我跑步

log("Resolved property value = " + getProject().getProperty(propertyName)); 

Right after, I get the correct value. 紧接着,我得到正确的值。

However in the Ant script, if I do 但是在Ant脚本中,如果我这样做

<echo message="${mg.prop}"/> 

it shows me the original value. 它显示了我的原始价值。

Any thoughts on how to solve this? 关于如何解决这个问题有什么想法吗?

From the Ant manual: 从Ant手册中:

"Properties are immutable: whoever sets a property first freezes it for the rest of the build; they are most definitely not variables." “属性是不可变的:谁首先设置属性,在其余的构建过程中都会冻结该属性;它们绝对不是变量。”

http://ant.apache.org/manual/Tasks/property.html http://ant.apache.org/manual/Tasks/property.html

Depending on your situation, you might be able to accomplish what you want by loading prop1 twice , using loadproperties and a filter chain that the first time takes only lines not containing "{mg2.prop}", and the second time takes only lines that do contain it. 根据您的情况,您可能能够完成你想要通过加载PROP1 两倍 ,使用loadproperties并在第一时间只需要包含“{} mg2.prop”,第二个时间线过滤器链只需要该行包含它。

http://ant.apache.org/manual/Tasks/loadproperties.html http://ant.apache.org/manual/Types/filterchain.html#linecontains http://ant.apache.org/manual/Tasks/loadproperties.html http://ant.apache.org/manual/Types/filterchain.html#linecontains

Here's how I ended up resolving this - I turfed the custom ant task. 这是我最终解决此问题的方法-我把自定义ant任务弄乱了。

I ended up concatenating all the properties files into one, in the reverse order of precedence. 我最终以相反的优先顺序将所有属性文件串联在一起。

So if I wanted properties from 3.properties to override those in 2.properties and 1.properties, I did the following: 因此,如果我希望3.properties中的属性覆盖2.properties和1.properties中的属性,则可以执行以下操作:

<concat destfile="resolved.properties">
    <fileset file="1.properties" />
    <fileset file="2.properties" />
    <fileset file="3.properties" />
</concat>

<property file="resolved.properties"/>

You can also use the var task of ant-contrib to reset values. 您还可以使用ant-contribvar任务来重置值。

From the doc: 从文档中:

The next example shows a property being set, echoed, unset, then reset: 下一个示例显示了被设置,回显,未设置然后重置的属性:

 <property name="x" value="6"/> <echo>${x}</echo> <!-- will print 6 --> <var name="x" unset="true"/> <property name="x" value="12"/> <echo>${x}</echo> <!-- will print 12 --> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM