Luo Weifeng 2011-4-11
转载保留:http://blog.csdn.net/luoweifeng1989/archive/2011/04/11/6316362.aspx
第一种:使用Copy(PS:需要关闭Tomcat)
<project name="travel" default="deploy" basedir="."> <property name="proj.name" value="travel"/> <!--******************************************************************************* catalina setup ********************************************************************************--> <property environment="env" /> <property name="webserver.home" value="${env.CATALINA_HOME}" /> <property name="webserver.deploy" value="${webserver.home}/webapps" /> <!--******************************************************************************* project structure - should not have to modify ********************************************************************************--> <property name="src.dir" value="src" /> <property name="web.dir" value="web" /> <property name="lib.dir" value="lib" /> <property name="build.dir" value="build" /> <property name="dist.dir" value="dist" /> <property name="javadoc.srcdir" value="./src"/> <property name="javadoc.destdir" value="docs"/> <!--******************************************************************************* CLASSPATH used during compilation ********************************************************************************--> <path id="build.classpath"> <pathelement location="${lib.dir}/servlet-api.jar" /> <pathelement location="${lib.dir}/jsp-api.jar" /> <pathelement location="${lib.dir}/cos.jar" /> </path> <!--******************************************************************************* Pre-compilation rule ********************************************************************************--> <target name="prepare"> <tstamp /> <mkdir dir="${build.dir}" /> <mkdir dir="${build.dir}/WEB-INF" /> <mkdir dir="${build.dir}/WEB-INF/classes" /> <mkdir dir="${build.dir}/WEB-INF/lib" /> <!-- Copy static content of this web application --> <copy todir="${build.dir}"> <fileset dir="${web.dir}" /> </copy> <!-- Copy external dependencies as required --> <copy todir="${build.dir}/WEB-INF/lib"> <fileset dir="${lib.dir}" /> </copy> </target> <!--******************************************************************************* Compiling rule ********************************************************************************--> <target name="compile" depends="prepare"> <javac destdir="${build.dir}/WEB-INF/classes" deprecation="on"> <classpath refid="build.classpath" /> <src path="${src.dir}" /> </javac> </target> <!--******************************************************************************* package war file ********************************************************************************--> <target name="war" depends="compile"> <echo>building war...</echo> <war warfile="${dist.dir}/${proj.name}.war" webxml="${web.dir}/WEB-INF/web.xml"> <fileset dir="${web.dir}"/> <classes dir ="${build.dir}/WEB-INF/classes" /> <classes dir="${lib.dir}"> <include name="*.properties"/> </classes> <lib dir="${lib.dir}"> <include name="*.jar"/> </lib> </war> </target> <!--******************************************************************************* Deploy rule ********************************************************************************--> <target name="deploy" depends="war"> <echo>copying war file to web server deployment dir...</echo> <copy file="${dist.dir}/${proj.name}.war" todir="${webserver.deploy}"/> </target> <!--******************************************************************************* Java Docs ********************************************************************************--> <target name="javadoc" > <mkdir dir="${javadoc.destdir}" /> <echo message="java docs ........."/> <javadoc destdir="${javadoc.destdir}" author="true" version="true" use="true" classpath="${compile.classpath}" encoding="UTF-8" charset="UTF-8" windowtitle="soap stack java api"> <fileset dir="${javadoc.srcdir}" includes="**/*.java"> <depth max="100"/> </fileset> </javadoc> <echo message="javadoc Ok ...Done"/> </target> <!--******************************************************************************* clean compilation remnants ********************************************************************************--> <target name="clean"> <delete dir="${build.dir}" /> </target> </project>
第二种方法:使用Tomcat Mangement(需要保持Tomcat开启,第二次部署时需要在manage里边undeploy或者关闭Tomcat并删除相关)
<project name="TestProject" default="install" basedir="."> <property name="app.name" value="TestProject"/> <property name="app.version" value="0.1-dev"/> <property name="manager.url" value="http://localhost:8080/manager"/> <property name="manager.username" value="admin"/> <property name="manager.password" value=""/> <property file="build.properties"/> <property environment="env"/> <property name="java.home" value="${env.JAVA_HOME}"/> <property name="j2ee.home" value="${env.J2EE_HOME}"/> <property name="webserver.home" value="${env.CATALINA_HOME}"/> <property name="webserver.deploy" value="${webserver.home}/webapps"/> <property name="app.path" value="/${app.name}"/> <property name="war.name" value="${app.name}.war"/> <property name="src.home" value="${basedir}/src"/> <property name="lib.home" value="${basedir}/lib"/> <property name="web.home" value="${basedir}/web"/> <property name="docs.home" value="${basedir}/docs"/> <property name="build.home" value="${basedir}/build"/> <property name="dist.home" value="${basedir}/dist"/> <!-- ==================== Compilation Control Options ==================== --> <!-- These properties control option settings on the Javac compiler when it is invoked using the <javac> task. compile.debug Should compilation include the debug option? compile.deprecation Should compilation include the deprecation option? compile.optimize Should compilation include the optimize option? --> <property name="compile.debug" value="true"/> <property name="compile.deprecation" value="false"/> <property name="compile.optimize" value="true"/> <!-- ================== Custom Ant Task Definitions ======================= --> <!-- These properties define custom tasks for the Ant build tool that interact with the "/manager" web application installed with Tomcat 5. Before they can be successfully utilized, you must perform the following steps: - Copy the file "server/lib/catalina-ant.jar" from your Tomcat 5 installation into the "lib" directory of your Ant installation. - Create a "build.properties" file in your application's top-level source directory (or your user login home directory) that defines appropriate values for the "manager.password", "manager.url", and "manager.username" properties described above. For more information about the Manager web application, and the functionality of these tasks, see <http://localhost:8080/tomcat-docs/manager-howto.html>. --> <taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask"/> <taskdef name="list" classname="org.apache.catalina.ant.ListTask"/> <taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask"/> <taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask"/> <!-- ==================== External Dependencies =========================== --> <!-- Use property values to define the locations of external JAR files on which your application will depend. In general, these values will be used for two purposes: * Inclusion on the classpath that is passed to the Javac compiler * Being copied into the "/WEB-INF/lib" directory during execution of the "deploy" target. Because we will automatically include all of the Java classes that Tomcat 5 exposes to web applications, we will not need to explicitly list any of those dependencies. You only need to worry about external dependencies for JAR files that you are going to include inside your "/WEB-INF/lib" directory. This is done in path definition below. --> <!-- ==================== Compilation Classpath =========================== --> <!-- Rather than relying on the CLASSPATH environment variable, Ant includes features that makes it easy to dynamically construct the classpath you need for each compilation. The example below constructs the compile classpath to include the servlet.jar file, as well as the other components that Tomcat makes available to web applications automatically, plus anything that you explicitly added. --> <path id="compile.classpath"> <!-- Include all JAR files that will be included in /WEB-INF/lib --> <fileset dir="${lib.home}"> <include name="*.jar"/> </fileset> <!-- Include all elements that Tomcat exposes to applications --> <pathelement location="${catalina.home}/common/classes"/> <fileset dir="${webserver.home}/common/endorsed"> <include name="*.jar"/> </fileset> <fileset dir="${webserver.home}/common/lib"> <include name="*.jar"/> </fileset> <pathelement location="${webserver.home}/shared/classes"/> <fileset dir="${webserver.home}/shared/lib"> <include name="*.jar"/> </fileset> </path> <!-- ==================== Clean Target ==================================== --> <!-- The "clean" target deletes any previous "build" and "dist" directory, so that you can be ensured the application can be built from scratch. --> <target name="clean" description="Delete old build and dist directories"> <delete dir="${build.home}"/> <delete dir="${dist.home}"/> </target> <!-- ==================== Prepare Target ================================== --> <!-- The "prepare" target is used to create the "build" destination directory, and copy the static contents of your web application to it. If you need to copy static files from external dependencies, you can customize the contents of this task. Normally, this task is executed indirectly when needed. --> <target name="prepare" depends="clean"> <!-- Create build directories as needed --> <mkdir dir="${build.home}"/> <mkdir dir="${build.home}/WEB-INF"/> <mkdir dir="${build.home}/WEB-INF/classes"/> <mkdir dir="${build.home}/WEB-INF/lib"/> <!-- Copy static content of this web application --> <copy todir="${build.home}"> <fileset dir="${web.home}"/> </copy> <!-- Copy external dependencies as required --> <copy todir="${build.home}/WEB-INF/lib"> <fileset dir="${lib.home}"/> </copy> <!-- Copy static files from external dependencies as needed --> <!-- *** CUSTOMIZE HERE AS REQUIRED BY YOUR APPLICATION *** --> </target> <!-- ==================== Compile Target ================================== --> <!-- The "compile" target transforms source files (from your "src" directory) into object files in the appropriate location in the build directory. This example assumes that you will be including your classes in an unpacked directory hierarchy under "/WEB-INF/classes". --> <target name="compile" depends="prepare" description="Compile Java sources"> <!-- Compile Java classes as necessary --> <javac srcdir="${src.home}" destdir="${build.home}/WEB-INF/classes" debug="${compile.debug}" deprecation="${compile.deprecation}" optimize="${compile.optimize}"> <classpath refid="compile.classpath"/> </javac> <!-- Copy application resources --> <copy todir="${build.home}/WEB-INF/classes"> <fileset dir="${src.home}" excludes="**/*.java"/> </copy> </target> <!-- ==================== All Target ====================================== --> <!-- The "all" target is a shortcut for running the "clean" target followed by the "compile" target, to force a complete recompile. --> <target name="all" depends=" clean,compile, dist, install" description="Clean build and dist directories, then compile"/> <!-- ==================== Javadoc Target ================================== --> <!-- The "javadoc" target creates Javadoc API documentation for the Java classes included in your application. Normally, this is only required when preparing a distribution release, but is available as a separate target in case the developer wants to create Javadocs independently. --> <target name="javadoc" depends="compile" description="Create Javadoc API documentation"> <mkdir dir="${dist.home}/docs/api"/> <javadoc sourcepath="${src.home}" destdir="${dist.home}/docs/api" packagenames="*"> <classpath refid="compile.classpath"/> </javadoc> </target> <!-- ==================== Dist Target ===================================== --> <!-- The "dist" target creates a binary distribution of your application in a directory structure ready to be archived in a tar.gz or zip file. Note that this target depends on two others: * "compile" so that the entire web application (including external dependencies) will have been assembled * "javadoc" so that the application Javadocs will have been created <jar jarfile="${dist.home}/${app.name}-${app.version}.war" <target name="dist" depends="compile,javadoc" --> <target name="dist" depends="compile" description="Create binary distribution"> <!-- Copy documentation subdirectories --> <mkdir dir="${dist.home}/docs"/> <copy todir="${dist.home}/docs"> <fileset dir="${docs.home}"/> </copy> <!-- Create application JAR file --> <jar jarfile="${dist.home}/${war.name}" basedir="${build.home}"/> <!-- Copy additional files to ${dist.home} as necessary --> </target> <!-- ==================== Install Target ================================== --> <!-- The "install" target tells the specified Tomcat 5 installation to dynamically install this web application and make it available for execution. It does *not* cause the existence of this web application to be remembered across Tomcat restarts; if you restart the server, you will need to re-install all this web application. If you have already installed this application, and simply want Tomcat to recognize that you have updated Java classes (or the web.xml file), use the "reload" target instead. NOTE: This target will only succeed if it is run from the same server that Tomcat is running on. NOTE: This is the logical opposite of the "remove" target. <target name="install" depends="compile" --> <target name="install" depends="dist" description="Install application to servlet container"> <deploy url="${manager.url}" username="${manager.username}" password="${manager.password}" path="${app.path}" war="${dist.home}/${war.name}"/> </target> <!-- ====================== List Target =================================== --> <!-- The "list" target asks the specified Tomcat 5 installation to list the currently running web applications, either loaded at startup time or installed dynamically. It is useful to determine whether or not the application you are currently developing has been installed. --> <target name="list" description="List installed applications on servlet container"> <list url="${manager.url}" username="${manager.username}" password="${manager.password}"/> </target> <!-- ==================== Reload Target =================================== --> <!-- The "reload" target tells the specified Tomcat 5 installation to dynamically reload this web application, to reflect changes in the underlying classes or the "web.xml" deployment descriptor. --> <target name="reload" depends="compile" description="Reload application on servlet container"> <reload url="${manager.url}" username="${manager.username}" password="${manager.password}" path="${app.path}"/> </target> <!-- ==================== Remove Target =================================== --> <!-- The "remove" target tells the specified Tomcat 5 installation to dynamically remove this web application from service. It removes the application from the server the folder or jar file is completely deleted from the server. NOTE: This is the logical opposite of the "install" target. --> <target name="remove" description="Remove application on servlet container"> <undeploy url="${manager.url}" username="${manager.username}" password="${manager.password}" path="${app.path}"/> </target> <!-- ======================== Zip Target =================================== --> <!-- target "zip": zips all the files in this project and keep it in current folder This is for archive porpose --> <target name="zip" depends=""> <tstamp/> <property name="zip.name" value="${app.name}-${DSTAMP}" /> <jar jarfile="${zip.name}.zip"> <fileset dir="${basedir}" includes="*.xml, src/**, web/**, lib/**, docs/**" excludes="**/CVS/**" /> </jar> </target> <!-- ==================== Deploy Target ================================== --> <!-- The "deploy" target deploy the war file to the web application. This is different from the definition of "install" where a dynamic installation is executed. Deploy copy the war file to server web application directory <target name="deploy" depends="dist" --> <target name="deploy" depends="dist" description="Deploy the war file from dist dir to tomcat server"> <copy todir="${webserver.deploy}"> <fileset dir="${dist.home}" includes="${war.name}"/> </copy> </target> <!-- ======================= Help Target =================================== --> <!-- Display help information --> <!-- target: Display help info --> <target name="help" description="Display help info. "> <echo message="ant clean: clean build and dist dirs."/> <echo message="ant prepare: makdir for WEN-INF, copy web and lib files. "/> <echo message="ant compile: compile java file and copy to WEB-INF/classes."/> <echo message="ant : default, do compile. "/> <echo message="ant javadoc: "/> <echo message="ant dist: call targets compile and javadoc, make war file "/> <echo message="ant install: deploy war to container."/> <echo message="ant list: list all currently running web app in server. "/> <echo message="ant remove: remove this web app from server."/> <echo message="ant reload: reload this web app to reflect new changes. "/> <echo message="ant -f build_tomcat.xml: to build using this build file "/> </target> </project>