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