在java開發中,ant為我們提供了一個非常強大的項目構建功能。日常開發中,最常用的功能是:編譯、打包、運行junit testcase、檢查編碼規范。在此給出這些最常用功能的一個ant模板文件,與各位共享。
<?xml version="1.0" encoding="gb2312"?>
<project name="Test Project" default="all" basedir=".">
<property name="src.code" value="src"/>
<property name="src.junit" value="junit"/>
<property name="lib.dir" value="lib"/>
<property name="lib.jar" value="TestClass.jar"/>
<!-- checkstyle configuration -->
<property name="checkstyle.config" value="checkstyle.xml"/>
<taskdef resource="checkstyletask.properties"
classpath="${lib.dir}/checkstyle-all-3.1.jar"/>
<!-- 輸出文檔 -->
<property name="doc.dir" value="doc"/>
<property name="doc.api" value="${doc.dir}/api"/>
<property name="javadoc.package" value="com.*"/>
<!-- 輸出二進制文件 -->
<property name="dist.root" value="dist"/>
<property name="dist.proj" value="${dist.root}/proj"/>
<property name="dist.classes" value="${dist.proj}/classes"/>
<property name="dist.lib" value="${dist.proj}/lib"/>
<property name="dist.junit" value="${dist.root}/junit"/>
<!-- classpath -->
<path id="classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar"/>
</fileset>
</path>
<path id="proj.libs">
<fileset dir="${dist.lib}">
<include name="**/*.jar"/>
</fileset>
</path>
<target name="init">
<mkdir dir="${doc.dir}"/>
<mkdir dir="${dist.root}"/>
<mkdir dir="${dist.proj}"/>
<mkdir dir="${dist.lib}"/>
<tstamp/>
<echo message="${TSTAMP}"></echo>
</target>
<target name="all" depends="compilesrc, javadoc, checkstyle"/>
<!-- 編譯源文件 -->
<target name="compilesrc" depends="init">
<mkdir dir="${dist.classes}"/>
<javac destdir="${dist.classes}" deprecation="on">
<src path="${src.code}"/>
<classpath refid="classpath"/>
</javac>
<jar jarfile="${dist.lib}/${lib.jar}" basedir="${dist.classes}">
<include name="**/*.class"/>
</jar>
</target>
<!-- 產生javadoc -->
<target name="javadoc" depends="init">
<mkdir dir="${doc.api}"/>
<javadoc packagenames="${javadoc.package}" sourcepath="${src.code}"
private="yes" defaultexcludes="yes" destdir="${doc.dir}/api">
<classpath refid="classpath"/>
</javadoc>
</target>
<!-- 編譯Junit文件 -->
<target name="compilejunit" depends="compilesrc">
<mkdir dir="${dist.junit}"/>
<javac destdir="${dist.junit}" deprecation="on">
<src path="${src.junit}"/>
<classpath refid="classpath"/>
<classpath refid="proj.libs"/>
</javac>
</target>
<!-- 運行checkstyle檢查代碼規范 -->
<target name="checkstyle" depends="init">
<checkstyle config="${checkstyle.config}">
<fileset dir="${src.code}" includes="**/*.java"/>
<formatter type="plain"/>
<formatter type="xml" toFile="${dist.root}/checkstyle_errors.xml"/>
</checkstyle>
</target>
<!-- 運行junit -->
<target name="junit" depends="compilejunit">
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<path refid="classpath"/>
<pathelement location="${dist.junit}"/>
</classpath>
<formatter type="plain"/>
<!-- test name="com.TestClassTest" haltonfailure="no" outfile="result"/ -->
<batchtest todir="${dist.junit}">
<fileset dir="${dist.junit}" includes="**/Test*.class" />
</batchtest>
</junit>
</target>
<!-- 清除產生的類、junit相關類、文檔 -->
<target name="clean">
<delete dir="${dist.classes}"/>
<delete dir="${dist.junit}"/>
<delete dir="${doc.api}"/>
</target>
<!-- 清除所有輸出結果 -->
<target name="cleanall" depends="clean">
<delete dir="${doc.dir}"/>
<delete dir="${dist.root}"/>
</target>
</project>