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