看標題就知道,這個是開發一個Java應用的利器組合,使用Ant完成工程的構建(Build),使用JUnit完成單元測試,使用Cobertura完成代碼覆蓋測試,也可以輔助查找性能瓶頸和一些類型的BUG,下面是一個完整的build.xml範例,可以完全拿來用,不需任何修改,只要你的目錄和這裡的目錄一致(應該也是很通用的):
下載下面的build.xml文件
文件內容:
<project default="all">
<!– =================================================================== –>
<!– Definitions –>
<!– =================================================================== –>
<property name="base-dir" location="." />
<property name="lib-dir" location="${base-dir}/lib" />
<property name="src-dir" location="${base-dir}/src" />
<property name="testcase-dir" location="${base-dir}/test" />
<property name="out-dir" location="${base-dir}/classes" />
<property name="report-dir" location="${base-dir}/report" />
<property name="junit-dir" location="${report-dir}/junit" />
<property name="junit-xml-dir" location="${junit-dir}/xml" />
<property name="junit-html-dir" location="${junit-dir}/html" />
<property name="cobertura-dir" location="${report-dir}/cobertura" />
<property name="cobertura-xml-dir" location="${cobertura-dir}/xml" />
<property name="cobertura-html-dir" location="${cobertura-dir}/html" />
<property name="instrumented-dir" location="${report-dir}/instrumented" />
<property name="instrument-file" location="${instrumented-dir}/cobertura.ser" />
<property name="cobertura.dir" value="${instrumented-dir}" />
<path id="classpath.all">
<pathelement location="${out-dir}" />
<fileset id="alljars" dir="${lib-dir}">
<include name="**/*.jar" />
</fileset>
</path>
<!– include the cobertura building jars –>
<path id="cobertura.classpath">
<path refid="classpath.all" />
</path>
<!– define the cobertura property file –>
<taskdef classpathref="cobertura.classpath" resource="tasks.properties"/>
<!– =================================================================== –>
<!– Project Targets –>
<!– =================================================================== –>
<target name="compile" depends="init">
<javac srcdir="${src-dir}" destdir="${out-dir}">
<classpath refid="classpath.all" />
</javac>
</target>
<!– =================================================================== –>
<!– Unit Test Targets –>
<!– =================================================================== –>
<!– - - - - - - - - - - - - - - - - -
target: init
initialize the build env
- - - - - - - - - - - - - - - - - –>
<target name="init-test" description="initialize the test env.">
<!– create the output folder –>
<mkdir dir="${junit-dir}" />
<mkdir dir="${junit-xml-dir}" />
<mkdir dir="${junit-html-dir}" />
</target>
<!– - - - - - - - - - - - - - - - - -
target: compile-test
compile the test cases
- - - - - - - - - - - - - - - - - –>
<target name="compile-test" depends="compile">
<javac srcdir="${testcase-dir}" destdir="${out-dir}">
<classpath refid="classpath.all" />
</javac>
</target>
<!– =================================
target: test
run the unit test
================================= –>
<target name="test" depends="init-test">
<junit fork="yes" printsummary="on" maxmemory="100m">
<sysproperty key="net.sourceforge.cobertura.datafile"
file="${instrument-file}" />
<classpath location="${instrumented-dir}" />
<classpath refid="cobertura.classpath" />
<formatter type="xml" />
<batchtest todir="${junit-xml-dir}">
<fileset dir="${out-dir}">
<include name="**/Test*.class" />
<exclude name="**/*$*.class" />
</fileset>
</batchtest>
</junit>
<junitreport todir="${junit-xml-dir}">
<fileset dir="${junit-xml-dir}">
<include name="TEST-*.xml" />
<!– - - - - - - - - - - - - - - - - -
target: init
initialize the build env. for code coverage
- - - - - - - - - - - - - - - - - –>
<target name="init-coverage" description="initialize the build env.">
<!– create the output folder –>
<mkdir dir="${instrumented-dir}" />
<mkdir dir="${cobertura-dir}" />
<mkdir dir="${cobertura-xml-dir}" />
<mkdir dir="${cobertura-html-dir}" />
</target>
<target name="compile-coverage" depends="init">
<javac srcdir="${src-dir}:${testcase-dir}" destdir="${out-dir}" debug="true" >
<classpath refid="cobertura.classpath" />
</javac>
</target>
<!– =================================
target: instrument
Instrument into the class files, but
exclude test classes
================================= –>
<target name="instrument" depends="init-coverage,compile-coverage" description="instrument into the class files">
<!–
Instrument the application classes, writing the
instrumented classes into ${instrumented.dir}.
–>
<cobertura-instrument todir="${instrumented-dir}" datafile="${instrument-file}">
<!–
The following line causes instrument to ignore any
source line containing a reference to log4j, for the
purposes of coverage reporting.
–>
<ignore regex="org.apache.log4j.*" />
<fileset dir="${out-dir}">
<!–
Instrument all the application classes, but
don』t instrument the test classes.
–>
<include name="**/*.class" />
<exclude name="**/Test*.class" />
</fileset>
</cobertura-instrument>
</target>
<!– =================================
target: coverage-check
check the code coverage by given rates.
================================= –>
<target name="coverage-check" description="check the code coverage by given rates">
<target name="full" depends="clean, coverage" />
</project>
[火星人 ] Java應用利器組合:Ant JUnit Cobertura已經有1067次圍觀