简体   繁体   English

Apache Ant 如何将.war 文件部署到 Tomcat

[英]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. 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.我的问题是,我注意到 .war 文件被放置在我期望的位置(deploy.dir 在我的主目录的 build.properties 文件中定义),但它也意外地解压了 .war 并将上下文本身提取到相同的位置目录。 Where in the below build.xml file is that configured?下面的 build.xml 文件在哪里配置?

  <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>

Tomcat has an autodeploy folder in which any war file that you place will be automatically unpacked and deployed. Tomcat 有一个 autodeploy 文件夹,您放置的任何 war 文件都将自动解包和部署。 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).您的 ant 文件只是通过在 tomcat 管理器 web 应用程序中调用特殊的 URL 将 war 文件复制到此目录中(这是预打包的 tocat)。

From this point on everything is handled by the tomcat core automatically, just if you copied the war file into the webapps directory manually.从此时起,一切都由 tomcat 内核自动处理,就像您手动将 war 文件复制到 webapps 目录中一样。

You can have ant do a lot more with some specific ant tasks for tomcat.您可以让 ant 为 tomcat 执行一些特定的 ant 任务。 Especially if the Tomcat server is not on the local machine.特别是如果 Tomcat 服务器不在本地计算机上。 See this link for details . 有关详细信息,请参阅此链接

You have autodeploy turned on in your Tomcat installation.您已在 Tomcat 安装中打开了自动部署。 This link gives a detailed overview of autodeploy, but in a nutshell, Tomcat scans certain directories for updated web.xml and war files. 此链接提供了自动部署的详细概述,但简而言之,Tomcat 会扫描某些目录以查找更新的 web.xml 和战争文件。 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.更好的部署方式(特别是如果您需要部署到远程计算机)是使用 Tomcat 附带的 Ant 任务。 This page shows how to set up your build file so you can deploy and undeploy from Ant.此页面显示如何设置您的构建文件,以便您可以从 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:这是我用来部署到 Tomcat 的 build.xml 的片段:

<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.您可以在 Tomcat 的 lib 目录中找到 catalina-ant.jar。

I've had very good luck with Tomcat's Ant tasks for deployment.我对 Tomcat 的 Ant 部署任务非常幸运。 Have a look at the Executing Manager Commands With Ant documentation for information.查看使用 Ant 文档执行管理器命令以获取信息。 If you decide to go that route, you should be able to get it working in short order.如果您决定使用 go 这条路线,您应该能够在短时间内让它工作。

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.可能,您首先复制dest dir中的所有文件,然后制作war文件,您应该将文件复制到某个temp目录,创建war文件,将其复制到dest dir ,删除temp目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM