blob: 18f36dcf853d56303faa9bc97337322c3174db2f [file] [log] [blame]
Marc R. Hoffmannf7d17522009-08-07 11:21:23 +00001<?xml version="1.0" encoding="ISO-8859-1" ?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
4<head>
5 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
6 <link rel="stylesheet" href=".resources/doc.css" charset="ISO-8859-1" type="text/css" />
7 <title>JaCoCo - Ant Tasks</title>
8</head>
9<body>
10
11<div class="breadcrumb">
12 <a href="../index.html" class="el_session">JaCoCo</a> &gt;
13 <a href="index.html" class="el_group">Documentation</a> &gt;
14 <span class="el_source">Ant Tasks</span>
15</div>
16
17<h1>Ant Tasks</h1>
18
19<p>
20 JaCoCo comes with Ant tasks to launch Java programs with execution recording
Marc R. Hoffmannb8a77462009-08-21 14:53:50 +000021 and for creating coverage reports from the recorded data. The JaCoCo Ant tasks
22 require
23</p>
24
25<ul>
26 <li>Ant 1.7.0 or higher and</li>
27 <li>Java 1.5 or higher (for both, the Ant runner and the test executor).</li>
28</ul>
29
30
Marc R. Hoffmann57f9ab42009-08-24 10:52:42 +000031<p>All tasks are defined in <code>jacocoant.jar</code> (which is part of the
32 distribution) and can be included in your Ant scripts with the usual
33 <code>taskdef</code> declaration:
Marc R. Hoffmannf7d17522009-08-07 11:21:23 +000034</p>
35
36<pre class="source">
37<span class="nr"> 1</span>&lt;project name="Example" xmlns:jacoco="antlib:org.jacoco.ant"&gt;
38<span class="nr"> 2</span>
39<span class="nr"> 3</span> &lt;taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml"&gt;
40<span class="nr"> 4</span> &lt;classpath&gt;
Marc R. Hoffmanndb930772009-08-20 17:17:37 +000041<span class="nr"> 5</span> &lt;pathelement location="<i>path_to_jacoco</i>/lib/jacocoant.jar"/&gt;
Marc R. Hoffmannf7d17522009-08-07 11:21:23 +000042<span class="nr"> 6</span> &lt;/classpath&gt;
43<span class="nr"> 7</span> &lt;/taskdef&gt;
44<span class="nr"> 8</span>
45<span class="nr"> 9</span> ...
46<span class="nr"> 10</span>
47<span class="nr"> 11</span>&lt;/project&gt;
48</pre>
49
50<p>
Marc R. Hoffmanndb3e7d02009-08-12 10:57:39 +000051 Alternatively you might also place the <code>jacocoant.jar</code> in your
52 Ant <code><i>ANT_HOME</i>/lib</code> folder. If you use the name space URI
53 <code>antlib:org.jacoco.ant</code> for JaCoCo tasks Ant will find them
54 automatically without the <code>taskdef</code> declaration above.
55</p>
56
57<p>
Marc R. Hoffmannf7d17522009-08-07 11:21:23 +000058 Declaring a XML namespace for JaCoCo tasks is optional but always recommended
59 if you mix tasks from different libraries. All subsequent examples use the
60 <code>jacoco</code> prefix declared above.
61</p>
62
63
64<h2>Task <code>coverage</code></h2>
65
66<p>
67 The standard Ant tasks to launch Java programs are <code>java</code> and
68 <code>junit</code>. To add code coverage recording to these tasks they can
69 simply be wrapped with the <code>coverage</code> task as shown in the
70 following examples:
71</p>
72
73<pre class="source">
74<span class="nr"> 1</span>&lt;jacoco:coverage>
75<span class="nr"> 2</span> &lt;java classname="org.jacoco.examples.HelloJaCoCo" fork="true"&gt;
76<span class="nr"> 3</span> &lt;classpath&gt;
77<span class="nr"> 4</span> &lt;pathelement location="./bin"/&gt;
78<span class="nr"> 5</span> &lt;/classpath&gt;
79<span class="nr"> 6</span> &lt;/java&gt;
80<span class="nr"> 7</span>&lt;/jacoco:coverage&gt;
81<span class="nr"> 8</span>
82<span class="nr"> 9</span>
83<span class="nr"> 10</span>&lt;jacoco:coverage>
84<span class="nr"> 11</span> &lt;junit fork="true" forkmode="once"&gt;
85<span class="nr"> 12</span> &lt;test name="org.jacoco.examples.HelloJaCoCoTest"/&gt;
86<span class="nr"> 13</span> &lt;classpath&gt;
87<span class="nr"> 14</span> &lt;pathelement location="./bin"/&gt;
88<span class="nr"> 15</span> &lt;/classpath&gt;
89<span class="nr"> 16</span> &lt;/junit&gt;
90<span class="nr"> 17</span>&lt;/jacoco:coverage>
91</pre>
92
93<p>
94 As a result coverage information is collected during execution and written
95 to a file when the process terminates. Note the <code>fork</code> attribute in
96 wrapped <code>java</code> task.
97</p>
98
99<p class="hint">
100 The nested task always has to declare <code>fork="true"</code>, otherwise the
101 <code>coverage</code> task can't record coverage information and will fail.
102 In addition the <code>junit</code> task should declare
103 <code>forkmode="once"</code> to avoid starting a new JVM for every single test
104 case and decreasing execution performance dramatically (unless this is
105 required by the nature of the test cases).
106</p>
107
108<p>
109 The coverage task must wrap exactly one task. While it typically works without
110 any configuration, the behavior can be adjusted with some optional attributes:
111</p>
112
113<table class="coverage">
114 <thead>
115 <tr>
116 <td>Attribute</td>
117 <td>Description</td>
118 <td>Default</td>
119 </tr>
120 </thead>
121 <tbody>
122 <tr>
123 <td><code>file</code></td>
124 <td>Path to the output file for execution data.</td>
125 <td><code>jacoco.exec</code></td>
126 </tr>
127 <tr>
128 <td><code>merge</code></td>
129 <td>If set to <code>true</code> and the execution data file already
130 exists, coverage data is merged to the existing file. If set to
131 <code>false</code>, an existing execution data file will be replaced.
132 </td>
133 <td><code>true</code></td>
134 </tr>
135 <tr>
136 <td><code>exclclassloader</code></td>
Marc R. Hoffmann2a6b5bf2009-08-11 12:54:36 +0000137 <td>A list of class loader names, that should be excluded from execution
138 analysis. The list entries are separated by a vertical bar
139 (<code>|</code>) and may use wildcard characters (<code>*</code> and
140 <code>?</code>). This option might be required in case of special
Marc R. Hoffmann57f9ab42009-08-24 10:52:42 +0000141 frameworks that conflict with JaCoCo code instrumentation, in
142 particular class loaders that do not have access to the Java runtime
143 classes.
Marc R. Hoffmannf7d17522009-08-07 11:21:23 +0000144 </td>
145 <td><code>sun.reflect.DelegatingClassLoader</code></td>
146 </tr>
147 </tbody>
148</table>
149
150
151<h2>Task <code>agent</code></h2>
152
Marc R. Hoffmannd519add2009-08-11 11:09:29 +0000153<p>
154 If the <code>coverage</code> task is not suitable for your launch target, you
155 might alternatively use the <code>agent</code> task to create the Java agent
156 parameter. The following example defines a Ant property with the name
157 <code>agentvmparam</code> that can be directly used as Java VM parameter:
158</p>
159
160<pre class="source">
161<span class="nr"> 1</span>&lt;jacoco:agent property="agentvmparam"/>
162</pre>
163
164<p>
165 This task has the same attributes as the <code>coverage</code> task plus an
166 additional property to specify the target property name:
167</p>
168
169<table class="coverage">
170 <thead>
171 <tr>
172 <td>Attribute</td>
173 <td>Description</td>
174 <td>Default</td>
175 </tr>
176 </thead>
177 <tbody>
178 <tr>
179 <td><code>property</code></td>
180 <td>Name of the Ant property to set.</td>
181 <td><i>none (required)</i></td>
182 </tr>
Marc R. Hoffmann2bb6fca2009-08-13 14:04:21 +0000183 <tr>
184 <td colspan="3"><i>All attributes of the <code>coverage</code> task.</i></td>
185 </tr>
Marc R. Hoffmannd519add2009-08-11 11:09:29 +0000186 </tbody>
187</table>
188
Marc R. Hoffmannf7d17522009-08-07 11:21:23 +0000189<h2>Task <code>report</code></h2>
190
Marc R. Hoffmann2bb6fca2009-08-13 14:04:21 +0000191<p>
192 Finally different reports can be created with the <code>report</code> task.
193 A report task declaration consists of different sections, two specify the
194 input data, additional ones specify the output formats:
195</p>
196
197<pre class="source">
198<span class="nr"> 1</span>&lt;jacoco:report&gt;
199<span class="nr"> 2</span>
200<span class="nr"> 3</span> &lt;executiondata&gt;
201<span class="nr"> 4</span> &lt;file file="jacoco.exec"/&gt;
202<span class="nr"> 5</span> &lt;/executiondata&gt;
203<span class="nr"> 6</span>
204<span class="nr"> 7</span> &lt;structure name="Example Project"&gt;
205<span class="nr"> 8</span> &lt;classfiles&gt;
206<span class="nr"> 9</span> &lt;fileset dir="bin"/&gt;
207<span class="nr"> 10</span> &lt;/classfiles&gt;
208<span class="nr"> 11</span> &lt;sourcefiles encoding="UTF-8"&gt;
209<span class="nr"> 12</span> &lt;fileset dir="src"/&gt;
210<span class="nr"> 13</span> &lt;/sourcefiles&gt;
211<span class="nr"> 14</span> &lt;/structure&gt;
212<span class="nr"> 15</span>
213<span class="nr"> 16</span> &lt;html destdir="report"/&gt;
214<span class="nr"> 17</span>
215<span class="nr"> 18</span>&lt;/jacoco:report&gt;
216</pre>
217
218<p>
219 As you can see from the example above the <code>report</code> task is based
220 on several nested elements:
221</p>
222
223<h3>Element <code>executiondata</code></h3>
224
225<p>
226 Within this element Ant resources and resource collections can be specified,
227 that represent JaCoCo execution data files. If more than one execution data
228 file is specified, execution data is combined. A particular piece of code is
229 considered as executed, when it is marked as executed in any of the input
230 files.
231</p>
232
233<h3>Element <code>structure</code></h3>
234
235<p>
236 This element defines structure of the report. It might contain the following
237 nested elements:
238</p>
239
240<ul>
241 <li><code>classfiles</code>: Container element for Ant resources and resource
242 collections that can specify Java class files, JAR files or directories
243 containing class files.</li>
244 <li><code>sourcefiles</code>: Optional container element for Ant resources and
245 resource collections that specify corresponding source files. The element
246 has an optional attribute <code>encoding</code> to specify the character
247 encoding of the source files. If no encoding is given, the platform default
248 is used. If source files are specified, some report formats include
249 highlighted versions of the source code.</li>
250</ul>
251
252<p>
253 The structure can be refined with a hierarchy of <code>group</code> elements.
254 This way the coverage report can reflect different modules of an software
255 project. For each group element the corresponding class and source files can
256 be specified separately. For example the build script of JaCoCo itself
257 contains the following declaration to separate the different bundles in the
258 report (see the <a href="../coverage/index.html">resulting report</a>):
259</p>
260
261<pre class="source">
262<span class="nr"> 1</span>&lt;structure name="JaCoCo"&gt;
263<span class="nr"> 2</span> &lt;group name="org.jacoco.core"&gt;
264<span class="nr"> 3</span> &lt;classfiles&gt;
265<span class="nr"> 4</span> &lt;path refid="bundle-org.jacoco.core"/&gt;
266<span class="nr"> 5</span> &lt;/classfiles&gt;
267<span class="nr"> 6</span> &lt;sourcefiles&gt;
268<span class="nr"> 7</span> &lt;fileset dir="${workspace.dir}/org.jacoco.core/src"/&gt;
269<span class="nr"> 8</span> &lt;/sourcefiles&gt;
270<span class="nr"> 9</span> &lt;/group&gt;
271<span class="nr"> 10</span> &lt;group name="org.jacoco.report"&gt;
272<span class="nr"> 11</span> &lt;classfiles&gt;
273<span class="nr"> 12</span> &lt;path refid="bundle-org.jacoco.report"/&gt;
274<span class="nr"> 13</span> &lt;/classfiles&gt;
275<span class="nr"> 14</span> &lt;sourcefiles&gt;
276<span class="nr"> 15</span> &lt;fileset dir="${workspace.dir}/org.jacoco.report/src"/&gt;
277<span class="nr"> 16</span> &lt;/sourcefiles&gt;
278<span class="nr"> 17</span> &lt;/group&gt;
279<span class="nr"> 18</span>
280<span class="nr"> 19</span> ...
281<span class="nr"> 20</span>
282<span class="nr"> 21</span>&lt;/structure&gt;
283</pre>
284
285<h3>Element <code>html</code></h3>
286
287<p>
288 Create a multi-page report in HTML format.
289</p>
290
291<table class="coverage">
292 <thead>
293 <tr>
294 <td>Attribute</td>
295 <td>Description</td>
296 <td>Default</td>
297 </tr>
298 </thead>
299 <tbody>
300 <tr>
301 <td><code>destdir</code></td>
302 <td>Directory to create the report in.</td>
303 <td><i>none (required)</i></td>
304 </tr>
Marc R. Hoffmann2ecbea52009-09-03 14:19:11 +0000305 <tr>
306 <td><code>footer</code></td>
307 <td>Footer text for each report page.</td>
308 <td><i>No footer</i></td>
309 </tr>
Marc R. Hoffmann2bb6fca2009-08-13 14:04:21 +0000310 </tbody>
311</table>
312
313<h3>Element <code>csv</code></h3>
314
315<p>
316 Create a multi-page report in CSV format.
317</p>
318
319<table class="coverage">
320 <thead>
321 <tr>
322 <td>Attribute</td>
323 <td>Description</td>
324 <td>Default</td>
325 </tr>
326 </thead>
327 <tbody>
328 <tr>
329 <td><code>destdir</code></td>
330 <td>Directory to create the report in.</td>
331 <td><i>none (required)</i></td>
332 </tr>
333 </tbody>
334</table>
335
Marc R. Hoffmannf7d17522009-08-07 11:21:23 +0000336
337<div class="footer">
338 <div class="versioninfo"><a href="@HOMEURL@">JaCoCo</a> @VERSION@</div>
339 <a href="license.html">Copyright</a> &copy; 2009 Mountainminds GmbH &amp; Co. KG and Contributors
340</div>
341
342</body>
343</html>