2011-05-25 111 views
7

我使用Apache Ant 1.8将Web应用程序部署到本地Tomcat服务器,并且在运行'ant deploy'时,build.xml文件(下文)会产生所需的效果。在命令行。我的问题是,我注意到.war文件被放置在我期望的位置(deploy.dir在我的主目录的build.properties文件中定义),但它也意外地解压缩了.war并提取了上下文本身进入同一个目录。在下面的build.xml文件中的配置?Apache Ant如何将.war文件部署到Tomcat

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

回答

12

Tomcat有在您将任何战争的文件会自动解压缩并部署了一个自动部署文件夹。你的ant文件只是通过调用tomcat-manager web应用程序(预先包装到tomcat中)的特殊URL将war文件复制到这个目录中。

从这一刻起,所有事情都由tomcat内核自动处理,就像手动将war文件复制到webapps目录中一样。

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

0

也许,你第一次复制在你dest dir的再制造war文件中的所有文件,你应该将你的文件复制到一些temp目录,创建war文件,将其复制到dest dir,除去temp目录。

1

我用Tomcat的Ant部署任务取得了非常好的运气。查看Executing Manager Commands With Ant documentation的信息。如果你决定走这条路线,你应该能够在短时间内完成工作。

+0

@dvanaria:另外,您的build.xml文件看起来很像[JavaRanch标准构建文件之一](http://www.javaranch.com/drive/servlet/index.jsp#ant)。 ..也许你也可以向他们的社区寻求帮助? – 2011-05-25 22:32:32

3

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

更好的部署方式(尤其是如果您需要部署到远程计算机)是使用Tomcat附带的Ant任务。 This page显示了如何设置您的构建文件,以便您可以从Ant进行部署和取消部署。该网页是旧的,但信息仍然很好。下面是一个build.xml我用它来部署到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> 

你可以在Tomcat的lib目录卡塔利娜 - ant.jar文件。