2011-05-25 108 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文件。