Ant腳本文件build文件編寫示例
build.xml文件編寫示例
1、設置目錄常量
例:<property name="work.home" value="work" />
2、設置classpath
例:
<!-- 設置運行環境 -->
<path id="classpath">
<pathelement location="${servlet.jar}"/>
<pathelement location="${webapp.libs}"/>
<fileset dir="${webapp.libs}">
<include name="**/*.jar"/>
<exclude name="servlet.jar"/>
</fileset>
<pathelement path="${compile.classpath}"/>
</path>
3、設置初始化內容
例:
<target name="init">
<!-- Do we need to copy dependent libraries? -->
<available property="copy.libs" file="${webapp.libs}" />
</target>
4、創建需要的文件目錄
例:
<target name="prepare" depends="init"
description="Prepare target directory">
<mkdir dir="${webapp.target}" />
</target>
5、設置運行類庫lib,將需要的運行類庫拷貝到指定目錄
例:
<target name="libs" depends="prepare" if="copy.libs"
description="Copy dependent libraries">
<mkdir dir="${webapp.target}/WEB-INF/lib" />
<copy todir="${webapp.target}/WEB-INF/lib">
<fileset dir="${webapp.libs}">
<include name="*.jar"/>
<exclude name="servlet.jar"/>
</fileset>
</copy>
</target>
6、復制運行此系統所需的文件
例:
<target name="static" depends="prepare,libs"
description="Copy static files">
<copy todir="${webapp.target}">
<fileset dir="${webapp.web}">
<exclude name="**/CVS/*.*"/>
<exclude name="Tomcat/**"/>
<exclude name="WEB-INF/lib/**"/>
<exclude name="WEB-INF/classes/**"/>
<exclude name="WEB-INF/log/**"/>
</fileset>
</copy>
</target>
7、編譯
例:
<target name="compile" depends="static" if="webapp.compile"
description="Compile Java sources">
<!-- 編譯源碼 -->
<javac srcdir="${webapp.src}"
destdir="${webapp.target}/WEB-INF/classes"
debug="${compile.debug}"
deprecation="${compile.deprecation}"
optimize="${compile.optimize}">
<classpath refid="classpath"/>
</javac>
<!-- 將編譯得到的class文件拷貝到指定地方 -->
<copy todir="${webapp.target}/WEB-INF/classes">
<fileset dir="${webapp.src}">
<include name="**/*.properties" />
<include name="**/*.xml" />
<exclude name="**/CVS/*.*"/>
</fileset>
</copy>
</target>
8、測試并生成測試報告
例:
<target name="test" depends="compile,compile.test">
<property name="tests" value="Test*"/>
<!-- 創建文件夾 -->
<mkdir dir="${work.home}/test"/>
<mkdir dir="${work.home}/test/xml"/>
<mkdir dir="${work.home}/test/report"/>
<!-- 測試 -->
<junit dir="${work.home}/test" printsummary="yes" haltonerror="yes" fork="yes">
<formatter type="plain" usefile="false" />
<formatter type="xml" />
<batchtest todir="${work.home}/test/xml">
<fileset dir="${webapp.test}">
<include name="**/${tests}.java" />
<exclude name="**/Test*All*.java"/>
</fileset>
</batchtest>
<classpath>
<pathelement location="${webapp.target}/WEB-INF/classes" />
<pathelement location="${work.home}/${webapp.test}"/>
</classpath>
<classpath refid="classpath" />
</junit>
<!-- 生成測試報告 -->
<junitreport todir="${work.home}/test/xml">
<fileset dir="${work.home}/test/xml">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${work.home}/test/report"/>
</junitreport>
<fail if="test.failed"/>
</target>
9、生成發布程序文件
例:
<target name="dist" depends="compile" description="Create web application archive">
<!-- 將web工程以war形式發布 --?
<jar basedir="${work.home}/${webapp.name}"
destfile="${webapp.dist}/${webapp.war}"/>
</target>
說明:basedir為要發布的程序文件位置
destfile為發布程序文件
10、清理舊的測試信息
例:
<target name="clean" description="Clean build and distribution directories">
<delete dir="${work.home}" />
<delete dir="${webapp.dist}" />
</target>
說明:可以刪除目錄也可以刪除文件
delete中為dir時刪除指定目錄
delete中為file是刪除指定文件
依賴關系基本上從下往上的,也就是說只有上面的成立了,下面的才能正常
運行下去。其中的1、2、3是不需要依賴任何一個的
在build.xml文件中,出現兩個或多個同名的屬性,在使用該屬性時將以第一
個為準。
目錄結構規范:
build 類和其他內容的臨時存放位置
dist 發布目錄
docs 顯示格式的文檔文件
etc 示例文件
lib 項目依賴庫,通常是第三方的.jar文件
src java源代碼的根目錄,其下級是包的目錄結構
src/xdocs xml格式的文檔,可以通過構建過程轉換的顯示格式
src/META-INF jar文件的元數據
web web內容(.html、.jpg、.jsp)的根目錄
web/WEB-INF web配置信息,例如web.xml
build和dist目錄不應包含ant無法生成的內容,這樣clean可以只通過刪除它
們來進行清理。