blob: e3d030a82960e1eb1256abf0e597ff114df36e36 [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
Brock Janiczakd267c572010-01-28 09:53:54 +000064<h2><a name="coverage">Task <code>coverage</code></a></h2>
Marc R. Hoffmannf7d17522009-08-07 11:21:23 +000065
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>
Radek Libaad5fbc92009-10-26 13:26:53 +000094 Resulting coverage information is collected during execution and written
95 to a file when the process terminates. Note the <code>fork</code> attribute
96 above in the wrapped <code>java</code> task.
Marc R. Hoffmannf7d17522009-08-07 11:21:23 +000097</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>
Brock Janiczakd267c572010-01-28 09:53:54 +0000123 <tr>
124 <td><code>enabled</code></td>
125 <td>If set to <code>true</code> coverage data will be collected for the contained task.</td>
126 <td><code>true</code></td>
127 </tr>
Brock Janiczak003d47c2010-01-15 00:45:17 +0000128 <td><code>destfile</code></td>
Marc R. Hoffmannf7d17522009-08-07 11:21:23 +0000129 <td>Path to the output file for execution data.</td>
130 <td><code>jacoco.exec</code></td>
131 </tr>
132 <tr>
Brock Janiczak003d47c2010-01-15 00:45:17 +0000133 <td><code>append</code></td>
Marc R. Hoffmannf7d17522009-08-07 11:21:23 +0000134 <td>If set to <code>true</code> and the execution data file already
Brock Janiczak003d47c2010-01-15 00:45:17 +0000135 exists, coverage data is appended to the existing file. If set to
Marc R. Hoffmannf7d17522009-08-07 11:21:23 +0000136 <code>false</code>, an existing execution data file will be replaced.
137 </td>
138 <td><code>true</code></td>
139 </tr>
140 <tr>
Marc R. Hoffmanncf5345f2009-09-10 15:01:24 +0000141 <td><code>includes</code></td>
Radek Libaad5fbc92009-10-26 13:26:53 +0000142 <td>A list of class names that should be included in execution analysis.
Marc R. Hoffmanncf5345f2009-09-10 15:01:24 +0000143 The list entries are separated by a vertical bar (<code>|</code>) and
144 may use wildcard characters (<code>*</code> and <code>?</code>).
145 Except for performance optimization or technical corner cases this
146 option is normally not required.
147 </td>
148 <td><code>*</code> (all classes)</td>
149 </tr>
150 <tr>
151 <td><code>excludes</code></td>
152 <td>A list of class names that should be excluded from execution analysis.
153 The list entries are separated by a vertical bar (<code>|</code>) and
154 may use wildcard characters (<code>*</code> and <code>?</code>).
155 Except for performance optimization or technical corner cases this
156 option is normally not required.
157 </td>
158 <td><i>empty</i> (no excluded classes)</td>
159 </tr>
160 <tr>
Marc R. Hoffmannf7d17522009-08-07 11:21:23 +0000161 <td><code>exclclassloader</code></td>
Marc R. Hoffmann2a6b5bf2009-08-11 12:54:36 +0000162 <td>A list of class loader names, that should be excluded from execution
163 analysis. The list entries are separated by a vertical bar
164 (<code>|</code>) and may use wildcard characters (<code>*</code> and
165 <code>?</code>). This option might be required in case of special
Marc R. Hoffmann57f9ab42009-08-24 10:52:42 +0000166 frameworks that conflict with JaCoCo code instrumentation, in
167 particular class loaders that do not have access to the Java runtime
168 classes.
Marc R. Hoffmannf7d17522009-08-07 11:21:23 +0000169 </td>
170 <td><code>sun.reflect.DelegatingClassLoader</code></td>
171 </tr>
172 </tbody>
173</table>
174
175
Brock Janiczakd267c572010-01-28 09:53:54 +0000176<h2><a name="agent">Task <code>agent</code></a></h2>
Marc R. Hoffmannf7d17522009-08-07 11:21:23 +0000177
Marc R. Hoffmannd519add2009-08-11 11:09:29 +0000178<p>
179 If the <code>coverage</code> task is not suitable for your launch target, you
180 might alternatively use the <code>agent</code> task to create the Java agent
181 parameter. The following example defines a Ant property with the name
Radek Libaad5fbc92009-10-26 13:26:53 +0000182 <code>agentvmparam</code> that can be directly used as a Java VM parameter:
Marc R. Hoffmannd519add2009-08-11 11:09:29 +0000183</p>
184
185<pre class="source">
186<span class="nr"> 1</span>&lt;jacoco:agent property="agentvmparam"/>
187</pre>
188
189<p>
190 This task has the same attributes as the <code>coverage</code> task plus an
191 additional property to specify the target property name:
192</p>
193
194<table class="coverage">
195 <thead>
196 <tr>
197 <td>Attribute</td>
198 <td>Description</td>
199 <td>Default</td>
200 </tr>
201 </thead>
202 <tbody>
203 <tr>
Brock Janiczakd267c572010-01-28 09:53:54 +0000204 <td><code>enabled</code></td>
205 <td>When this variable is set to <code>false</code> the value of <code>property</code> will be set to an empty string, effectively
206 disabling coverage instrumentation for any tasks that used the value</td>
207 <td><code>true</code></td>
208 </tr>
209 <tr>
Marc R. Hoffmannd519add2009-08-11 11:09:29 +0000210 <td><code>property</code></td>
211 <td>Name of the Ant property to set.</td>
212 <td><i>none (required)</i></td>
213 </tr>
Marc R. Hoffmann2bb6fca2009-08-13 14:04:21 +0000214 <tr>
215 <td colspan="3"><i>All attributes of the <code>coverage</code> task.</i></td>
216 </tr>
Marc R. Hoffmannd519add2009-08-11 11:09:29 +0000217 </tbody>
218</table>
219
Brock Janiczakd267c572010-01-28 09:53:54 +0000220<h2><a name="report">Task <code>report</code></a></h2>
Marc R. Hoffmannf7d17522009-08-07 11:21:23 +0000221
Marc R. Hoffmann2bb6fca2009-08-13 14:04:21 +0000222<p>
223 Finally different reports can be created with the <code>report</code> task.
224 A report task declaration consists of different sections, two specify the
225 input data, additional ones specify the output formats:
226</p>
227
228<pre class="source">
229<span class="nr"> 1</span>&lt;jacoco:report&gt;
230<span class="nr"> 2</span>
231<span class="nr"> 3</span> &lt;executiondata&gt;
232<span class="nr"> 4</span> &lt;file file="jacoco.exec"/&gt;
233<span class="nr"> 5</span> &lt;/executiondata&gt;
234<span class="nr"> 6</span>
235<span class="nr"> 7</span> &lt;structure name="Example Project"&gt;
236<span class="nr"> 8</span> &lt;classfiles&gt;
237<span class="nr"> 9</span> &lt;fileset dir="bin"/&gt;
238<span class="nr"> 10</span> &lt;/classfiles&gt;
239<span class="nr"> 11</span> &lt;sourcefiles encoding="UTF-8"&gt;
240<span class="nr"> 12</span> &lt;fileset dir="src"/&gt;
241<span class="nr"> 13</span> &lt;/sourcefiles&gt;
242<span class="nr"> 14</span> &lt;/structure&gt;
243<span class="nr"> 15</span>
244<span class="nr"> 16</span> &lt;html destdir="report"/&gt;
245<span class="nr"> 17</span>
246<span class="nr"> 18</span>&lt;/jacoco:report&gt;
247</pre>
248
249<p>
250 As you can see from the example above the <code>report</code> task is based
251 on several nested elements:
252</p>
253
254<h3>Element <code>executiondata</code></h3>
255
256<p>
257 Within this element Ant resources and resource collections can be specified,
258 that represent JaCoCo execution data files. If more than one execution data
259 file is specified, execution data is combined. A particular piece of code is
Radek Libaad5fbc92009-10-26 13:26:53 +0000260 considered executed when it is marked as such in any of the input files.
Marc R. Hoffmann2bb6fca2009-08-13 14:04:21 +0000261</p>
262
263<h3>Element <code>structure</code></h3>
264
265<p>
Radek Libaad5fbc92009-10-26 13:26:53 +0000266 This element defines the report structure. It might contain the following
Marc R. Hoffmann2bb6fca2009-08-13 14:04:21 +0000267 nested elements:
268</p>
269
270<ul>
271 <li><code>classfiles</code>: Container element for Ant resources and resource
272 collections that can specify Java class files, JAR files or directories
273 containing class files.</li>
274 <li><code>sourcefiles</code>: Optional container element for Ant resources and
275 resource collections that specify corresponding source files. The element
276 has an optional attribute <code>encoding</code> to specify the character
277 encoding of the source files. If no encoding is given, the platform default
278 is used. If source files are specified, some report formats include
Radek Libaad5fbc92009-10-26 13:26:53 +0000279 highlighted source code.</li>
Marc R. Hoffmann2bb6fca2009-08-13 14:04:21 +0000280</ul>
281
282<p>
283 The structure can be refined with a hierarchy of <code>group</code> elements.
Radek Libaad5fbc92009-10-26 13:26:53 +0000284 This way the coverage report can reflect different modules of a software
Marc R. Hoffmann2bb6fca2009-08-13 14:04:21 +0000285 project. For each group element the corresponding class and source files can
Radek Libaad5fbc92009-10-26 13:26:53 +0000286 be specified separately. For example, the build script of JaCoCo itself
Marc R. Hoffmann2bb6fca2009-08-13 14:04:21 +0000287 contains the following declaration to separate the different bundles in the
288 report (see the <a href="../coverage/index.html">resulting report</a>):
289</p>
290
291<pre class="source">
292<span class="nr"> 1</span>&lt;structure name="JaCoCo"&gt;
293<span class="nr"> 2</span> &lt;group name="org.jacoco.core"&gt;
294<span class="nr"> 3</span> &lt;classfiles&gt;
295<span class="nr"> 4</span> &lt;path refid="bundle-org.jacoco.core"/&gt;
296<span class="nr"> 5</span> &lt;/classfiles&gt;
297<span class="nr"> 6</span> &lt;sourcefiles&gt;
298<span class="nr"> 7</span> &lt;fileset dir="${workspace.dir}/org.jacoco.core/src"/&gt;
299<span class="nr"> 8</span> &lt;/sourcefiles&gt;
300<span class="nr"> 9</span> &lt;/group&gt;
301<span class="nr"> 10</span> &lt;group name="org.jacoco.report"&gt;
302<span class="nr"> 11</span> &lt;classfiles&gt;
303<span class="nr"> 12</span> &lt;path refid="bundle-org.jacoco.report"/&gt;
304<span class="nr"> 13</span> &lt;/classfiles&gt;
305<span class="nr"> 14</span> &lt;sourcefiles&gt;
306<span class="nr"> 15</span> &lt;fileset dir="${workspace.dir}/org.jacoco.report/src"/&gt;
307<span class="nr"> 16</span> &lt;/sourcefiles&gt;
308<span class="nr"> 17</span> &lt;/group&gt;
309<span class="nr"> 18</span>
310<span class="nr"> 19</span> ...
311<span class="nr"> 20</span>
312<span class="nr"> 21</span>&lt;/structure&gt;
313</pre>
314
315<h3>Element <code>html</code></h3>
316
317<p>
318 Create a multi-page report in HTML format.
319</p>
320
321<table class="coverage">
322 <thead>
323 <tr>
324 <td>Attribute</td>
325 <td>Description</td>
326 <td>Default</td>
327 </tr>
328 </thead>
329 <tbody>
330 <tr>
331 <td><code>destdir</code></td>
332 <td>Directory to create the report in.</td>
333 <td><i>none (required)</i></td>
334 </tr>
Marc R. Hoffmann2ecbea52009-09-03 14:19:11 +0000335 <tr>
336 <td><code>footer</code></td>
337 <td>Footer text for each report page.</td>
338 <td><i>No footer</i></td>
339 </tr>
Marc R. Hoffmann2becd332009-10-07 16:13:17 +0000340 <tr>
341 <td><code>encoding</code></td>
342 <td>Encoding of the generated HTML pages.</td>
343 <td><code>UTF-8</code></td>
344 </tr>
345 </tbody>
346</table>
347
348<h3>Element <code>xml</code></h3>
349
350<p>
351 Create a single-file report in XML format.
352</p>
353
354<table class="coverage">
355 <thead>
356 <tr>
357 <td>Attribute</td>
358 <td>Description</td>
359 <td>Default</td>
360 </tr>
361 </thead>
362 <tbody>
363 <tr>
Marc R. Hoffmann88b3f2f2009-10-19 16:10:35 +0000364 <td><code>destfile</code></td>
365 <td>Location to write the report file to.</td>
Marc R. Hoffmann2becd332009-10-07 16:13:17 +0000366 <td><i>none (required)</i></td>
367 </tr>
368 <tr>
369 <td><code>encoding</code></td>
370 <td>Encoding of the generated XML document.</td>
371 <td><code>UTF-8</code></td>
372 </tr>
Marc R. Hoffmann2bb6fca2009-08-13 14:04:21 +0000373 </tbody>
374</table>
375
376<h3>Element <code>csv</code></h3>
377
378<p>
Marc R. Hoffmann2becd332009-10-07 16:13:17 +0000379 Create single-file report in CSV format.
Marc R. Hoffmann2bb6fca2009-08-13 14:04:21 +0000380</p>
381
382<table class="coverage">
383 <thead>
384 <tr>
385 <td>Attribute</td>
386 <td>Description</td>
387 <td>Default</td>
388 </tr>
389 </thead>
390 <tbody>
391 <tr>
Marc R. Hoffmann88b3f2f2009-10-19 16:10:35 +0000392 <td><code>destfile</code></td>
393 <td>Location to write the report file to.</td>
Marc R. Hoffmann2bb6fca2009-08-13 14:04:21 +0000394 <td><i>none (required)</i></td>
395 </tr>
Marc R. Hoffmanne17fd112009-10-19 15:27:04 +0000396 <tr>
397 <td><code>encoding</code></td>
398 <td>Encoding of the generated CSV document.</td>
399 <td><code>UTF-8</code></td>
400 </tr>
Marc R. Hoffmann2bb6fca2009-08-13 14:04:21 +0000401 </tbody>
402</table>
403
Marc R. Hoffmannf7d17522009-08-07 11:21:23 +0000404
405<div class="footer">
406 <div class="versioninfo"><a href="@HOMEURL@">JaCoCo</a> @VERSION@</div>
Marc R. Hoffmann889d62b2010-01-26 20:08:15 +0000407 <a href="license.html">Copyright</a> &copy; 2009, 2010 Mountainminds GmbH &amp; Co. KG and Contributors
Marc R. Hoffmannf7d17522009-08-07 11:21:23 +0000408</div>
409
410</body>
411</html>