Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Jacoco Configuration for multi-module projects [duplicate]

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.

like image 271
John Avatar asked Sep 22 '16 08:09

John


2 Answers

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.

like image 122
markdsievers Avatar answered Oct 22 '22 16:10

markdsievers


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.

like image 33
John Avatar answered Oct 22 '22 15:10

John