使用Ant合并两个不同的属性文件
我有一个默认属性文件,以及一些特定于部署的属性文件,它们根据部署环境覆盖默认设置中的某些设置。我希望我的Ant构建脚本合并两个属性文件(使用部署特定的值覆盖默认值),然后将结果属性输出到新文件使用Ant合并两个不同的属性文件,ant,build,merge,properties,Ant,Build,Merge,Properties,我有一个默认属性文件,以及一些特定于部署的属性文件,它们根据部署环境覆盖默认设置中的某些设置。我希望我的Ant构建脚本合并两个属性文件(使用部署特定的值覆盖默认值),然后将结果属性输出到新文件 <copy todir="${web-inf.path}/conf" file="${build.config.path}/template.application.properties" overwrite="true" failonerror="true" > <filter
<copy todir="${web-inf.path}/conf" file="${build.config.path}/template.application.properties" overwrite="true" failonerror="true" >
<filterset refid="application-properties-filterset" />
</copy>
我试着这样做,但没有成功:
<target depends="init" name="configure-target-environment">
<filterset id="application-properties-filterset">
<filtersfile file="${build.config.path}/${target.environment}/application.properties" />
</filterset>
<copy todir="${web-inf.path}/conf" file="${build.config.path}/application.properties" overwrite="true" failonerror="true" >
<filterset refid="application-properties-filterset" />
</copy>
</target>
<copy todir="${web-inf.path}/conf" file="${build.config.path}/template.application.properties" overwrite="true" failonerror="true" >
<filterset refid="application-properties-filterset" />
</copy>
也许你应该看看ant的任务。我想出了这个。需要创建一个额外的属性文件,每个键/值的格式如下:
<copy todir="${web-inf.path}/conf" file="${build.config.path}/template.application.properties" overwrite="true" failonerror="true" >
<filterset refid="application-properties-filterset" />
</copy>
mail.server.host=@mail.server.host@
等等
<copy todir="${web-inf.path}/conf" file="${build.config.path}/template.application.properties" overwrite="true" failonerror="true" >
<filterset refid="application-properties-filterset" />
</copy>
然后将此“模板”文件指定给任务的“文件”属性。同样在filterset中,指定多个,并首先列出最不重要的一个
<copy todir="${web-inf.path}/conf" file="${build.config.path}/template.application.properties" overwrite="true" failonerror="true" >
<filterset refid="application-properties-filterset" />
</copy>
所以看起来是这样的:
<copy todir="${web-inf.path}/conf" file="${build.config.path}/template.application.properties" overwrite="true" failonerror="true" >
<filterset refid="application-properties-filterset" />
</copy>
<property prefix="app.properties" file="custom.application.properties" />
<property prefix="app.properties" file="default.application.properties" />
<echoproperties destfile="application.properties">
<propertyset>
<propertyref prefix="app.properties"/>
<mapper type="glob" from="app.properties.*" to="*"/>
</propertyset>
</echoproperties>
<copy todir="${web-inf.path}/conf" file="${build.config.path}/template.application.properties" overwrite="true" failonerror="true" >
<filterset refid="application-properties-filterset" />
</copy>
我个人使用这个:
<copy todir="${web-inf.path}/conf" file="${build.config.path}/template.application.properties" overwrite="true" failonerror="true" >
<filterset refid="application-properties-filterset" />
</copy>
<copy todir="${web-inf.path}/conf" filtering="true">
<fileset dir="${build.config.path}" includes="*.properties" />
<filterset>
<filtersfile file="application-properties-filterset" />
</filterset>
</copy>
我是这样做的:
<copy todir="${web-inf.path}/conf" file="${build.config.path}/template.application.properties" overwrite="true" failonerror="true" >
<filterset refid="application-properties-filterset" />
</copy>
<property prefix="app.properties" file="custom.application.properties" />
<property prefix="app.properties" file="default.application.properties" />
<echoproperties destfile="application.properties">
<propertyset>
<propertyref prefix="app.properties"/>
<mapper type="glob" from="app.properties.*" to="*"/>
</propertyset>
</echoproperties>
其他答案都可以,但我需要一个没有这些限制的答案:
<copy todir="${web-inf.path}/conf" file="${build.config.path}/template.application.properties" overwrite="true" failonerror="true" >
<filterset refid="application-properties-filterset" />
</copy>
- 需要将所有属性指定为带有@tokens@(第一个答案)的模板
- 属性扩展-例如,我将属性定义为prop2=${prop1},它将被加载和回送属性的任何解决方案扩展
- EchoProperties(@user2500146)转义像冒号这样的字符,这对URL属性来说很烦人(不是Ant的错,这是标准Java属性,它允许:代替=)
- 基于concat的解决方案中的重复属性(这是可行的,因为忽略了第二个定义,但我不希望重复)
<copy todir="${web-inf.path}/conf" file="${build.config.path}/template.application.properties" overwrite="true" failonerror="true" >
<filterset refid="application-properties-filterset" />
</copy>
您可以使用这种逐字逐句的方式,但在确信之后,您可能希望删除日志语句或将其更改为调试级别
<copy todir="${web-inf.path}/conf" file="${build.config.path}/template.application.properties" overwrite="true" failonerror="true" >
<filterset refid="application-properties-filterset" />
</copy>
<!-- 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>
对我来说似乎是最好的答案,因为它应该适用于常规属性文件,而不是要求@tokens@However这似乎为各种属性的值添加了转义标记。例如:我有aa=D:\abcd。这已转换为aa=D\:\\abcd。有什么方法可以避免这种情况吗?发现ant concat任务工作得更好。为了更正确,我可以读取前缀属性以确保其相同,但不值得额外花费