How Apache Ant deploys .war file to Tomcat

2019-02-13 13:48发布

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>

4条回答
一夜七次
2楼-- · 2019-02-13 14:05

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.

查看更多
来,给爷笑一个
3楼-- · 2019-02-13 14:10

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.

查看更多
别忘想泡老子
4楼-- · 2019-02-13 14:12

Probably, you're first copying all your files in your dest dir an then making war file, you should instead copy your files to some temp directory, create war file, copy it to dest dir, remove temp directory.

查看更多
戒情不戒烟
5楼-- · 2019-02-13 14:15

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.

查看更多
登录 后发表回答