Jacoco Maven multi module project coverage
One problem in multimodule projects is caused, if the aggregator pom is used as parent pom for the modules either, like it is the case in the above example:
- parentAggregator pom
---sub module A pom.xml -> parentAggregator pom
---sub module B pom.xml -> parentAggregator pom
---sub module C pom.xml -> parentAggregator pom
In this case, the build order is:
- parentAggregator
- sub module A
- sub module B
- sub module C
which means, that the parent aggregator can not collect complete information. In my case a transfer of data into sonarQube by mvn sonar:sonar resulted in unexpected and uncomplete results.
Changing the module structure to:
- aggregator pom
-- parent pom
---sub module A pom.xml -> parent pom
---sub module B pom.xml -> parent pom
---sub module C pom.xml -> parent pom
will change the build order to:
- parent
- sub module A
- sub module B
- sub module C
- aggregator
In this case aggregator will be the last one and work with the results of the modules. In my case the results in SonarQube were like expected.
JaCoCo version 0.7.7 can generate an aggregate coverage report from multiple Maven modules through a new goal jacoco:report-aggregate
.
After scanning many solutions I created a simple but complete Jacoco demo project showing:
- Multi module project
- Unit test (via
mvn clean install
) - Integration test (via
mvn clean install -P integration-test
) - Jacoco - test coverage ( both aggregate data file and aggregate reporting)
- FindBugs - code quality
Enjoy the simple demo project. In this simple project the README.md file contains information you are looking for. An example is:
The simple demo project contains 3 branches:
- Master branch - containing the above functionality
- Multi-module-only-unit-tests - contains modules with only unit tests
- Multi-module-unit-tests-try2 - contains modules with unit tests, differently.
Follow below-mentioned instructions
- Create a new sub-project (usually called maven module). This will be used as report aggregator.
parent pom will be like:
<modules>
<module>A</module>
<module>B</module>
<module>C</module>
<module>ReportAggregator</module>
</modules>
- In aggregator module pom- add other subprojects dependencies.
<dependency>
<groupId>xyz</groupId>
<artifactId>A</artifactId>
<version>${project.version}</version>
</dependency>
- In aggregator module pom- configure jacoco plugin
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report-aggregate</id>
<phase>verify</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
<configuration>
<dataFileIncludes>
<dataFileInclude>**/jacoco.exec</dataFileInclude>
</dataFileIncludes>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
- In aggregator module pom- configure surefire plugin as
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>**/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
- (optional step) If anybody face warning/error like: Classes in bundle '*' do no match with execution data. For report generation, the same class files must be used as at runtime.**
Then add below mentioned lines in aggregator module pom
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
<executions>
<execution>
<id>instrument-ut</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>restore-ut</id>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report-aggregate</id>
<phase>verify</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
<configuration>
<dataFileIncludes>
<dataFileInclude>**/jacoco.exec</dataFileInclude>
</dataFileIncludes>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
- run
mvn clean install