其他的答案是好的,但我需要一個沒有這些限制:
- 需要所有的屬性被指定爲以@標記@(第一個答案)
- 物業擴展模板 - 例如我有屬性定義爲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