使用 Ant 更改 jar 内的属性文件
我在 jar 内的属性文件中有一个设置,我希望在构建时使用 ant 更改它。理想情况下,如果我能够在属性文件中搜索特定文本并轻松替换它,我想这样做,但不确定如何做。
所以我想我可以用另一个已经预定义了新设置的属性文件覆盖它。该 jar 已存在于我的目录中,并且我的 jar 的层次结构如下:
food.jar
/com/food/donut.properties
some file...
some file...
如果我有另一个具有不同设置的 donut.properties 文件,位于不同的目录中。我怎样才能用ant覆盖它?
感谢您的帮助,非常感谢!
编辑:
使用以下代码,我能够将属性文件复制到 jar 中。但是,每当我尝试将新属性文件复制到旧属性文件的同一目录中时,它都不会被替换。 (即,如果我将前缀更改为“com”,我可以看到新的属性文件被插入到 jar 中。如果前缀更改为 com/food,则不会替换任何内容。我做错了什么?
>
I have a settings in a properties file located within a jar that I wish to alter at build-time using ant. Ideally if I am able to search for a specific text in the properties file and replace it easily, I would like to do that but isn't sure how.
So I was thinking I can overwrite it with another properties file that has the new settings already predefined. The jar already exists in my directory and the hierarchy of my jar is as follows:
food.jar
/com/food/donut.properties
some file...
some file...
If I had another donut.properties file with a different setting located in a different directory. How can I overwrite it with ant?
Thanks for the help, much appreciated!
EDIT:
With the following code I was able to copy the properties file into the jar. But whenever I attempt to copy the new properties file into the same directory of the old properties file, it does not get replaced. (i.e. If i change the prefix to 'com' i can see the new properties file being inserted into the jar. If the prefix is changed to com/food, nothing is replaced. What am i doing incorrectly?
<jar destfile="${dist.dir}/food.jar" update="true"> <zipfileset file="donut.xml" prefix="com/food/" /> </jar>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
需要 Ant 1.8.x
步骤 1)
编辑您的属性文件,可以使用多个嵌套条目元素:
请参阅Ant 手册属性文件
步骤2)
使用更改后的属性文件更新您的 jar:
重命名时使用像这样的嵌套映射器:
needs Ant 1.8.x
Step 1)
edit your propertyfile, multiple nested entry elements possible :
see Ant Manual propertyfile
Step 2)
update your jar with the altered propertyfile :
for renaming use nested mapper like that :
jar 任务的 ant 文档 说:
您可能需要确保属性文件比 jar 文件中的属性文件新。使用触摸任务可以解决这个问题。
或者,您可以将 jar 解压缩到临时目录中,使用复制任务复制属性文件并将其覆盖属性设置为 true,然后重新 jar 临时目录的内容。
The ant documentation of the jar task says:
You might need to make sure the properties file is newer than the one in the jar file. Using the touch task could solve the problem.
Or you might just unzip the jar in the temp directory, copy the properties file with the copy task and its overwrite attribute set to true, and re-jar the contents of the temp directory.