Maven Jacoco Configuration for multi-module projects

John picture John · Sep 22, 2016 · Viewed 16.6k times · Source

I was trying to generate code coverage reports using jacoco plugin in maven for a multi module project that I was working on.

I added the following in my parent pom.xml within the build tags.

      <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.8-SNAPSHOT</version>
            <configuration>
                <output>file</output>
                <append>true</append>
            </configuration>
            <executions>
                <execution>
                    <id>jacoco-initialize</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>jacoco-site</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
            <configuration>
                <argLine>${argLine}</argLine>
            </configuration>
        </plugin>

On running mvn verify, the respective jacoco reports were generated for each module at "project-root\module\target\site\jacoco\"

Is it possible to generate a consolidated jacoco report at the project-root containing the test coverage details of each module?

Please suggest the best possible way to merge the individual module reports.

Answer

markdsievers picture markdsievers · Sep 22, 2016

Sure is!

Took me a while and a few sources to cook this pattern up, but has worked well.

For a multi-module Maven project:

ROOT
|--LIB-1
|--LIB-2

The LIB projects both have their own unit tests.

ROOT pom.xml

<!- properties-->
<jacoco.reportPath>${project.basedir}/../target/jacoco.exec</jacoco.reportPath>

<!-- build/plugins (not build/pluginManagement/plugins!) -->
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.6.201602180812</version>
    <executions>
        <execution>
            <id>agent-for-ut</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <append>true</append>
                <destFile>${jacoco.reportPath}</destFile>
            </configuration>
        </execution>
    </executions>
</plugin>

LIB projects pom.xml will inherit the the JaCoCo plugins execution, so just need to wire up the argline in the Surefire plugin.

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <configuration>
        <argLine>${argLine}</argLine>
    </configuration>
 </plugin>

I have an extended answer for rolling up integration tests as well as unit tests for JaCoCo being reported via Sonar, you can see my detailed answer here.