5

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>
user459811
  • 2,874
  • 10
  • 37
  • 63
  • Hope this will do the trick http://stackoverflow.com/questions/4737560/how-do-i-modify-a-file-in-a-jar-file-using-ant – Charls Apr 01 '14 at 08:30

2 Answers2

5

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
Rebse
  • 10,307
  • 2
  • 38
  • 66
3

The ant documentation of the jar task says:

The update parameter controls what happens if the JAR file already exists. When set to yes, the JAR file is updated with the files specified. When set to no (the default) the JAR file is overwritten. An example use of this is provided in the Zip task documentation. Please note that ZIP files store file modification times with a granularity of two seconds. If a file is less than two seconds newer than the entry in the archive, Ant will not consider it newer.

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I can perform a manual unzip and move but I wanted a way to automate it. I've also tried touch but I'm still encountering the same problems. – user459811 Oct 13 '11 at 15:22
  • My point was that you could use ant, with its unzip task, to unzip it, the use the ant copy task to overwrite the properties file, and the zip again with the ant zip task. Everything automated with ant. – JB Nizet Oct 13 '11 at 22:41