Alex Rivera | Logout

Updating files inside an existing WAR file

Asked 2010-02-05T20:03:12.027
16

I am trying to update files inside an existing WAR file using the ANT WAR task. I need to replace a set of xml files in a folder in WAR with new ones from my HDD.

<war destfile="myApp.war" update="true" >
    <zipfileset dir="<PathToStubsFolderOnHDD>" includes="**/*.xml" prefix="<PathToStubsFolderInWAR>"/>
</war>

This works fine if the original WAR does not have xmls with same name. However if the original WAR contains xmls with same name; WAR task does not update them with files from HDD.

The ANT WAR task documentation reads:

update | indicates whether to update or overwrite the destination file if it already exists. Default is "false".
duplicate | behavior when a duplicate file is found. Valid values are "add", "preserve", and "fail". The default value is "add".

if i use update="false"; all other files in the original WAR are deleted and only the new xmls stored.

using duplicate="add" didnot have any effect either.

Any suggestions on how this can be achieved??

ant
Edit
Report

2 Answers

9

Thanks Aito!

Here is the complete ANT script:

<target name = "UpdateWARWithStubs"
    description="Updates WAR file with files from Stub folder">

    <!-- Use touch to set modification time of all stubs to current time. This will force war task to update stub files inside the war -->
    <tstamp> <format property="touch.time" pattern="MM/dd/yyyy hh:mm aa"/>  </tstamp>
    <touch datetime="${touch.time}">
        <fileset dir="${path.to.stubs.on.hdd}" />
    </touch>

    <war destfile="myApp.war" update="true">
        <zipfileset dir="${path.to.stubs.on.hdd}"  prefix="${path.to.stubs.in.war}"/>
    </war>
</target>
answered 2010-02-08T06:28:33.313
0

Neither touch option worked for me. The original war file being created by a maven artifact from within the ant script might have something to do with this. Ended up repackaging the war file into a temp file and overwriting the original war file like this:

<target name="update-properties">
    <war destfile="target/${war.name}temp" needxmlfile='false'>
        <zipfileset src="target/${war.name}" includes="**/*" 
                      excludes="${path.to.properties.in.war}" />
        <zipfileset file="${path.to.new.properties}"  
                      fullpath="${path.to.properties.in.war}" />
    </war>
    <move file="target/${local.war.name}temp" tofile="target/${local.war.name}"/>
    <delete file="target/${local.war.name}temp"/>
</target>

where ${path.to.properties.in.war} can be something like "WEB-INF/classes/META-INF/spring/database.properties"

answered 2013-03-19T16:20:14.367

Your Answer