繁体   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.

我的问题是,我注意到 .war 文件被放置在我期望的位置(deploy.dir 在我的主目录的 build.properties 文件中定义),但它也意外地解压了 .war 并将上下文本身提取到相同的位置目录。 下面的 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 有一个 autodeploy 文件夹,您放置的任何 war 文件都将自动解包和部署。 您的 ant 文件只是通过在 tomcat 管理器 web 应用程序中调用特殊的 URL 将 war 文件复制到此目录中(这是预打包的 tocat)。

从此时起,一切都由 tomcat 内核自动处理,就像您手动将 war 文件复制到 webapps 目录中一样。

您可以让 ant 为 tomcat 执行一些特定的 ant 任务。 特别是如果 Tomcat 服务器不在本地计算机上。 有关详细信息,请参阅此链接

您已在 Tomcat 安装中打开了自动部署。 此链接提供了自动部署的详细概述,但简而言之,Tomcat 会扫描某些目录以查找更新的 web.xml 和战争文件。 如果它找到一个战争文件,它会自动部署它。

更好的部署方式(特别是如果您需要部署到远程计算机)是使用 Tomcat 附带的 Ant 任务。 此页面显示如何设置您的构建文件,以便您可以从 Ant 部署和取消部署。 页面很旧,但信息仍然很好。 这是我用来部署到 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>

您可以在 Tomcat 的 lib 目录中找到 catalina-ant.jar。

我对 Tomcat 的 Ant 部署任务非常幸运。 查看使用 Ant 文档执行管理器命令以获取信息。 如果您决定使用 go 这条路线,您应该能够在短时间内让它工作。

可能,您首先复制dest dir中的所有文件,然后制作war文件,您应该将文件复制到某个temp目录,创建war文件,将其复制到dest dir ,删除temp目录。

暂无
暂无

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

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