完整的Ant build file範例
基本架構
<project name="ProjectName" default="all">
<property name="src" location="src"/>
<property name="dist" location="bin"/>
<!-- This is comment. -->
<target name="all" depends="compile"/>
<target name="compile">
<javac srcdir="${src}" destdir="${dist}" encoding="UTF-8">
<classpath>
<pathelement location="lib/javax.mail.jar" />
<pathelement location="lib/jsoup-1.7.3.jar"/>
</classpath>
</javac>
</target>
</project>
第一行指定此project的名字,還有此build script一開始要執行那一個target(之於makefile的tag)。
第二行和第三行定義變數(之於makefile的變數定義),用${src}可以取得src的值。
第四行的註解就跟html語法一樣,從<!-- 到 -->為止。
第五行和第七行的target,就是標記所有執行的task。第五行的task名稱為all,depends所指定的就是執行這個task之前需要再執行哪些task,在這裡就是會先執行compile這個task。
第七行到第14行<target>就指定compile這個task要做哪些事。<javac>的屬性告訴java的compiler,原始碼所在的目錄-srcdir、所產生的class檔案要放置的目錄-destdir、程式編碼指定UTF-8。
第九行到第12行<classpath>可以指定其他外部library的路徑,由其子元素<pathelement location="">指定library的檔名及路徑。
JUnit test task
<target name="test" depends="compile">
<!--Compile the test class files-->
<mkdir dir="${testclass}"/>
<javac srcdir="${t_src}" destdir="${testclass}">
<classpath>
<pathelement location="${dist}"/>
<pathelement location="lib/junit-4.11.jar"/>
</classpath>
</javac>
<!--Test progress and the test result would be
stored at the directory ${testresult}.-->
<mkdir dir="${testresult}"/>
<junit haltonfailure="no" printsummary="on">
<classpath>
<pathelement location="${dist}"/>
<pathelement location="lib/junit-4.11.jar"/>
<pathelement location="lib/jsoup-1.7.3.jar"/>
<pathelement location="${testclass}"/>
</classpath>
<formatter type="brief" usefile="false"/>
<formatter type="xml"/>
<batchtest todir="${testresult}">
<fileset dir="${testclass}" includes="*Test.class"/>
</batchtest>
</junit>
</target>
JUnit Task for Ant
第四行到第九行與compile類似,主要是編譯JUnit會用到的class,必須包含原始碼所編譯出來的.class檔。
第三行的<mkdir>可以建立指定名稱的目錄。
第13行到第24行為JUnit的設定。第13行要求JUnit在進行測試時,就算途中出現錯誤也無法停止,並將結果輸出。
第15到18行必須將所有需要用到的class檔還有library都包含進來。
第20行為指定輸出型態,brief指定只有在失敗時才輸出,usefile則是要不要將這些結果輸出成檔案。
第21行指定將測試結果輸出成xml檔案。
第22行的<batchset>指定需要使用的test class,其中todir屬性指定將結果輸出到該目錄。
第23行的<fileset>就幫助指定這些test class在哪裡,以及有哪些class檔。
Jar
<target name="jar" depends="compile">
<mkdir dir="${build}"/>
<jar jarfile="${build}/ProjectName.jar" basedir="${dist}"/>
</target>
這是建立jar file的task。可以看到先建立目錄,再指定輸出的jar檔名以及來源目錄。
其他task
<target name="clean">
<delete dir="${dist}"/>
<delete dir="${testclass}"/>
<delete dir="${build}"/>
</target>
這是一個清除可以用來清除前次build的task。<delete>指定的目錄無論是否為空都會被清除掉。
如果要讓每一次的build都可以完整跑完整個流程,則可以將all task寫成
<target name="all" depends="clean, compile, test, jar"/>
要注意task的執行順序會有先後。
參考資料
Cruise Control(一)-介紹Continuous Integration
Cruise Control(二)-架設Cruise Control及概觀
Cruise Control(三)-config.xml設定
沒有留言:
張貼留言