How Apache Ant deploys .war file to Tomcat

24,437

Solution 1

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.

Solution 2

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.

Solution 3

I've had very good luck with Tomcat's Ant tasks for deployment. Have a look at the Executing Manager Commands With Ant documentation for information. If you decide to go that route, you should be able to get it working in short order.

Share:
24,437
dvanaria
Author by

dvanaria

I'm currently working for a telecommunications company in Colorado, doing some software development and systems integration work. I've been interested in programming from an early age, from about when I was 13 or so, programming on Apple II systems at school and eventually at home when my father bought me an Apple IIc. I loved picking up programming books from the public library and just picking out whatever interested me and trying it out first hand. I went on to learn C programming in college, then OpenGL graphics programming, Object Oriented Programming with Java, just about anything new to me I found interesting. Today I still work on my own programming projects (now usually in Python and C++), but I've always had great memories of how much fun it was to discover programming when I was a kid. I'm more interested these days in getting other people interested in computers and programming, maybe trying to inspire other people (especially kids) to get into it. I'm always going back to the basics and really trying to break concepts down so they are accessible and so I understand them better myself. My hope is to one day either write a programming book for kids, that recaptures some of that wonder and excitement, or develop a series of YouTube tutorials that may help newbies pick up programming in a way that is more accessible and easily understood. I think today's biggest barrier to entry in this field of interest is the complexity of today’s systems. It's not like the old days where you turned your computer on and it booted in a few seconds into a BASIC command prompt. Those systems were a lot of fun because you had to pick up programming right from the start in order to really do anything with them. Either way, this site (Stack Overflow) has been a tremendous help to me and a lot of fun to contribute to. Ideally, I would love to feel some kind of expertise with programming in general, and this site is a good step toward getting that kind of experience – the best way to learn anything, I’m convinced, is to teach others, to ask a lot of questions, and help out other people by answering their questions.

Updated on July 09, 2022

Comments

  • dvanaria
    dvanaria almost 2 years

    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>
    

  • Mike Christianson
    Mike Christianson almost 13 years
    @dvanaria: As an aside, your build.xml file looks a lot like one of the JavaRanch standard build files... perhaps you could ask their community for help, too?
  • Andrew T Finnell
    Andrew T Finnell over 5 years
    There's one main difference, the API for deployment ensures the copy is Atomic, where as using the File System can cause the Tomcat Autodeploy to start processing the WAR file before it's finished copying.