Oct 6, 2020 by Thibault Debatty | 24327 views
https://cylab.be/blog/97/compute-code-coverage-for-a-multi-module-maven-project-with-jacoco
In a previous blog post, we showed how to use Jacoco to check the code coverage of your tests in a maven Java project. If your project is substantial, you will have multiple maven modules. So how can we compute the global code coverage?
https://cylab.be/blog/94/compute-the-code-coverage-of-your-tests-with-java-and-maven
The trick is to create a dedicated module for the creation of the aggregated report. So here are the different steps.
In each module, add jacoco to compute per-module coverage report, as explained in our previous blog post:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<!-- to avoid bugs in some situations -->
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- create report during maven verify phase -->
<execution>
<id>report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
Create a new module. For this blog post we will call it coverage. In this module you have to:
report-aggregate
.maven.deploy.skip
, to avoid deploying this module with the rest of the project.packaging
as jar
, otherwise you might receive an error "Not executing Javadoc as the project is not a Java classpath-capable package".Here is a complete example, that you have to tune for your project:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>my.project</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>coverage</artifactId>
<name>coverage</name>
<description>Compute aggregated test code coverage</description>
<properties>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>module1</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>module2</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<execution>
<id>report-aggregate</id>
<phase>verify</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Finally, add coverage to the parent module, so your code coverage is computed each time you build your project:
<modules>
<module>module1</module>
<module>module2</module>
<module>coverage</module>
</modules>
You can now rebuild your complete project:
mvn clean install
The aggregated coverage report will available in coverage/target/site/jacoco-aggregate
, and you can use the jacoco-summary script from previous blog post to display the coverage ratio:
This blog post is licensed under
CC BY-SA 4.0