tags:

views:

278

answers:

3

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

A: 

Perhaps the easiest way is to explode the war to a temporary directory, make your changes and then rebuild the war. Not nice, admittedly.

Brian Agnew
A: 

Seems that with 'update=true' option, the war file is newer than the files that you want to update. Someone suggest to apply the 'touch' task with '0' to workaround the problem.

The zip task checks only if the source files you whish to add to an existing zip are more recent than the zip. A workaround would be to the zip file before adding.

Or you could do the reverse stuff:

Performing a 'touch' before the 'war' task on xml file(s) makes it work.

I hope it helps you.

Aito
A: 

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>
Nirmal Patel
I'm glad that it works!
Aito