This is my solution to replace existing files in zip(.war) file. Initial state is I have build.xml to compile and package mywebapp.war for Tomcat6 server. Minor configuration changes within war file are required for Tomcat7 server.
- project subfolders web and webT7 are CVS controlled folders, I don't want to touch timestamps for no reason. The following trick does the job.
- compile and create web/WEB-INF/lib/mywebapp.jar file as usual
- target "war" creates tomcat6 archive from web folder
- have few specific files in webT7 subfolder such as META-INF/context.xml and WEB-INF/web.xml ones I have to modify a bit for each tomcat version.
- create a copy of mywebapp.war.zip file
- copy files from webT7 subfolder to a new build temp folder, do not use preservelastmodified attribute !!! This gives a new timestamp for each files, so its touches without separate touch command.
- failsafe touch new zip to put past timestamp, this makes sure zip update works properly.
- use zip task to update mywebapp.war_T7.zip content, it should replace existing files because we copied them without preserving original timestamps.
Reason I copy webT7 content to a temp build folder is content management systems. I don't want to change timestamps of the original repository files for no reason. Everything else compile,jar,war targets are always same no matter what Tomcat target I use.
Zip update="true" attribute as was said earlier do not replace files, it updates only if zip had older file than the one we give. This may introduce problems if I have web/config.jsp(2013-01-21 14:01:01) and webT7/config.jsp(2012-12-21 15:02:03) files. File was not replaced.
Snippet from build.xml file
<target name="war" depends="compile,jar" description="Create a .war file">
<delete file="${name}.war.zip" />
<zip destfile="${name}.war.zip"
basedir="./web/"
excludes="
**/CVS*
"
/>
</target>
<target name="warT7" depends="war" description="Create a .war file for Tomcat7">
<delete dir="${build}" />
<mkdir dir="${build}" />
<delete file="${name}.war_T7.zip" />
<copy file="${name}.war.zip"
tofile="${name}.war_T7.zip" overwrite="true" preservelastmodified="true"
/>
<copy todir="${build}" overwrite="true">
<fileset dir="./webT7" />
</copy>
<touch datetime="01/31/1981 01:00:00 AM" file="${name}.war_T7.zip" />
<zip destfile="${name}.war_T7.zip" update="true">
<zipfileset dir="${build}" prefix="" />
</zip>
<delete dir="${build}" />
</target>