I am trying to create unit test sonar code coverage data for a multi-module maven project and not get the correct results.
The structure of the project is similar to the following:
- Project
- parentPom
- Modulea
- moduleB
- module C
I use the jacoco maven plugin to generate jacoco.exec files and the maven sonar plugin to analyze the code (including jacoco.exec files).
These files are created in the process-tests phase as follows:
<properties> <jacoco.report.path>${project.build.directory}/jacoco.exec</jacoco.report.path> <sonar.jacoco.reportPath>${jacoco.report.path}</sonar.jacoco.reportPath> </properties> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.7.4.201502262128</version> <executions> <execution> <id>default-prepare-agent</id> <phase>process-test-classes</phase> <goals> <goal>prepare-agent</goal> </goals> <configuration> <destFile>${jacoco.report.path}</destFile> </configuration> </execution> </executions> </plugin>
When I run mvn clean install , I see that a jacoco.exec file has been created for each module:
$ find . -name '*.exec' ./moduleA/target/jacoco.exec ./moduleB/moduleB1/target/jacoco.exec ./moduleB/moduleB2/target/jacoco.exec ./moduleC/target/jacoco.exec
When I run the mvn sonar: sonar, I see that the Jacoco sensor works for all modules, but it seems to work only with the first module. The following modules show Coverage information was not collected :
[INFO] [17:13:58.333] Sensor JaCoCoSensor [INFO] [17:13:58.350] Analysing moduleA\target\jacoco.exec [INFO] [17:13:58.374] No information about coverage per test. [INFO] [17:13:58.374] Sensor JaCoCoSensor (done) | time=41ms ... [INFO] [17:14:02.202] Sensor JaCoCoSensor [INFO] [17:14:02.261] Analysing moduleB\moduleB1\target\jacoco.exec [WARN] [17:14:02.334] Coverage information was not collected. Perhaps you forget to include debug information into compiled classes? [INFO] [17:14:02.334] Sensor JaCoCoSensor (done) | time=132ms ...
I'm not sure why there is no coverage information in the second and subsequent modules, since the maven-compiler-plugin includes debugging information by default, and to be safe, I also ran mvn clean install -Dmaven.compiler.debug=true but got the same results.
As a result of this, when I test the project on the sonar server, it shows code coverage only for the first module: moduleA . No code coverage information is available for other modules.
Apparently the solution here is to generate only one jacoco.exec file, so when the jacoco-maven-plugin executes it, it adds the result for each module to this file so that the sonar can work with its magic correctly .
Accordingly, I modified the parentPom/pom.xml as follows:
<properties> <jacoco.report.path>${session.executionRootDirectory}/code-coverage/jacoco.exec</jacoco.report.path> <sonar.jacoco.reportPath>${jacoco.report.path}</sonar.jacoco.reportPath> </properties> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.7.4.201502262128</version> <executions> <execution> <id>default-prepare-agent</id> <phase>process-test-classes</phase> <goals> <goal>prepare-agent</goal> </goals> <configuration> <destFile>${jacoco.report.path}</destFile> <append>true</append> </configuration> </execution> </executions> </plugin>
This means that after running unit tests, the jacaco agent is activated. Now when I run mvn clean install , I see only one jacoco.exec file:
$ find . -name '*.exec' ./code-coverage/target/jacoco.exec
But when I run mvn sonar: sonar, JaCoCoSensor does not seem to be called, and the project on the sonar server does not have code coverage at all.
What am I doing wrong here? How to get sonar to analyze code coverage for all modules in my maven project?
Do I need to change the maven-surefire-plugin ?
I am using SonarQube 5.1, JDK 1.8, jacoco-maven-plugin 0.7.4.201502262128