在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>