4

I need to unzip a war file in tomcat/webapps directory using ANT build script. The war file name is not fixed. How can I unzip it in a directory whose name is the same as the war file name. I know how to unzip the file but the problem is it unzips the content in the specified destination directory. What if I don't know the directory name?

before build:

tomcat/webapps/
   myApp-0.1.war

after the build:

tomcat/webapps
   myApp-0.1/
   myApp-0.1.war
bluetech
  • 1,380
  • 3
  • 13
  • 22
  • Shouldn't the directory name be `myApp-0.1` or should the directory names be all lowercase? – beny23 Aug 28 '12 at 21:56

2 Answers2

5

So after learning about some Ant tasks here is what I came up with:

<!-- Get the path of the war file. I know the file name pattern in this case -->
<path id="warFilePath">
    <fileset dir="./tomcat/webapps/">
        <include name="myApp-*.war"/>
    </fileset>
</path>

<property name="warFile" refid="warFilePath" />

<!-- Get file name without extension -->
<basename property="warFilename" file="${warFile}" suffix=".war" />

<!-- Create directory with the same name as the war file name -->
<mkdir dir="./tomcat/webapps/${warFilename}" />

<!-- unzip war file -->
<unwar dest="./tomcat/webapps/${warFilename}">
    <fileset dir="./tomcat/webapps/">
        <include name="${warFilename}.war"/>    
    </fileset>
</unwar>

Let me know if there is a better way to do it. I also found a solution on stackoverflow using ant-contrib but that was not I was looking for.

Community
  • 1
  • 1
bluetech
  • 1,380
  • 3
  • 13
  • 22
3

Nice work bluetech. Your solution could also be expressed as follows:

<target name="unwar-test">
  <property name="webapps.dir" value="tomcat/webapps" />

  <fileset id="war.file.id" dir="${basedir}"
      includes="${webapps.dir}/myApp-*.war" />
  <property name="war.file" refid="war.file.id" />

  <basename property="war.basename" file="${war.file}" suffix=".war" />
  <property name="unwar.dir" location="${webapps.dir}/${war.basename}" />
  <mkdir dir="${unwar.dir}" />
  <unwar dest="${unwar.dir}" src="${war.file}" />
</target>
Community
  • 1
  • 1
Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117