【问题标题】:How Apache Ant deploys .war file to TomcatApache Ant 如何将 .war 文件部署到 Tomcat
【发布时间】:2011-09-02 03:41:04
【问题描述】:

我正在使用 Apache Ant 1.8 将 Web 应用程序部署到本地 Tomcat 服务器中,当我在命令行运行“ant deploy”时,build.xml 文件(如下)会产生预期的效果。

我的问题是,我注意到 .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>

【问题讨论】:

    标签: java apache tomcat deployment ant


    【解决方案1】:

    Tomcat 有一个 autodeploy 文件夹,您放置的任何 war 文件都将被自动解包和部署。您的 ant 文件只是通过调用 tomcat-manager Web 应用程序(预打包到 tomcat 中)中的特殊 URL 将 war 文件复制到此目录中。

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

    您可以让 ant 为 tomcat 执行一些特定的 ant 任务。特别是如果 Tomcat 服务器不在本地计算机上。 See this link for details.

    【讨论】:

    • 有一个主要区别,用于部署的 API 确保副本是原子的,而使用文件系统可能会导致 Tomcat Autodeploy 在完成复制之前开始处理 WAR 文件。
    【解决方案2】:

    您已在 Tomcat 安装中启用了自动部署。 This link 给出了自动部署的详细概述,但简而言之,Tomcat 会扫描某些目录以查找更新的 web.xml 和 war 文件。如果它找到一个战争文件,它会自动部署它。

    一种更好的部署方式(特别是如果您需要部署到远程机器上)是使用 Tomcat 附带的 Ant 任务。 This page 展示了如何设置您的构建文件,以便您可以从 Ant 部署和取消部署。页面很旧,但信息仍然很好。这是我用来部署到 Tomcat 的 build.xml 的 sn-p:

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

    【讨论】:

      【解决方案3】:

      我对 Tomcat 的 Ant 部署任务非常幸运。查看Executing Manager Commands With Ant documentation 获取信息。如果你决定走那条路,你应该能够在短时间内让它发挥作用。

      【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-05
        • 2017-01-24
        • 2016-08-23
        • 2012-05-08
        • 2018-07-25
        • 1970-01-01
        • 2014-04-19
        相关资源
        最近更新 更多