Compute code coverage for a multi-module maven project with Jacoco

Oct 6, 2020 by Thibault Debatty | 24452 views

Java

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.

1. Per-module coverage report

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>

Aggregation module

Create a new module. For this blog post we will call it coverage. In this module you have to:

  1. Add the jacoco build plugin, with the goal report-aggregate.
  2. Add the main modules as dependencies, with the correct scope:
    • compile, runtime, provided: module source and test execution data is included in the aggregated report;
    • test: only test execution data is considered for the aggregated report.
  3. Add the property maven.deploy.skip, to avoid deploying this module with the rest of the project.
  4. Leave the 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>

Parent module

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>

Test

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

This website uses cookies. More information about the use of cookies is available in the cookies policy.
Accept