其他的答案是好的,但我需要一个没有这些限制:
- 需要所有的属性被指定为以@标记@(第一个答案)
- 物业扩展模板 - 例如我有属性定义为prop2 = $ {prop1}这将扩大任何解决方案加载和回声属性
- EchoProperties(@ user2500146)转义字符像冒号的URL属性烦人(不是Ant的错误,这是标准的Java属性,允许:在地方=)的基于CONCAT的解决方案
- 重复的属性(这工作,因为第二个定义被忽略,但我不想重复
最后,我不得不求助于在过滤器的JavaScript,但我的解决方案带来了默认属性,当且仅当它们没有在主要属性文件中定义。 它通过加载主属性wi然后将其复制到目标,然后连接默认属性,而则过滤掉在第一步中加载的任何默认属性。
您可以使用此逐字但可能要拿出日志报表或将其更改为调试级别,一旦你确信
<!-- merge the main.properties.file with the default.properties.file
into the output.properties.file (make sure these are defined) -->
<target name="merge">
<!--Obscure enough prefix to ensure the right props are handled-->
<property name="prefix" value="__MY_PREFIX__"/>
<!--Load the main properties so we can tell if the default is needed-->
<property prefix="${prefix}" file="${main.properties.file}"/>
<!--Copy the main properties, then append the defaults selectively-->
<copy file="${main.properties.file}" tofile="${output.properties.file}" overwrite="true"/>
<concat destfile="${output.properties.file}" append="true">
<fileset file="${default.properties.file}"/>
<filterchain>
<!--Filter out lines with properties that were already in the main properties -->
<scriptfilter language="javascript"> <![CDATA[
var line = self.getToken();
project.log("line: " + line);
var skipLine = false;
// lines that do not define properties are concatenated
if (line.indexOf("=") != -1) {
// get the property name from the line
var propName = line.substr(0, line.indexOf('='));
project.log("line prop: " + propName);
var loadedPropName = "__MY_PREFIX__" + propName;
if (project.getProperty(loadedPropName) != null) {
project.log("prop has original: " + project.getProperty(loadedPropName));
// skip this line, the property is defined
skipLine = true;
}
}
if (skipLine) {
project.log("skipping line: " + line);
self.setToken(null);
}
else {
// else leave the line in as it was
project.log("adding default line: " + line);
self.setToken(line);
}
]]> </scriptfilter>
</filterchain>
</concat>
</target>
标记您自己的答案是正确的,因为它完美的作品。 – 2012-04-23 15:15:04