标签:
通常在web应用程序需要上线或测试时通常需要部署到类似于tomcat、jboss、weblogic或webspare这些web服务器中,为避免手动部署带来的操作繁琐、易出错等问题,这里使用ant进行标准化部署,这里我的web服务器采用了Nginx+Tomcat集群,so,需要编写的ant脚本如下:
<?xml version="1.0" encoding="UTF-8"?> <project name="oprmonitor-user" basedir="." default="redeploy"> <!-- 系统环境配置 --> <property environment="env" /> <!-- 源码路径 --> <property name="src.dir" value="${basedir}/src" /> <!-- 编译路径 --> <property name="dist.dir" value="${basedir}/dist" /> <!-- 静态资源路径 --> <property name="web.root.dir" value="${basedir}/WebRoot" /> <!-- 当前工程类路径 --> <property name="lib.dir" value="${basedir}/WebRoot/WEB-INF/lib" /> <!-- 部署描述符路径 --> <property name="web.xml.dir" value="${basedir}/WebRoot/WEB-INF/web.xml" /> <!-- War包名称 --> <property name="war.name" value="oprmonitor-user" /> <!-- Tomcat路径 --> <property name="tomcat.dir" value="D:/soft/apache/apache-tomcat-7.0.67" /> <!-- Tomcat可执行脚本路径 --> <property name="tomcat.bin.dir" value="${tomcat.dir}/bin" /> <!-- Tomcat发布应用路径 --> <property name="tomcat.webapps.dir" value="${tomcat.dir}/webapps" /> <!-- Tomcat发布日志路径 --> <property name="tomcat.logs.dir" value="${tomcat.dir}/logs" /> <!-- 当前时间 --> <tstamp> <format property="current.time" pattern="yyyyMMddHHmmss" locale="zh" /> </tstamp> <path id="build-classpath"> <fileset dir="${lib.dir}"> <include name="**/*.jar" /> </fileset> </path> <target name="init" description="验证操作系统"> <echo>验证操作系统...</echo> <echo>${os.name}</echo> <echo>验证操作系统完成</echo> </target> <target name="clean" depends="init" description="清除旧的编译文件"> <echo>正在清除旧的编译文件...</echo> <delete dir="${dist.dir}" /> <mkdir dir="${dist.dir}" /> <echo>清除旧的编译文件完成</echo> </target> <target name="compile" depends="clean" description="编译源码"> <echo>正在编译源码...</echo> <javac srcdir="${src.dir}" destdir="${dist.dir}" includeantruntime="false"> <compilerarg line="-encoding UTF-8" /> <classpath refid="build-classpath" /> </javac> <echo>编译源码完成</echo> </target> <target name="generate-war" depends="compile" description="生成War包"> <echo>正在生成War包...</echo> <delete dir="${basedir}/${war.name}.war" /> <war destfile="${basedir}/${war.name}.war" webxml="${web.xml.dir}" excludes="*.svn"> <fileset dir="${web.root.dir}"> <include name="**/**.*" /> <exclude name="**/*.jar" /> <exclude name="**/*.class" /> </fileset> <lib dir="${lib.dir}" /> <classes dir="${dist.dir}" /> </war> <echo>生成War包完成</echo> </target> <target name="stop-tomcat" depends="generate-war" description="停止Tomcat"> <echo>正在关闭Tomcat...</echo> <exec executable="${tomcat.bin.dir}/shutdown.bat" failonerror="false" output="${tomcat.logs.dir}/log.txt" append="true"> <env key="CATALINA_HOME" path="${tomcat.dir}" /> <arg value="/c shutdown.bat" /> </exec> <!-- 检测tomcat的URL是否访问失败(用not标签),访问失败则认为tomcat已经关闭 --> <waitfor maxwait="5" maxwaitunit="minute" checkevery="5"> <not> <http url="http://localhost:8080/" /> </not> </waitfor> <echo>关闭Tomcat完成</echo> </target> <target name="old-war-exist-check" depends="stop-tomcat" description="检查旧的同名War包是否存在"> <echo>检查旧的同名War包是否存在...</echo> <available file="${tomcat.dir}/webapps/${war.name}.war" property="old-war-isexist" /> </target> <target name="backup-old-war" depends="old-war-exist-check" if="old-war-isexist" description="如果旧的同名War包存在则备份"> <echo>旧的同名war包[存在],开始备份...</echo> <copy tofile="${tomcat.dir}/webapps/${war.name}.war.bak.${current.time}"> <fileset file="${tomcat.dir}/webapps/${war.name}.war" /> </copy> <echo>旧的同名war包[存在],备份完成</echo> </target> <target name="delete-old-war" depends="backup-old-war" if="old-war-isexist" description="如果旧的同名War包备份完成后则删除"> <echo>删除旧的同名war包...</echo> <delete file="${tomcat.dir}/webapps/${war.name}.war" /> <echo>删除旧的同名war包完成</echo> <echo>删除旧的同名war已解压工程...</echo> <delete dir="${tomcat.dir}/webapps/${war.name}" /> <echo>删除旧的同名war已解压工程完成</echo> </target> <target name="copy-new-war-into-webapps" depends="delete-old-war" description="把新的War包复制到Tomcat的webapps中"> <echo>复制新的War包到tomcat的webapps目录下...</echo> <copy tofile="${tomcat.webapps.dir}/${war.name}.war"> <fileset file="${basedir}/${war.name}.war" /> </copy> <echo>复制新的War包到tomcat的webapps目录下完成</echo> </target> <target name="start-tomcat" depends="copy-new-war-into-webapps" description="启动Tomcat"> <echo>正在启动Tomcat...</echo> <exec executable="${tomcat.bin.dir}/startup.bat" failonerror="false" vmlauncher="false" output="${tomcat.logs.dir}/log.txt" append="true"> <env key="CATALINA_HOME" path="${tomcat.dir}" /> <arg value="/c startup.bat" /> </exec> <!-- 检测tomcat的URL是否可以访问成功,访问成功则认为tomcat启动完成 --> <waitfor maxwait="5" maxwaitunit="minute" checkevery="5"> <http url="http://localhost:8080/" /> </waitfor> <echo>启动Tomcat完成</echo> </target> <target name="redeploy" depends="start-tomcat" description="自动发布"> <echo>发布完成</echo> </target> </project>
标签:
原文地址:http://www.cnblogs.com/mengyao/p/5511342.html