我在位于 jar 中的属性文件中有一个设置,我希望在构建时使用 ant 更改该设置。理想情况下,如果我能够在属性文件中搜索特定文本并轻松替换它,我想这样做,但不确定如何。
所以我想我可以用另一个已经预定义新设置的属性文件覆盖它。 jar 已经存在于我的目录中,我的 jar 的层次结构如下:
food.jar
/com/food/donut.properties
some file...
some file...
如果我有另一个位于不同目录中的不同设置的 donut.properties 文件。我怎样才能用 Ant 覆盖它?
感谢您的帮助,非常感谢!
编辑:
使用以下代码,我能够将属性文件复制到 jar 中。但是每当我尝试将新属性文件复制到旧属性文件的同一目录中时,它都不会被替换。 (即,如果我将前缀更改为“com”,我可以看到新的属性文件被插入到 jar 中。如果将前缀更改为 com/food,则没有任何内容被替换。我做错了什么?
<jar destfile="${dist.dir}/food.jar" update="true"> <zipfileset file="donut.xml" prefix="com/food/" /> </jar>
最佳答案
需要 Ant 1.8.x
第1步)
编辑您的属性文件,多个嵌套条目元素可能:
<propertyfile file="/path/to/propertyfile/foo.properties">
<!-- will change an existing key named 'somekey' with the value 'foo' inplace -->
<entry key="somekey" value="foo"/>
</propertyfile>
见 Ant Manual propertyfile
第2步)
使用更改后的属性文件更新您的 jar:
<jar destfile="/path/to/your/foo.jar" update="true">
<fileset dir="/path/to/propertyfile" includes="*.properties"/>
</jar>
重命名使用这样的嵌套映射器:
<jar destfile="/path/to/your/foo.jar" update="true">
<mappedresources>
<fileset dir="." includes="*.properties"/>
<globmapper from="*.properties" to="/com/xml/*.properties"/>
</mappedresources>
</jar
关于ant - 使用 Ant 更改 jar 中的属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7746338/