How Apache Ant Deploys .war File in Tomcat

I use Apache Ant 1.8 to deploy a web application to a local Tomcat server, and the build.xml file (below) gives the desired effect when running 'ant deploy' on the command line.

My question is that I noticed that the .war file is placed where I expect it to be. (deploy.dir is defined in the home.properties file of my home directory), but it also unexpectedly unzipped .war and extracted the context itself into the same directory. Where in the build.xml file below is configured?

<target name='init'> <property file='${user.home}/build.properties'/> <property name='app.name' value='${ant.project.name}'/> <property name='src.dir' location='src'/> <property name='lib.dir' location='lib'/> <property name='build.dir' location='build'/> <property name='classes.dir' location='${build.dir}/classes'/> <property name='dist.dir' location='${build.dir}/dist'/> </target> <target name='initdirs' depends='init'> <mkdir dir='${classes.dir}'/> <mkdir dir='${dist.dir}'/> </target> <target name='compile' depends='initdirs'> <javac srcdir='${src.dir}/java' destdir='${classes.dir}'> <!-- <classpath> <fileset dir='${lib.dir}/development' includes='javaee.jar'/> <fileset dir='${lib.dir}/production' includes='jr.jar'/> </classpath> --> </javac> </target> <target name='war' depends='compile'> <war destFile='${dist.dir}/${app.name}.war' webxml='${src.dir}/web/WEB-INF/web.xml'> <classes dir='${classes.dir}'/> <!-- <zipfileset dir='${lib.dir}/production' includes='jr.jar' prefix='WEB-INF/lib' /> --> <fileset dir='${src.dir}/web' excludes='WEB-INF/web.xml' /> </war> </target> <target name='build' depends='war' description='compile and create the war' /> <target name='clean' depends='init' description='Use for a clean build'> <delete dir='${build.dir}' /> </target> <target name='ffbuild' depends='clean, build' description='clean and create the war'/> <target name='deploy' depends='initdirs' description='copy the war file to the app server'> <delete verbose='true' dir='${deploy.dir}/${app.name}'/> <fail unless='deploy.dir' message='build.properties must exist in your home directory and define deploy.dir' /> <copy todir='${deploy.dir}' file='${dist.dir}/${app.name}.war'/> </target> 

+7
source share
4 answers

Tomcat has an autodeploy folder in which any war file that you place will be automatically unpacked and deployed. Your ant file simply copies the war file to this directory, invoking a special URL in the tomcat-manager web application (which is pre-packaged in tomcat).

From now on, everything is processed automatically by the tomcat core only if you manually copied the war file into the webapps directory.

You can have ant do much more with some specific ant tasks for tomcat. Especially if the Tomcat server is not located on the local machine. See this link for more details .

+12
source

You have autorun enabled on your Tomcat installation. This link provides a detailed overview of autodeploy, but in a nutshell Tomcat scans specific directories for updated web.xml and war files. If he finds a war file, he automatically deploys it.

The best way to deploy (especially if you ever need to deploy to a remote computer) is to use the Ant tasks that come with Tomcat. This page shows how to customize the build file so that you can deploy and remove from Ant. The page is out of date, but the information is still good. Here is a snippet of the build.xml file that I use to deploy to Tomcat:

 <taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask"> <classpath> <path location="${build-jars}/catalina-ant.jar" /> </classpath> </taskdef> <target name="buildAndDeploy" depends="buildWar"> <deploy url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" path="/${target.name}" update="true" war="file:${basedir}/deploy/${target.name}.war" /> </target> 

You can find the ant.jar directory directory in the Tomcat lib directory.

+3
source

I was very lucky with the Tomcat Ant deployment tasks. Take a look at Execution Manager Commands with Ant documentation . If you decide to go along this route, you can make it work in a short time.

+1
source

Perhaps you first copy all your files to your dest dir and then create a war file, instead copy the files to some temp directory, create a war file, copy it to dest dir >, delete the temp directory.

0
source

All Articles