blob: 55824b575ddfca66a3e12be599086ec006c58761 [file] [log] [blame]
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link rel="stylesheet" href=".resources/doc.css" charset="ISO-8859-1" type="text/css" />
<title>JaCoCo - Ant Tasks</title>
</head>
<body>
<div class="breadcrumb">
<a href="../index.html" class="el_session">JaCoCo</a> &gt;
<a href="index.html" class="el_group">Documentation</a> &gt;
<span class="el_source">Ant Tasks</span>
</div>
<h1>Ant Tasks</h1>
<p>
JaCoCo comes with Ant tasks to launch Java programs with execution recording
and for creating coverage reports from the recorded data. The JaCoCo Ant tasks
require
</p>
<ul>
<li>Ant 1.7.0 or higher and</li>
<li>Java 1.5 or higher (for both, the Ant runner and the test executor).</li>
</ul>
<p>All tasks are defined in <code>jacocoant.jar</code> (which is part of the
distribution) and can be included in your Ant scripts with the usual
<code>taskdef</code> declaration:
</p>
<pre class="source">
<span class="nr"> 1</span>&lt;project name="Example" xmlns:jacoco="antlib:org.jacoco.ant"&gt;
<span class="nr"> 2</span>
<span class="nr"> 3</span> &lt;taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml"&gt;
<span class="nr"> 4</span> &lt;classpath&gt;
<span class="nr"> 5</span> &lt;pathelement location="<i>path_to_jacoco</i>/lib/jacocoant.jar"/&gt;
<span class="nr"> 6</span> &lt;/classpath&gt;
<span class="nr"> 7</span> &lt;/taskdef&gt;
<span class="nr"> 8</span>
<span class="nr"> 9</span> ...
<span class="nr"> 10</span>
<span class="nr"> 11</span>&lt;/project&gt;
</pre>
<p>
Alternatively you might also place the <code>jacocoant.jar</code> in your
Ant <code><i>ANT_HOME</i>/lib</code> folder. If you use the name space URI
<code>antlib:org.jacoco.ant</code> for JaCoCo tasks Ant will find them
automatically without the <code>taskdef</code> declaration above.
</p>
<p>
Declaring a XML namespace for JaCoCo tasks is optional but always recommended
if you mix tasks from different libraries. All subsequent examples use the
<code>jacoco</code> prefix declared above.
</p>
<h2>Task <code>coverage</code></h2>
<p>
The standard Ant tasks to launch Java programs are <code>java</code> and
<code>junit</code>. To add code coverage recording to these tasks they can
simply be wrapped with the <code>coverage</code> task as shown in the
following examples:
</p>
<pre class="source">
<span class="nr"> 1</span>&lt;jacoco:coverage>
<span class="nr"> 2</span> &lt;java classname="org.jacoco.examples.HelloJaCoCo" fork="true"&gt;
<span class="nr"> 3</span> &lt;classpath&gt;
<span class="nr"> 4</span> &lt;pathelement location="./bin"/&gt;
<span class="nr"> 5</span> &lt;/classpath&gt;
<span class="nr"> 6</span> &lt;/java&gt;
<span class="nr"> 7</span>&lt;/jacoco:coverage&gt;
<span class="nr"> 8</span>
<span class="nr"> 9</span>
<span class="nr"> 10</span>&lt;jacoco:coverage>
<span class="nr"> 11</span> &lt;junit fork="true" forkmode="once"&gt;
<span class="nr"> 12</span> &lt;test name="org.jacoco.examples.HelloJaCoCoTest"/&gt;
<span class="nr"> 13</span> &lt;classpath&gt;
<span class="nr"> 14</span> &lt;pathelement location="./bin"/&gt;
<span class="nr"> 15</span> &lt;/classpath&gt;
<span class="nr"> 16</span> &lt;/junit&gt;
<span class="nr"> 17</span>&lt;/jacoco:coverage>
</pre>
<p>
Resulting coverage information is collected during execution and written
to a file when the process terminates. Note the <code>fork</code> attribute
above in the wrapped <code>java</code> task.
</p>
<p class="hint">
The nested task always has to declare <code>fork="true"</code>, otherwise the
<code>coverage</code> task can't record coverage information and will fail.
In addition the <code>junit</code> task should declare
<code>forkmode="once"</code> to avoid starting a new JVM for every single test
case and decreasing execution performance dramatically (unless this is
required by the nature of the test cases).
</p>
<p>
The coverage task must wrap exactly one task. While it typically works without
any configuration, the behavior can be adjusted with some optional attributes:
</p>
<table class="coverage">
<thead>
<tr>
<td>Attribute</td>
<td>Description</td>
<td>Default</td>
</tr>
</thead>
<tbody>
<tr>
<td><code>file</code></td>
<td>Path to the output file for execution data.</td>
<td><code>jacoco.exec</code></td>
</tr>
<tr>
<td><code>merge</code></td>
<td>If set to <code>true</code> and the execution data file already
exists, coverage data is merged to the existing file. If set to
<code>false</code>, an existing execution data file will be replaced.
</td>
<td><code>true</code></td>
</tr>
<tr>
<td><code>includes</code></td>
<td>A list of class names that should be included in execution analysis.
The list entries are separated by a vertical bar (<code>|</code>) and
may use wildcard characters (<code>*</code> and <code>?</code>).
Except for performance optimization or technical corner cases this
option is normally not required.
</td>
<td><code>*</code> (all classes)</td>
</tr>
<tr>
<td><code>excludes</code></td>
<td>A list of class names that should be excluded from execution analysis.
The list entries are separated by a vertical bar (<code>|</code>) and
may use wildcard characters (<code>*</code> and <code>?</code>).
Except for performance optimization or technical corner cases this
option is normally not required.
</td>
<td><i>empty</i> (no excluded classes)</td>
</tr>
<tr>
<td><code>exclclassloader</code></td>
<td>A list of class loader names, that should be excluded from execution
analysis. The list entries are separated by a vertical bar
(<code>|</code>) and may use wildcard characters (<code>*</code> and
<code>?</code>). This option might be required in case of special
frameworks that conflict with JaCoCo code instrumentation, in
particular class loaders that do not have access to the Java runtime
classes.
</td>
<td><code>sun.reflect.DelegatingClassLoader</code></td>
</tr>
</tbody>
</table>
<h2>Task <code>agent</code></h2>
<p>
If the <code>coverage</code> task is not suitable for your launch target, you
might alternatively use the <code>agent</code> task to create the Java agent
parameter. The following example defines a Ant property with the name
<code>agentvmparam</code> that can be directly used as a Java VM parameter:
</p>
<pre class="source">
<span class="nr"> 1</span>&lt;jacoco:agent property="agentvmparam"/>
</pre>
<p>
This task has the same attributes as the <code>coverage</code> task plus an
additional property to specify the target property name:
</p>
<table class="coverage">
<thead>
<tr>
<td>Attribute</td>
<td>Description</td>
<td>Default</td>
</tr>
</thead>
<tbody>
<tr>
<td><code>property</code></td>
<td>Name of the Ant property to set.</td>
<td><i>none (required)</i></td>
</tr>
<tr>
<td colspan="3"><i>All attributes of the <code>coverage</code> task.</i></td>
</tr>
</tbody>
</table>
<h2>Task <code>report</code></h2>
<p>
Finally different reports can be created with the <code>report</code> task.
A report task declaration consists of different sections, two specify the
input data, additional ones specify the output formats:
</p>
<pre class="source">
<span class="nr"> 1</span>&lt;jacoco:report&gt;
<span class="nr"> 2</span>
<span class="nr"> 3</span> &lt;executiondata&gt;
<span class="nr"> 4</span> &lt;file file="jacoco.exec"/&gt;
<span class="nr"> 5</span> &lt;/executiondata&gt;
<span class="nr"> 6</span>
<span class="nr"> 7</span> &lt;structure name="Example Project"&gt;
<span class="nr"> 8</span> &lt;classfiles&gt;
<span class="nr"> 9</span> &lt;fileset dir="bin"/&gt;
<span class="nr"> 10</span> &lt;/classfiles&gt;
<span class="nr"> 11</span> &lt;sourcefiles encoding="UTF-8"&gt;
<span class="nr"> 12</span> &lt;fileset dir="src"/&gt;
<span class="nr"> 13</span> &lt;/sourcefiles&gt;
<span class="nr"> 14</span> &lt;/structure&gt;
<span class="nr"> 15</span>
<span class="nr"> 16</span> &lt;html destdir="report"/&gt;
<span class="nr"> 17</span>
<span class="nr"> 18</span>&lt;/jacoco:report&gt;
</pre>
<p>
As you can see from the example above the <code>report</code> task is based
on several nested elements:
</p>
<h3>Element <code>executiondata</code></h3>
<p>
Within this element Ant resources and resource collections can be specified,
that represent JaCoCo execution data files. If more than one execution data
file is specified, execution data is combined. A particular piece of code is
considered executed when it is marked as such in any of the input files.
</p>
<h3>Element <code>structure</code></h3>
<p>
This element defines the report structure. It might contain the following
nested elements:
</p>
<ul>
<li><code>classfiles</code>: Container element for Ant resources and resource
collections that can specify Java class files, JAR files or directories
containing class files.</li>
<li><code>sourcefiles</code>: Optional container element for Ant resources and
resource collections that specify corresponding source files. The element
has an optional attribute <code>encoding</code> to specify the character
encoding of the source files. If no encoding is given, the platform default
is used. If source files are specified, some report formats include
highlighted source code.</li>
</ul>
<p>
The structure can be refined with a hierarchy of <code>group</code> elements.
This way the coverage report can reflect different modules of a software
project. For each group element the corresponding class and source files can
be specified separately. For example, the build script of JaCoCo itself
contains the following declaration to separate the different bundles in the
report (see the <a href="../coverage/index.html">resulting report</a>):
</p>
<pre class="source">
<span class="nr"> 1</span>&lt;structure name="JaCoCo"&gt;
<span class="nr"> 2</span> &lt;group name="org.jacoco.core"&gt;
<span class="nr"> 3</span> &lt;classfiles&gt;
<span class="nr"> 4</span> &lt;path refid="bundle-org.jacoco.core"/&gt;
<span class="nr"> 5</span> &lt;/classfiles&gt;
<span class="nr"> 6</span> &lt;sourcefiles&gt;
<span class="nr"> 7</span> &lt;fileset dir="${workspace.dir}/org.jacoco.core/src"/&gt;
<span class="nr"> 8</span> &lt;/sourcefiles&gt;
<span class="nr"> 9</span> &lt;/group&gt;
<span class="nr"> 10</span> &lt;group name="org.jacoco.report"&gt;
<span class="nr"> 11</span> &lt;classfiles&gt;
<span class="nr"> 12</span> &lt;path refid="bundle-org.jacoco.report"/&gt;
<span class="nr"> 13</span> &lt;/classfiles&gt;
<span class="nr"> 14</span> &lt;sourcefiles&gt;
<span class="nr"> 15</span> &lt;fileset dir="${workspace.dir}/org.jacoco.report/src"/&gt;
<span class="nr"> 16</span> &lt;/sourcefiles&gt;
<span class="nr"> 17</span> &lt;/group&gt;
<span class="nr"> 18</span>
<span class="nr"> 19</span> ...
<span class="nr"> 20</span>
<span class="nr"> 21</span>&lt;/structure&gt;
</pre>
<h3>Element <code>html</code></h3>
<p>
Create a multi-page report in HTML format.
</p>
<table class="coverage">
<thead>
<tr>
<td>Attribute</td>
<td>Description</td>
<td>Default</td>
</tr>
</thead>
<tbody>
<tr>
<td><code>destdir</code></td>
<td>Directory to create the report in.</td>
<td><i>none (required)</i></td>
</tr>
<tr>
<td><code>footer</code></td>
<td>Footer text for each report page.</td>
<td><i>No footer</i></td>
</tr>
<tr>
<td><code>encoding</code></td>
<td>Encoding of the generated HTML pages.</td>
<td><code>UTF-8</code></td>
</tr>
</tbody>
</table>
<h3>Element <code>xml</code></h3>
<p>
Create a single-file report in XML format.
</p>
<table class="coverage">
<thead>
<tr>
<td>Attribute</td>
<td>Description</td>
<td>Default</td>
</tr>
</thead>
<tbody>
<tr>
<td><code>destfile</code></td>
<td>Location to write the report file to.</td>
<td><i>none (required)</i></td>
</tr>
<tr>
<td><code>encoding</code></td>
<td>Encoding of the generated XML document.</td>
<td><code>UTF-8</code></td>
</tr>
</tbody>
</table>
<h3>Element <code>csv</code></h3>
<p>
Create single-file report in CSV format.
</p>
<table class="coverage">
<thead>
<tr>
<td>Attribute</td>
<td>Description</td>
<td>Default</td>
</tr>
</thead>
<tbody>
<tr>
<td><code>destfile</code></td>
<td>Location to write the report file to.</td>
<td><i>none (required)</i></td>
</tr>
<tr>
<td><code>encoding</code></td>
<td>Encoding of the generated CSV document.</td>
<td><code>UTF-8</code></td>
</tr>
</tbody>
</table>
<div class="footer">
<div class="versioninfo"><a href="@HOMEURL@">JaCoCo</a> @VERSION@</div>
<a href="license.html">Copyright</a> &copy; 2009 Mountainminds GmbH &amp; Co. KG and Contributors
</div>
</body>
</html>