JaCoCo in Maven Multi-Module Projects
Join the DZone community and get the full member experience.
Join For Free
Code coverage is an important measurement used during our development that describes the degree to which source code is tested.
In this post I am going to explain how to run code coverage using Maven and JaCoCo plugins in multi-module projects.
JaCoCo is a code coverage library for Java, which has been created by the EclEmma team. It has a plugin for Eclipse, and can be run with Ant and Maven too.
Now we will focus only on a Maven approach.
In a project with only one module is as easy as registering a build plugin:
<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.5.7.201204190339</version> <executions> <execution> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>report</id> <phase>prepare-package</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin>
And now running mvn package in site/jacoco directory, a coverage report will be present in different formats.
But with multimodule projects a new problem arises. How do we merge the metrics
of all subprojects into only one file so we can have a quick overview
of all subprojects? For now the Maven JaCoCo Plugin does not support it.
There are many alternatives and I am going to cite the most common:
- Sonar. It has the disadvantage that you need to install Sonar (maybe you are already using it, but maybe not).
- Jenkins. The plugin for JaCoCo
is still under development. Moreover you need to run a build job to
inspect your coverage. This is good in terms of continuous integration
but could be a problem if you are trying to "catch" some piece of code
that have not been covered with previously implemented tests.
- Arquillian JaCoCo Extension. Arquillian
is a container test framework that has an extension which during test
execution can capture the coverage. It's a good option if you are using Arquillian. The disadvantage is that maybe your project does not require a container.
- Ant. You can use an Ant task with Maven. JaCoCo Ant tasks can merge results from multiple JaCoCo file results. Note that this is the most generic solution, and this is the chosen approach that we are going to use.
The first thing to do is add a JaCoCo plugin to the parent pom so
all projects could generate a coverage report. Of course, if there are
modules which do not require coverage, the plugin definition should be
changed from parent pom to specific projects.
The next step is creating a specific submodule for appending all results of the JaCoCo plugin by using an Ant task. I suggest using something like project-name-coverage.
<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.5.7.201204190339</version> <executions> <execution> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>report</id> <phase>prepare-package</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin>
The next step is creating a specific submodule for appending all results of the JaCoCo plugin by using an Ant task. I suggest using something like project-name-coverage.
Then let's open generated pom.xml and we are going to insert the required plugins to join all coverage information. To append them. As we have already written we are going to use a JaCoCo Ant task which has the ability to open all JaCoCo output files and append all their content into one. So the first thing to do is download the jar which contains the JaCoCo Ant task. To automate the download process, we are going to use maven dependency plugin:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <!-- Copy the ant tasks jar. Needed for ts.jacoco.report-ant . --> <execution> <id>jacoco-dependency-ant</id> <goals> <goal>copy</goal> </goals> <phase>process-test-resources</phase> <inherited>false</inherited> <configuration> <artifactItems> <artifactItem> <groupId>org.jacoco</groupId> <artifactId>org.jacoco.ant</artifactId> <version>${jacoco.version}</version> </artifactItem> </artifactItems> <stripVersion>true</stripVersion> <outputDirectory>${basedir}/target/jacoco-jars</outputDirectory> </configuration> </execution> </executions> </plugin>
During process-test-resources phase Jacoco Ant artifact will be downloaded and copied to the target directory so it can be registered into the pom without worrying about the jar location.
We also need a way to handle Ant tasks from Maven. And this is as simple as using maven antrun plugin, which you can specify any ant command in its configuration section. See next simple example:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.6</version> <executions> <execution> <phase>compile</phase> <goals> <goal>run</goal> </goals> <configuration> <target> <!-- Execute an ant task within maven --> <echo message="Hello World from pom.xml"/> </target> </configuration> </execution> </executions> </plugin>
Notice that we can specify any Ant task in the target tag. And now we are ready to start configuring the JaCoCo Ant task. The JaCoCo report plugin requires you set the location of the build directory, class
directory, source directory or generated-source directory. For this
purpose we are going set them as properties.
<build.directory.projecta>../projectA/target</build.directory.projecta> <build.directory.projectb>../projectB/target</build.directory.projectb> <classes.directory.projecta>../projectA/target/classes</classes.directory.projecta> <classes.directory.projectb>../projectB/target/classes</classes.directory.projectb> <sources.directory.projecta>../projectA/src/main/java</sources.directory.projecta> <sources.directory.projectb>../projectB/src/main/java</sources.directory.projectb> <generated-sources.directory.projecta>../projectA/target/generated-sources/annotations</generated-sources.directory.projecta> <generated-sources.directory.projectb>../projectB/target/generated-sources/annotations</generated-sources.directory.projectb>
And now the Ant task part which will go into target tag of the antrun plugin.
First we need to define report task.
First we need to define report task.
<taskdef name="report" classname="org.jacoco.ant.ReportTask"> <classpath path="${basedir}/target/jacoco-jars/org.jacoco.ant.jar" /> </taskdef>
Do you see that org.jacoco.ant.jar file is downloaded by the dependency plugin? You don't need to worry about copying it manually.
Then we are going to call report task as defined in taskdef section.
<report> <executiondata> <fileset dir="${build.directory.projecta}"> <include name="jacoco.exec" /> </fileset> <fileset dir="${build.directory.projectb}"> <include name="jacoco.exec" /> </fileset> </executiondata> <structure name="JaCoCo-Multi Project"> <group name="JaCoCo-Multi"> <classfiles> <fileset dir="${classes.directory.projecta}" /> <fileset dir="${classes.directory.projectb}" /> </classfiles> <sourcefiles encoding="UTF-8"> <fileset dir="${sources.directory.projecta}" /> <fileset dir="${sources.directory.projectb}"></fileset> <fileset dir="${generated-sources.directory.projecta}"></fileset> <fileset dir="${generated-sources.directory.projectb}"></fileset> </sourcefiles> </group> </structure> <html destdir="${basedir}/target/coverage-report/html" /> <xml destfile="${basedir}/target/coverage-report/coverage-report.xml" /> <csv destfile="${basedir}/target/coverage-report/coverage-report.csv" /> </report>
Within the executiondata element, we specify locations where JaCoCo execution data files are stored. By default this is the target directory, and for each project we need to add one entry for each submodule.
The next element is structure. This element defines the report
structure, and can be defined with a hierarchy of group elements. Each
group should contain class files and source files of all projects that
belongs to that group. In our example only one group is used.
And finally we are setting output format using html, xml and csv tags.
Complete Code:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.6</version> <executions> <execution> <phase>post-integration-test</phase> <goals> <goal>run</goal> </goals> <configuration> <target> <!-- Execute an ant task within maven --> <echo message="Generating JaCoCo Reports" /> <taskdef name="report" classname="org.jacoco.ant.ReportTask"> <classpath path="${basedir}/target/jacoco-jars/org.jacoco.ant.jar" /> </taskdef> <mkdir dir="${basedir}/target/coverage-report" /> <report> <executiondata> <fileset dir="${build.directory.projecta}"> <include name="jacoco.exec" /> </fileset> <fileset dir="${build.directory.projectb}"> <include name="jacoco.exec" /> </fileset> </executiondata> <structure name="jacoco-multi Coverage Project"> <group name="jacoco-multi"> <classfiles> <fileset dir="${classes.directory.projecta}" /> <fileset dir="${classes.directory.projectb}" /> </classfiles> <sourcefiles encoding="UTF-8"> <fileset dir="${sources.directory.projecta}" /> <fileset dir="${sources.directory.projectb}"></fileset> </sourcefiles> </group> </structure> <html destdir="${basedir}/target/coverage-report/html" /> <xml destfile="${basedir}/target/coverage-report/coverage-report.xml" /> <csv destfile="${basedir}/target/coverage-report/coverage-report.csv" /> </report> </target> </configuration> </execution> </executions> <dependencies> <dependency> <groupId>org.jacoco</groupId> <artifactId>org.jacoco.ant</artifactId> <version>${jacoco.version}</version> </dependency> </dependencies> </plugin>
Hope you find this post useful.
We Keep Learning,
Alex.
Apache Maven
Published at DZone with permission of Alex Soto, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments