Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing properties file inside a jar with Ant

Tags:

ant

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>
like image 404
user459811 Avatar asked Oct 12 '11 20:10

user459811


1 Answers

needs Ant 1.8.x

Step 1)
edit your propertyfile, multiple nested entry elements possible :

<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>

see Ant Manual propertyfile

Step 2)
update your jar with the altered propertyfile :

<jar destfile="/path/to/your/foo.jar" update="true">
 <fileset dir="/path/to/propertyfile" includes="*.properties"/>
</jar>

for renaming use nested mapper like that :

<jar destfile="/path/to/your/foo.jar" update="true">
 <mappedresources>
  <fileset dir="." includes="*.properties"/>
   <globmapper from="*.properties" to="/com/xml/*.properties"/>
 </mappedresources>
</jar
like image 191
Rebse Avatar answered Oct 20 '22 11:10

Rebse