我正在使用Apache Ant 1.8将Web应用程序部署到本地Tomcat服务器,而当我在命令行运行“ant deploy”时,build.xml文件(如下)会产生所需的效果。
我的问题是,我注意到.war文件被放置在我期望的位置(deploy.dir在我的主目录的build.properties文件中定义),但是它也意外地解压缩了.war文件,并将上下文本身提取到同一个文件中目录。 在下面的build.xml文件中的configuration?
<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-manager Web应用程序中的一个特殊的URL(它被预先包装到tomcat中)来将war文件复制到这个目录中。
从这一点开始,所有事情都是由tomcat内核自动处理的,就像手动将war文件复制到webapps目录一样。
你可以让ant为tomcat做一些特定的ant任务。 特别是如果Tomcat服务器不在本地机器上。 详细信息请参阅此链接 。
您的Tomcat安装中已启用自动部署。 该链接提供了autodeploy的详细概述,但简而言之,Tomcat会扫描某些目录以更新web.xml和war文件。 如果它发现一个战争文件,它会自动部署它。
更好的部署方式(尤其是如果您需要部署到远程计算机)是使用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文档 。 如果你决定走这条路,你应该能够在短期内完成工作。
也许,你首先复制你的目录中的所有文件,然后制作war
文件,而不是将文件复制到temp
目录,创建war
文件,将其复制到dest dir
,删除temp
目录。