blob: 1eb4f9a61c07d5643dafe085124e0ca3d8a333c7 [file] [log] [blame]
Evgeny Mandrikov82a92ca2012-01-15 20:25:48 +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 <link rel="stylesheet" href="../coverage/.resources/prettify.css" charset="ISO-8859-1" type="text/css" />
8 <link rel="shortcut icon" href=".resources/report.gif" type="image/gif" />
9 <script type="text/javascript" src="../coverage/.resources/prettify.js"></script>
10 <title>JaCoCo - Ant Tasks</title>
11</head>
12<body onload="prettyPrint()">
13
14<div class="breadcrumb">
15 <a href="../index.html" class="el_report">JaCoCo</a> &gt;
16 <a href="index.html" class="el_group">Documentation</a> &gt;
17 <span class="el_source">Ant Tasks</span>
18</div>
19<div id="content">
20
21<h1>Ant Tasks</h1>
22
23<p>
24 JaCoCo comes with Ant tasks to launch Java programs with execution recording
25 and for creating coverage reports from the recorded data. Execution data can
26 be collected and managed with the tasks
27 <a href="#coverage"><code>coverage</code></a>,
28 <a href="#agent"><code>agent</code></a>,
29 <a href="#dump"><code>dump</code></a> and
30 <a href="#merge"><code>merge</code></a>. Reports in different formats are
31 creates with the <a href="#report"><code>report</code></a> task.
32</p>
33
34<p class="hint">
35 If you want to have line number information included in the coverage reports
36 or you want source code highlighting the class files of the test target must
37 be compiled with debug information.
38</p>
39
40<h2>Example</h2>
41
42<p>
43 The JaCoCo distribution contains a simple example how code coverage can be
44 added to a Ant based build. The
Marc R. Hoffmanna3aa78b2012-05-02 18:56:47 +000045 <a href="examples/build/build.xml">build script</a> compiles Java sources,
Marc R. Hoffmann78309272012-05-02 19:25:44 +000046 runs an simple Java programm and creates a coverage report. The complete
47 example is located in the <code>./doc/examples/build</code> folder of the
48 distribution.
Evgeny Mandrikov82a92ca2012-01-15 20:25:48 +000049</p>
50
51
52<h2>Prerequisites</h2>
53
54<p>
55 The JaCoCo Ant tasks require
56</p>
57
58<ul>
59 <li>Ant 1.7.0 or higher and</li>
60 <li>Java 1.5 or higher (for both, the Ant runner and the test executor).</li>
61</ul>
62
63
64<p>All tasks are defined in <code>jacocoant.jar</code> (which is part of the
65 distribution) and can be included in your Ant scripts with the usual
66 <code>taskdef</code> declaration:
67</p>
68
69<pre class="source lang-xml linenums">
70&lt;project name="Example" xmlns:jacoco="antlib:org.jacoco.ant"&gt;
71
72 &lt;taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml"&gt;
73 &lt;classpath path="<i>path_to_jacoco</i>/lib/jacocoant.jar"/&gt;
74 &lt;/taskdef&gt;
75
76 ...
77
78&lt;/project&gt;
79</pre>
80
81<p>
82 Alternatively you might also place the <code>jacocoant.jar</code> in your
83 Ant <code><i>ANT_HOME</i>/lib</code> folder. If you use the name space URI
84 <code>antlib:org.jacoco.ant</code> for JaCoCo tasks Ant will find them
85 automatically without the <code>taskdef</code> declaration above.
86</p>
87
88<p class="hint">
89 Declaring a XML namespace for JaCoCo tasks is optional but always recommended
90 if you mix tasks from different libraries. All subsequent examples use the
91 <code>jacoco</code> prefix declared above. If you don't declare a separate
92 namespace the <code>jacoco</code> prefix must be removed from the following
93 examples.
94</p>
95
96<h2><a name="coverage">Task <code>coverage</code></a></h2>
97
98<p>
99 The standard Ant tasks to launch Java programs are <code>java</code>, <code>junit</code> and
100 <code>testng</code>. To add code coverage recording to these tasks they can
101 simply be wrapped with the <code>coverage</code> task as shown in the
102 following examples:
103</p>
104
105<pre class="source lang-xml linenums">
106&lt;jacoco:coverage>
107 &lt;java classname="org.jacoco.examples.HelloJaCoCo" fork="true"&gt;
108 &lt;classpath&gt;
109 &lt;pathelement location="./bin"/&gt;
110 &lt;/classpath&gt;
111 &lt;/java&gt;
112&lt;/jacoco:coverage&gt;
113
114
115&lt;jacoco:coverage>
116 &lt;junit fork="true" forkmode="once"&gt;
117 &lt;test name="org.jacoco.examples.HelloJaCoCoTest"/&gt;
118 &lt;classpath&gt;
119 &lt;pathelement location="./bin"/&gt;
120 &lt;/classpath&gt;
121 &lt;/junit&gt;
122&lt;/jacoco:coverage&gt;
123</pre>
124
125<p>
126 Resulting coverage information is collected during execution and written
127 to a file when the process terminates. Note the <code>fork</code> attribute
128 above in the wrapped <code>java</code> task.
129</p>
130
131<p class="hint">
132 The nested task always has to declare <code>fork="true"</code>, otherwise the
133 <code>coverage</code> task can't record coverage information and will fail.
134 In addition the <code>junit</code> task should declare
135 <code>forkmode="once"</code> to avoid starting a new JVM for every single test
136 case and decreasing execution performance dramatically (unless this is
137 required by the nature of the test cases).
138</p>
139
140<p>
141 The coverage task must wrap exactly one task. While it typically works without
142 any configuration, the behavior can be adjusted with some optional attributes:
143</p>
144
145<table class="coverage">
146 <thead>
147 <tr>
148 <td>Attribute</td>
149 <td>Description</td>
150 <td>Default</td>
151 </tr>
152 </thead>
153 <tbody>
154 <tr>
155 <td><code>enabled</code></td>
156 <td>If set to <code>true</code> coverage data will be collected for the contained task.</td>
157 <td><code>true</code></td>
158 </tr>
159 <tr>
160 <td><code>destfile</code></td>
161 <td>Path to the output file for execution data.</td>
162 <td><code>jacoco.exec</code></td>
163 </tr>
164 <tr>
165 <td><code>append</code></td>
166 <td>If set to <code>true</code> and the execution data file already
167 exists, coverage data is appended to the existing file. If set to
168 <code>false</code>, an existing execution data file will be replaced.
169 </td>
170 <td><code>true</code></td>
171 </tr>
172 <tr>
173 <td><code>includes</code></td>
174 <td>A list of class names that should be included in execution analysis.
175 The list entries are separated by a colon (<code>:</code>) and
176 may use wildcard characters (<code>*</code> and <code>?</code>).
177 Except for performance optimization or technical corner cases this
178 option is normally not required.
179 </td>
180 <td><code>*</code> (all classes)</td>
181 </tr>
182 <tr>
183 <td><code>excludes</code></td>
184 <td>A list of class names that should be excluded from execution analysis.
185 The list entries are separated by a colon (<code>:</code>) and
186 may use wildcard characters (<code>*</code> and <code>?</code>).
187 Except for performance optimization or technical corner cases this
188 option is normally not required.
189 </td>
190 <td><i>empty</i> (no excluded classes)</td>
191 </tr>
192 <tr>
193 <td><code>exclclassloader</code></td>
194 <td>A list of class loader names, that should be excluded from execution
195 analysis. The list entries are separated by a colon
196 (<code>:</code>) and may use wildcard characters (<code>*</code> and
197 <code>?</code>). This option might be required in case of special
198 frameworks that conflict with JaCoCo code instrumentation, in
199 particular class loaders that do not have access to the Java runtime
200 classes.
201 </td>
202 <td><code>sun.reflect.DelegatingClassLoader</code></td>
203 </tr>
204 <tr>
205 <td><code>sessionid</code></td>
206 <td>A session identifier that is written with the execution data. Without
207 this parameter a random identifier is created by the agent.
208 </td>
209 <td><i>auto-generated</i></td>
210 </tr>
211 <tr>
212 <td><code>dumponexit</code></td>
213 <td>If set to <code>true</code> coverage data will be written on VM
214 shutdown.
215 </td>
216 <td><code>true</code></td>
217 </tr>
218 <tr>
219 <td><code>output</code></td>
220 <td>Output method to use for writing coverage data. Valid options are:
221 <ul>
222 <li><code>file</code>: At VM termination execution data is written to
223 the file specified in the <code>destfile</code> attribute.</li>
224 <li><code>tcpserver</code>: The agent listens for incoming connections
225 on the TCP port specified by the <code>address</code> and
226 <code>port</code> attribute. Execution data is written to this
227 TCP connection.</li>
228 <li><code>tcpclient</code>: At startup the agent connects to the TCP
229 port specified by the <code>address</code> and <code>port</code>
230 attribute. Execution data is written to this TCP connection.</li>
231 <li><code>mbean</code>: The agent registers an JMX MBean under the
232 name <code>org.jacoco:type=Runtime</code>.</li>
233 </ul>
234 </td>
235 <td><code>file</code></td>
236 </tr>
237 <tr>
238 <td><code>address</code></td>
239 <td>IP address or hostname to bind to when the output method is
240 <code>tcpserver</code> or connect to when the output method is
241 <code>tcpclient</code>. In <code>tcpserver</code> mode the value
242 "<code>*</code>" causes the agent to accept connections on any local
243 address.
244 </td>
245 <td><i>loopback interface</i></td>
246 </tr>
247 <tr>
248 <td><code>port</code></td>
249 <td>Port to bind to when the output method is <code>tcpserver</code> or
250 connect to when the output method is <code>tcpclient</code>. In
251 <code>tcpserver</code> mode the port must be available, which means
252 that if multiple JaCoCo agents should run on the same machine,
253 different ports have to be specified.
254 </td>
255 <td><code>6300</code></td>
256 </tr>
257 </tbody>
258</table>
259
260
261<h2><a name="agent">Task <code>agent</code></a></h2>
262
263<p>
264 If the <code>coverage</code> task is not suitable for your launch target, you
265 might alternatively use the <code>agent</code> task to create the
266 <a href="agent.html">Java agent</a> parameter. The following example defines a
267 Ant property with the name <code>agentvmparam</code> that can be directly used
268 as a Java VM parameter:
269</p>
270
271<pre class="source lang-xml linenums">
272&lt;jacoco:agent property="agentvmparam"/&gt;
273</pre>
274
275<p>
276 This task has the same attributes as the <code>coverage</code> task plus an
277 additional property to specify the target property name:
278</p>
279
280<table class="coverage">
281 <thead>
282 <tr>
283 <td>Attribute</td>
284 <td>Description</td>
285 <td>Default</td>
286 </tr>
287 </thead>
288 <tbody>
289 <tr>
290 <td><code>enabled</code></td>
291 <td>When this variable is set to <code>false</code> the value of <code>property</code> will be set to an empty string, effectively
292 disabling coverage instrumentation for any tasks that used the value.</td>
293 <td><code>true</code></td>
294 </tr>
295 <tr>
296 <td><code>property</code></td>
297 <td>Name of the Ant property to set.</td>
298 <td><i>none (required)</i></td>
299 </tr>
300 <tr>
301 <td colspan="3"><i>All attributes of the <code>coverage</code> task.</i></td>
302 </tr>
303 </tbody>
304</table>
305
306
307<h2><a name="dump">Task <code>dump</code></a></h2>
308
309<p>
310 This task allows to remotely collect execution data from another JVM without
311 stopping it. For example:
312</p>
313
314<pre class="source lang-xml linenums">
315&lt;jacoco:dump address="server.example.com" reset="true" destfile="remote.exec"/&gt;
316</pre>
317
318<p>
319 Remote dumps are usefull for long running Java processes like application
320 servers.
321</p>
322
323<p class="hint">
324 The target JVM needs to have a <a href="agent.html">JaCoCo agent</a>
325 configured with <code>output</code> mode <code>tcpserver</code>. See
326 <a href="#coverage"><code>coverage</code></a> and
327 <a href="#agent"><code>agent</code></a> tasks above.
328</p>
329
330<p>
331 The <code>dump</code> task has the following attributes:
332</p>
333
334<table class="coverage">
335 <thead>
336 <tr>
337 <td>Attribute</td>
338 <td>Description</td>
339 <td>Default</td>
340 </tr>
341 </thead>
342 <tbody>
343 <tr>
344 <td><code>address</code></td>
345 <td>Target IP address or DNS name.</td>
346 <td><code>localhost</code></td>
347 </tr>
348 <tr>
349 <td><code>port</code></td>
350 <td>Target TCP port.</td>
351 <td><code>6300</code></td>
352 </tr>
353 <tr>
354 <td><code>dump</code></td>
355 <td>Flag whether execution data should be dumped.</td>
356 <td><code>true</code></td>
357 </tr>
358 <tr>
359 <td><code>reset</code></td>
360 <td>Flag whether execution data should be reset in the target agent after
361 the dump.</td>
362 <td><code>false</code></td>
363 </tr>
364 <tr>
365 <td><code>destfile</code></td>
366 <td>File location to write the collected execution data to.</td>
367 <td><i>none (required if dump=true)</i></td>
368 </tr>
369 <tr>
370 <td><code>append</code></td>
371 <td>If set to <code>true</code> and the execution data file already
372 exists, coverage data is appended to the existing file. If set to
373 <code>false</code>, an existing execution data file will be replaced.
374 </td>
375 <td><code>true</code></td>
376 </tr>
377 </tbody>
378</table>
379
380
381<h2><a name="merge">Task <code>merge</code></a></h2>
382
383<p>
384 This task can be used to merge the execution data from multiple test runs
385 into a single data store.
386</p>
387
388<pre class="source lang-xml linenums">
389&lt;jacoco:merge destfile="merged.exec"&gt;
390 &lt;fileset dir="executionData" includes="*.exec"/&gt;
391&lt;/jacoco:merge&gt;
392</pre>
393
394<p>
395 The task definition can contain any number of resource collection types and
396 has the following mandatory attribute:
397</p>
398
399<table class="coverage">
400 <thead>
401 <tr>
402 <td>Attribute</td>
403 <td>Description</td>
404 <td>Default</td>
405 </tr>
406 </thead>
407 <tbody>
408 <tr>
409 <td><code>destfile</code></td>
410 <td>File location to write the merged execution data to.</td>
411 <td><i>none (required)</i></td>
412 </tr>
413 </tbody>
414</table>
415
416
417<h2><a name="report">Task <code>report</code></a></h2>
418
419<p>
420 Finally different reports can be created with the <code>report</code> task.
421 A report task declaration consists of different sections, two specify the
422 input data, additional ones specify the output formats:
423</p>
424
425<pre class="source lang-xml linenums">
426&lt;jacoco:report&gt;
427
428 &lt;executiondata&gt;
429 &lt;file file="jacoco.exec"/&gt;
430 &lt;/executiondata&gt;
431
432 &lt;structure name="Example Project"&gt;
433 &lt;classfiles&gt;
434 &lt;fileset dir="classes"/&gt;
435 &lt;/classfiles&gt;
436 &lt;sourcefiles encoding="UTF-8"&gt;
437 &lt;fileset dir="src"/&gt;
438 &lt;/sourcefiles&gt;
439 &lt;/structure&gt;
440
441 &lt;html destdir="report"/&gt;
442
443&lt;/jacoco:report&gt;
444</pre>
445
446<p>
447 As you can see from the example above the <code>report</code> task is based
448 on several nested elements:
449</p>
450
451<h3>Element <code>executiondata</code></h3>
452
453<p>
454 Within this element Ant resources and resource collections can be specified,
455 that represent JaCoCo execution data files. If more than one execution data
456 file is specified, execution data is combined. A particular piece of code is
457 considered executed when it is marked as such in any of the input files.
458</p>
459
460<h3>Element <code>structure</code></h3>
461
462<p>
463 This element defines the report structure. It might contain the following
464 nested elements:
465</p>
466
467<ul>
468 <li><code>classfiles</code>: Container element for Ant resources and resource
469 collections that can specify Java class files, ZIP archive files (jar, war,
470 ear etc.) or folders containing class files. Archives and folders are
471 searched recursively for class files.</li>
472 <li><code>sourcefiles</code>: Optional container element for Ant resources and
473 resource collections that specify corresponding source files. If source
Marc R. Hoffmann8b5d6a32012-01-18 20:24:45 +0000474 files are specified, some report formats include highlighted source code.
475 Source files can be specified as individual files or as source directories.</li>
Evgeny Mandrikov82a92ca2012-01-15 20:25:48 +0000476</ul>
477
478<p>
479 The <code>sourcefiles</code> element has these optional attributes:
480</p>
481
482<table class="coverage">
483 <thead>
484 <tr>
485 <td>Attribute</td>
486 <td>Description</td>
487 <td>Default</td>
488 </tr>
489 </thead>
490 <tbody>
491 <tr>
492 <td><code>encoding</code></td>
493 <td>Character encoding of the source files.</td>
494 <td>Platform default encoding</td>
495 </tr>
496 <tr>
497 <td><code>tabwidth</code></td>
498 <td>Number of whitespace characters that represent a tab character.</td>
499 <td>4 characters</td>
500 </tr>
501 </tbody>
502</table>
503
Marc R. Hoffmann8b5d6a32012-01-18 20:24:45 +0000504<p class="hint">
505 <b>Important:</b> Source file resources must always be specified relative to
506 the respective source folder. If directory resources are given, they must
507 directly point to source folders. Otherwise source lookup will not succeed.
508</p>
509
Evgeny Mandrikov82a92ca2012-01-15 20:25:48 +0000510<p>
511 Note that the <code>classfiles</code> and <code>sourcefiles</code> elements
512 accept any
513 <a href="http://ant.apache.org/manual/Types/resources.html#collection">Ant
514 resource collection</a>. Therefore also filtering the class file set is
515 possible and allows to narrow the scope of the report, for example:
516</p>
517
518<pre class="source lang-xml linenums">
519&lt;classfiles&gt;
520 &lt;fileset dir="classes"&gt;
521 &lt;include name="org/jacoco/examples/important/**/*.class"/&gt;
522 &lt;/fileset&gt;
523&lt;/classfiles&gt;
524</pre>
525
526<p class="hint">
527 <b>Performance Warning:</b> Although it is technically possible and sometimes
528 convenient to use Ant's <code>zipfileset</code> to specify class or source
529 files, this resource type has poor performance characteristics and comes with
530 an huge memory overhead especially for large scale projects.
531</p>
532
533<p>
534 The structure can be refined with a hierarchy of <code>group</code> elements.
535 This way the coverage report can reflect different modules of a software
536 project. For each group element the corresponding class and source files can
537 be specified separately. For example:
538</p>
539
540<pre class="source lang-xml linenums">
541&lt;structure name="Example Project"&gt;
542 &lt;group name="Server"&gt;
543 &lt;classfiles&gt;
544 &lt;fileset dir="${workspace.dir}/org.jacoco.example.server/classes"/&gt;
545 &lt;/classfiles&gt;
546 &lt;sourcefiles&gt;
547 &lt;fileset dir="${workspace.dir}/org.jacoco.example.server/src"/&gt;
548 &lt;/sourcefiles&gt;
549 &lt;/group&gt;
550 &lt;group name="Client"&gt;
551 &lt;classfiles&gt;
552 &lt;fileset dir="${workspace.dir}/org.jacoco.example.client/classes"/&gt;
553 &lt;/classfiles&gt;
554 &lt;sourcefiles&gt;
555 &lt;fileset dir="${workspace.dir}/org.jacoco.example.client/src"/&gt;
556 &lt;/sourcefiles&gt;
557 &lt;/group&gt;
558
559 ...
560
561&lt;/structure&gt;
562</pre>
563
564<p>
565 Both <code>structure</code> and <code>group</code> elements have the following
566 mandatory attribute:
567</p>
568
569<table class="coverage">
570 <thead>
571 <tr>
572 <td>Attribute</td>
573 <td>Description</td>
574 <td>Default</td>
575 </tr>
576 </thead>
577 <tbody>
578 <tr>
579 <td><code>name</code></td>
580 <td>Name of the structure or group.</td>
581 <td><i>none (required)</i></td>
582 </tr>
583 </tbody>
584</table>
585
586<h3>Element <code>html</code></h3>
587
588<p>
589 Create a multi-page report in HTML format. The report can either be written as
590 multiple files into a directory or compressed into a single ZIP file.
591</p>
592
593<table class="coverage">
594 <thead>
595 <tr>
596 <td>Attribute</td>
597 <td>Description</td>
598 <td>Default</td>
599 </tr>
600 </thead>
601 <tbody>
602 <tr>
603 <td><code>destdir</code></td>
604 <td>Directory to create the report in. Either this property or
605 <code>destfile</code> has to be supplied.</td>
606 <td><i>none (required)</i></td>
607 </tr>
608 <tr>
609 <td><code>destfile</code></td>
610 <td>Zip file to create the report in. Either this property or
611 <code>destdir</code> has to be supplied.</td>
612 <td><i>none (required)</i></td>
613 </tr>
614 <tr>
615 <td><code>footer</code></td>
616 <td>Footer text for each report page.</td>
617 <td><i>no footer</i></td>
618 </tr>
619 <tr>
620 <td><code>encoding</code></td>
621 <td>Character encoding of generated HTML pages.</td>
622 <td><code>UTF-8</code></td>
623 </tr>
624 <tr>
625 <td><code>locale</code></td>
626 <td>Locale specified as ISO code (en, fr, jp, ...) used for number formating.</td>
627 <td><i>platform locale</i></td>
628 </tr>
629 </tbody>
630</table>
631
632<h3>Element <code>xml</code></h3>
633
634<p>
635 Create a single-file report in XML format.
636</p>
637
638<table class="coverage">
639 <thead>
640 <tr>
641 <td>Attribute</td>
642 <td>Description</td>
643 <td>Default</td>
644 </tr>
645 </thead>
646 <tbody>
647 <tr>
648 <td><code>destfile</code></td>
649 <td>Location to write the report file to.</td>
650 <td><i>none (required)</i></td>
651 </tr>
652 <tr>
653 <td><code>encoding</code></td>
654 <td>Encoding of the generated XML document.</td>
655 <td><code>UTF-8</code></td>
656 </tr>
657 </tbody>
658</table>
659
660<h3>Element <code>csv</code></h3>
661
662<p>
663 Create single-file report in CSV format.
664</p>
665
666<table class="coverage">
667 <thead>
668 <tr>
669 <td>Attribute</td>
670 <td>Description</td>
671 <td>Default</td>
672 </tr>
673 </thead>
674 <tbody>
675 <tr>
676 <td><code>destfile</code></td>
677 <td>Location to write the report file to.</td>
678 <td><i>none (required)</i></td>
679 </tr>
680 <tr>
681 <td><code>encoding</code></td>
682 <td>Encoding of the generated CSV document.</td>
683 <td><code>UTF-8</code></td>
684 </tr>
685 </tbody>
686</table>
687
688</div>
689<div class="footer">
690 <span class="right"><a href="@jacoco.home.url@">JaCoCo</a> @qualified.bundle.version@</span>
691 <a href="license.html">Copyright</a> &copy; @copyright.years@ Mountainminds GmbH &amp; Co. KG and Contributors
692</div>
693
694</body>
695</html>