JaCoCo in Maven Multi-Module Projects
Join the DZone community and get the full member experience.
Join For Free<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>
- 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.
<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>
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>
<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>
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>
<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>
<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.
Published at DZone with permission of Alex Soto, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments