Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Apache Ant deploys .war file to Tomcat

I'm using Apache Ant 1.8 to deploy a web application into a local Tomcat server, and the build.xml file (below) produces the desired effect when I run 'ant deploy' at the command line.

My question is, I noticed that the .war file gets placed where I expect it to (deploy.dir is defined in my home directory's build.properties file), but it also unexpectedly unpacked the .war and extracted the context itself into that same directory. Where in the below build.xml file is that 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>

like image 631
dvanaria Avatar asked May 25 '11 21:05

dvanaria


People also ask

Which folder under the Tomcat server installation should you deploy your .WAR file to?

Perhaps the simplest way to deploy a WAR file to Tomcat is to copy the file to Tomcat's webapps directory. Copy and paste WAR files into Tomcat's webapps directory to deploy them. Tomcat monitors this webapps directory for changes, and if it finds a new file there, it will attempt to deploy it.

Can Apache HTTP server deploy WAR files?

Deploying a web application to Apache Tomcat is very straightforward using a WAR (Web ARchive) file. By deploying we mean that we are placing a zipped web application in a location on the file system where Tomcat can make the web page(s) available to the world.

Where does Tomcat deploy WAR files?

war files, for example authService. war . Paste the files to the following location: For Tomcat, <User install directory>\apache-tomcat-8.5.


2 Answers

Tomcat has an autodeploy folder in which any war file that you place will be automatically unpacked and deployed. Your ant file is simply copying the war file into this directory by calling a special URL in the tomcat-manager web application (which is prepackaged into the tomcat).

From this point on everything is handled by the tomcat core automatically, just if you copied the war file into the webapps directory manually.

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

like image 53
Vincent Ramdhanie Avatar answered Sep 21 '22 23:09

Vincent Ramdhanie


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

A better way to deploy (especially if you'll ever need to deploy to a remote machine) is to use the Ant tasks that come with Tomcat. This page shows how to set up your build file so you can deploy and undeploy from Ant. The page is old but the information is still good. Here's a snippet of a build.xml 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 catalina-ant.jar in Tomcat's lib directory.

like image 39
Paul Avatar answered Sep 21 '22 23:09

Paul