• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<?xml version="1.0" encoding="UTF-8"?>
2
3<!--
4   Copyright (c) 2009, 2017 Mountainminds GmbH & Co. KG and Contributors
5   All rights reserved. This program and the accompanying materials
6   are made available under the terms of the Eclipse Public License v1.0
7   which accompanies this distribution, and is available at
8   http://www.eclipse.org/legal/epl-v10.html
9
10   Contributors:
11      Marc R. Hoffmann - initial API and implementation
12-->
13
14<project name="Example Ant Build with JaCoCo Offline Instrumentation" default="rebuild" xmlns:jacoco="antlib:org.jacoco.ant">
15
16	<description>
17	  Example Ant build file that demonstrates how JaCoCo can be used with
18	  offline instrumentation. This requires preprocessing of the class files
19	  before the test is launched and adding the JaCoCo agent to the classpath.
20	</description>
21
22	<property name="src.dir" location="./src/main/java" />
23	<property name="result.dir" location="./target" />
24	<property name="result.classes.dir" location="${result.dir}/classes" />
25	<property name="result.classes.instr.dir" location="${result.dir}/classes-instr" />
26	<property name="result.report.dir" location="${result.dir}/site/jacoco" />
27	<property name="result.exec.file" location="${result.dir}/jacoco.exec" />
28
29	<!-- Step 1: Import JaCoCo Ant tasks -->
30	<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
31		<classpath path="../../../lib/jacocoant.jar" />
32	</taskdef>
33
34	<target name="clean">
35		<delete dir="${result.dir}" />
36	</target>
37
38	<target name="compile">
39		<mkdir dir="${result.classes.dir}" />
40		<javac srcdir="${src.dir}" destdir="${result.classes.dir}" debug="true" includeantruntime="false" />
41	</target>
42
43	<target name="instrument" depends="compile">
44		<!-- Step 2: Instrument class files -->
45		<jacoco:instrument destdir="${result.classes.instr.dir}">
46			<fileset dir="${result.classes.dir}" />
47		</jacoco:instrument>
48	</target>
49
50
51	<target name="test" depends="instrument">
52		<!-- Step 3: Run tests with instrumented classes -->
53		<java classname="org.jacoco.examples.parser.Main" fork="true">
54			<!-- jacocoagent.jar must be on the classpath -->
55			<classpath>
56				<pathelement path="../../../lib/jacocoagent.jar"/>
57				<pathelement path="${result.classes.instr.dir}" />
58			</classpath>
59			<!-- Agent is configured with system properties -->
60			<sysproperty key="jacoco-agent.destfile" file="${result.exec.file}"/>
61			<arg value="2 * 3 + 4"/>
62			<arg value="2 + 3 * 4"/>
63			<arg value="(2 + 3) * 4"/>
64			<arg value="2 * 2 * 2 * 2"/>
65			<arg value="1 + 2 + 3 + 4"/>
66			<arg value="2 * 3 + 2 * 5"/>
67		</java>
68	</target>
69
70	<target name="report" depends="test">
71		<!-- Step 4: Create coverage report -->
72		<jacoco:report>
73
74			<!-- This task needs the collected execution data and ... -->
75			<executiondata>
76				<file file="${result.exec.file}" />
77			</executiondata>
78
79			<!-- the class files and optional source files ... -->
80			<structure name="JaCoCo Ant Example">
81				<classfiles>
82					<fileset dir="${result.classes.dir}" />
83				</classfiles>
84				<sourcefiles encoding="UTF-8">
85					<fileset dir="${src.dir}" />
86				</sourcefiles>
87			</structure>
88
89			<!-- to produce reports in different formats. -->
90			<html destdir="${result.report.dir}" />
91			<csv destfile="${result.report.dir}/report.csv" />
92			<xml destfile="${result.report.dir}/report.xml" />
93		</jacoco:report>
94	</target>
95
96	<target name="rebuild" depends="clean,compile,instrument,test,report" />
97
98</project>