12

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.

Hilikus
  • 9,954
  • 14
  • 65
  • 118
John
  • 2,445
  • 2
  • 17
  • 25

2 Answers2

12

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.

Community
  • 1
  • 1
markdsievers
  • 7,151
  • 11
  • 51
  • 83
  • Thanks....I followed the steps mentioned in your detailed answer..Finally It Worked :) – John Sep 22 '16 at 11:59
1

In addition to the steps suggested in markdsievers detailed answer, I had to setup sonarqube-5.3 (supports jdk 7+) in localhost:9000

Setup SonarQube

And use mvn package to generate jacoco.exec files. Then mvn sonar:sonar to generate report in sonar dashboard.

John
  • 2,445
  • 2
  • 17
  • 25