[英]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.