基于apache ant工程的Jacoco覆盖率工具使用之ant 脚本编写

基于apache ant工程的Jacoco覆盖率工具使用之ant 脚本编写


承接上一篇"Jacoco覆盖率工具使用"内容点击打开链接


Web项目结构如图所示:



废话少说,直接上源码:

build.properties:

src.dir=${basedir}/src
test.src.dir=${basedir}/test
build.dir=${basedir}/build
build.classes=${build.dir}/build
build.test.dir=${build.dir}/test
build.test.classes=${build.test.dir}/classes
build.test.report=${build.dir}/report
build.test.report.coverage=${build.test.report}/coverage
junit.lib.dir=devlib


build-jacoco.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- ====================================================================== 
	project:icity-heritrix description : junit test clouder date: 2014-04-10 
	author:wangmuming ====================================================================== -->
<project name="build" default="all-report" basedir="."
	xmlns:jacoco="antlib:org.jacoco.ant">

	<property name="lib" value="${basedir}/WebRoot/WEB-INF/lib" />

	<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
		<classpath path="${basedir}/devlib/jacocoant.jar">
		</classpath>
	</taskdef>

	<property file="build.properties">
	</property>

	<path id="compile-path">
		<pathelement location="${build.classes}" />
		<fileset dir="${junit.lib.dir}">
			<include name="**/*.jar" />
		</fileset>
		<fileset dir="${lib}">
			<include name="**/*.jar" />
		</fileset>
	</path>

	<path id="run-test-path">
		<path refid="compile-path" />
		<pathelement location="${build.test.classes}" />
	</path>


	<target name="init" depends="clean">
		<mkdir dir="${build.dir}" />
		<mkdir dir="${build.classes}" />
		<mkdir dir="${build.test.dir}" />
		<mkdir dir="${build.test.classes}" />
		<mkdir dir="${build.test.report}" />
		<mkdir dir="${build.test.report.coverage}" />
	</target>

	<target name="clean">
		<delete  includeEmptyDirs="true" verbose="true" failonerror="false">
					 <fileset dir="${build.dir}"/>    
		</delete>
		<!--
		<delete verbose="true" failonerror="false">
			<fileset dir="${build.dir}">
				<include name="**/*.xml" />
				<include name="**/*.properties" />
			</fileset>
		</delete>
		-->
	</target>


	<target name="src_compile" depends="init">
		<javac srcdir="${src.dir}" destdir="${build.classes}"
			failonerror="true" verbose="true" debug="on" optimize="off" >
			<compilerarg line="-encoding UTF-8 " />
			<classpath refid="compile-path" />
		</javac>
		<copy todir="${build.classes}">
			<fileset dir="${src.dir}">
				<include name="**/*.xml" />
				<include name="**/*.properties" />
			</fileset>
		</copy>
	</target>

	<target name="test_compile" depends="src_compile">
		<javac srcdir="${test.src.dir}" destdir="${build.test.classes}"
			failonerror="true" verbose="true" debug="on" optimize="off" >
			<compilerarg line="-encoding UTF-8 " />
			<classpath refid="compile-path">
			</classpath>
		</javac>
	</target>

	<target name="coverage">
		<jacoco:coverage>
			<junit haltonfailure="false" fork="true" printsummary="true">
				<classpath refid="run-test-path">
				</classpath>
				<formatter type="xml" />
				<batchtest todir="${build.test.report}">
					<fileset dir="${build.test.classes}" includes="**/*Test*.class">
					</fileset>
				</batchtest>
			</junit>
		</jacoco:coverage>
	</target>

	<target name="run-test" depends="test_compile">
		<junit haltonfailure="false" fork="true" printsummary="true">
			<classpath refid="run-test-path">
			</classpath>
			<formatter type="xml" />
			<batchtest todir="${build.test.report}">
				<fileset dir="${build.test.classes}" includes="**/*Test*.class">
				</fileset>
			</batchtest>
		</junit>
	</target>

	<target name="test-report" depends="run-test">
		<junitreport todir="${build.test.report}">
			<fileset dir="${build.test.report}">
				<include name="TEST-*.xml" />
			</fileset>
			<report format="frames" todir="${build.test.report}/html" />
		</junitreport>
	</target>


	<target name="coverreport" depends="coverage">
		<jacoco:report>
			<executiondata>
				<file file="${basedir}/jacoco.exec" />
			</executiondata>
			<structure name="MeltingPot unit test coverage rate report.">
				<classfiles>
					<fileset dir="${build.classes}" />
				</classfiles>
				<sourcefiles encoding="UTF-8">
					<fileset dir="${src.dir}" />
				</sourcefiles>
			</structure>

			<html footer="Onecloud MeltingPot." destdir="${build.test.report.coverage}" />
			<csv destfile="${build.test.report.coverage}/coverage-report.csv" />
			<xml destfile="${build.test.report.coverage}/coverage-report.xml" />
		</jacoco:report>
	</target>

	<target name="all-report" depends="test-report">
		<echo>generate unit test report and coverage rate report.</echo>
		<antcall target="coverreport" ></antcall>
	</target>
</project> 

参考:http://www.eclemma.org/jacoco/trunk/doc/ant.html  ant  task

在进行javac时,需要设置

debug="on"

属性,否则代码覆盖率展示不出来。



你可能感兴趣的:(ant,持续集成,Jenkins,CI)