結合 Ant / Maven


繼續 簡介 Cobertura 的說明,直接下 Cobertura 指令太過麻煩,可以使用Ant設定好相關資訊後,後續就交給Ant來處理,相關設定可以參考Cobertura官網的 Ant Task Reference

例如,在簡介 Cobertura 的範例專案結構下,以下是個參考的範本,重要設定的部份以粗體表示:
<?xml version="1.0" encoding="UTF-8"?>
<project name="example" default="reports">
<property name="src.dir" value="src" />
<property name="bin.dir" value="bin" />
<property name="instrumented.dir" value="instrumented" />
<property name="reports.dir" value="reports" />
<property name="reports.junit.dir" value="\${reports.dir}/junit" />
<property name="reports.coverage.dir" value="\${reports.dir}/coverage" />
<property name="cobertura.dir" value="cobertura" />

<property name="junit.jar" value="lib/junit-4.8.2.jar" />
<property name="driver.jar"
value="lib/mysql-connector-java-5.1.7-bin.jar" />

<property name="cobertura.metafile"
value="\${reports.coverage.dir}/cobertura.ser" />

<path id="cobertura.classpath">
<fileset dir="\${cobertura.dir}">
<include name="cobertura.jar" />
<include name="lib/**/*.jar" />
</fileset>
</path>

<taskdef classpathref="cobertura.classpath"
resource="tasks.properties" />

<target name="clean">
<delete dir="\${bin.dir}" />
<delete dir="\${instrumented.dir}" />
<delete dir="\${reports.dir}" />
</target>

<target name="prepare" depends="clean">
<mkdir dir="\${bin.dir}" />
<mkdir dir="\${reports.junit.dir}" />
<mkdir dir="\${reports.coverage.dir}" />
</target>

<target name="compile" depends="prepare">
<!-- <javac> 預設不記錄行數,要設定 debug 為 true -->
<javac srcdir="\${src.dir}" destdir="\${bin.dir}" debug="true">
<classpath>
<pathelement location="\${junit.jar}" />
<pathelement location="\${driver.jar}" />
</classpath>
</javac>
</target>

<target name="instrument" depends="compile">
<cobertura-instrument datafile="\${cobertura.metafile}"
todir="\${instrumented.dir}">
<ignore regex="org.apache.log4j.*" />
<fileset dir="\${bin.dir}">
<include name="**/*.class" />
<exclude name="test/**/*.class" />
</fileset>
</cobertura-instrument>
</target>

<target name="test" depends="instrument">
<junit fork="yes" dir="\${basedir}" printsummary="yes">
<sysproperty key="net.sourceforge.cobertura.datafile"
value="\${cobertura.metafile}" />

<!-- 注意Classpath的順序 -->
<classpath location="\${instrumented.dir}"/>
<classpath location="\${bin.dir}"/>
<classpath location="\${junit.jar}"/>
<classpath location="\${driver.jar}"/>
<classpath refid="cobertura.classpath" />

<formatter type="xml" />
<batchtest todir="\${reports.junit.dir}">
<fileset dir="\${src.dir}" includes="**/*Test.java" />
</batchtest>
</junit>
</target>

<target name="reports" depends="test">
<junitreport todir="\${reports.junit.dir}">
<fileset dir="\${reports.junit.dir}">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="\${reports.junit.dir}/html" />
</junitreport>
<cobertura-report datafile="\${cobertura.metafile}"
format="html" destdir="\${reports.coverage.dir}">
<fileset dir="\${src.dir}">
<include name="**/*.java" />
<exclude name="test/**/*.java" />
</fileset>
</cobertura-report>
</target>
</project>

至於Maven 的結合,可以參考
http://cobertura.sourceforge.net/maven.html 的內容,最簡單的情況下,要產生測試覆蓋率報告,只要下 cobertura:cobertura