注:有可能打包失敗是eclipse沒有集成的緣由,用我本身的eclipse能夠,同事那就有可能打包失敗,必定要注意這點。java
一、先在eclipse中集成或本身安裝解壓ant,總之有可用的ant就好。linux
二、照下圖新建extlib(好比servlet-api.jar是tomcat裏的包,ant打包須要可是引用不到的包都放入extlib文件夾),war(打成功的war包的位置)文件夾。web
三、編寫build.xml文件,內容以下: windows
<?xml version="1.0" encoding="UTF-8"?>
<project name="GBEMSSystemMGR" default="deploy" basedir="."><!--GBEMSSystemMGR是項目名稱-->
<!-- 判斷當前系統是windows仍是linux -->
<condition property="isWindows">
<os family="windows" />
</condition>
<condition property="isLinux">
<os family="unix" />
</condition>
<!-- 定義了一些變量 -->
<property name="resource.dir" location="${basedir}/resources" />
<property name="src.dir" location="${basedir}/src" />
<property name="web.dir" location="${basedir}/WebRoot" />
<property name="web.web-inf.dir" location="${web.dir}/WEB-INF" />
<property name="lib.dir" location="${web.web-inf.dir}/lib" />
<property name="classes.dir" location="${web.web-inf.dir}/classes" />
<property name="ext.dir" location="${basedir}/extlib" />
<!--定義一個時間戳-->
<tstamp prefix="backup">
<format property="time" pattern="yyyy-MM-dd.HH.mm.ss" />
</tstamp>
<!--path表示一個文件或路徑名列表-->
<path id="classpath">
<!--Fileset 數據類型定義了一組文件-->
<fileset dir="${lib.dir}">
<!--該文件夾下全部以.jar結尾的文件-->
<include name="*.jar" />
</fileset>
<!--Fileset 數據類型定義了一組文件-->
<fileset dir="${ext.dir}">
<!--該文件夾下全部以.jar結尾的文件-->
<include name="*.jar" />
</fileset>
</path>
<property name="war.file.path" location="${basedir}/war" />
<property name="war.file.name" value="GBEMSSystemMGR.war" />
<!-- =================================
target: deploy
================================= -->
<target name="deploy" depends="clean-classes-dir,copy-resource-to-classes,full-compile,war-app,deploy-under-windows">
<echo>now you can start tomcat.</echo>
</target>
<target name="deploy-under-windows" if="isWindows">
<!-- echo 往控制檯輸出一段話 -->
<echo>通常使用Eclipse集成的tomcat進行測試,省略</echo>
</target>
<!-- ================================= target: war-app ================================= --> <target name="war-app"> <echo>make War ..</echo> <mkdir dir="${war.file.path}" /> <!--將指定文件打成war包--> <war warfile="${war.file.path}/${war.file.name}" webxml="${web.web-inf.dir}/web.xml"> <lib dir="${lib.dir}" /> <classes dir="${classes.dir}" /> <fileset dir="${web.dir}"> </fileset> </war> <echo>War Success : ${war.file.path}/${war.file.name}</echo> </target> <!-- ================================= target: full-compile ================================= --> <target name="full-compile" description="description"> <echo>start compile.</echo> <!--編譯,其中refild標籤是引用以前定義的name爲classpath的path文件或路徑--> <javac encoding="utf-8" srcdir="${src.dir}" destdir="${classes.dir}" includeAntRuntime="false" debug="true" > <classpath refid="classpath" /> </javac> <javac encoding="utf-8" srcdir="${resource.dir}" destdir="${classes.dir}" includeAntRuntime="false" debug="true"> <classpath refid="classpath" /> </javac> <echo>full-compile successfully.</echo> </target> <!-- copy src/**/*.(xml|properties ...) to classes dir --> <target name="copy-resource-to-classes"> <!--將指定文件拷貝到指定目錄--> <copy todir="${classes.dir}"> <fileset dir="${src.dir}"> <!-- 表示除了以.java結尾的文件都包含--> <exclude name="**/*.java" /> </fileset> </copy> <copy todir="${classes.dir}"> <fileset dir="${resource.dir}"> <!-- 表示除了以.java結尾的文件都包含--> <exclude name="**/*.java" /> </fileset> </copy> </target> <!-- 刪除整個classes目錄 --> <target name="clean-classes-dir"> <delete dir="${classes.dir}" /> <echo>${classes.dir} deleted.</echo> </target> </project>