I have a maven multi-module java project for which I can’t get the coverage to show on SonarCloud. I followed the instructions here, which suggest adding a project submodule to aggregate the coverage report of other submodules.
My report aggregation maven submodule is called: coverage-report-aggregator
In the parent POM,
I defined sonar.coverage.jacoco.xmlReportPaths
property:
<sonar.coverage.jacoco.xmlReportPaths>
${project.basedir}/coverage-report-aggregator/target/site/jacoco-aggregate/jacoco.xml
</sonar.coverage.jacoco.xmlReportPaths>
and defined the jacoco maven plugin:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
In the report aggregator submodule coverage-report-aggregator
, I added the other submodules as dependencies and added the Jacoco plugin setup:
...
<dependencies>
<dependency>
<groupId>com.myorg</groupId>
<artifactId>module-one</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.myorg</groupId>
<artifactId>module-two</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
...
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>report-aggregate</id>
<phase>verify</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
When I run mvn install
locally against the parent project, the reports get aggregated as expected under: <parent-project>/coverage-report-aggregator/target/site
. Opening the local index.html file shows that both submodules (module-one and module-two) have test coverage as expected.
However after Sonar analysis is run from CI/CD (see Drone setup further below), there is no coverage shown on SonarCloud:
CI/CD logs show that Jacoco report XML file was imported by sonar:
[INFO] 12:03:37.443 Importing 1 report(s). Turn your logs in debug mode in order to see the exhaustive list.
[DEBUG] 12:03:37.443 Reading report '/drone/src/coverage-report-aggregator/target/site/jacoco-aggregate/jacoco.xml'
[INFO] 12:03:37.461 Sensor JaCoCo XML Report Importer [jacoco] (done) | time=18ms
I’ve also checked that the aggregate XML report file existed on the CI/CD server, and it size matched the size of the equivalent one I got generated on my local machine:
+ ls -l ./coverage-report-aggregator/target/site/jacoco-aggregate/jacoco.xml
-rw-r--r--. 1 root root 196570 Nov 2 12:03 ./coverage-report-aggregator/target/site/jacoco-aggregate/jacoco.xml
I hope someone can help me get to the root cause of this problem.
Thanks in advance.
More details about the setup
- ALM used: GitHub
- CI system used: Drone
- Scanner command used:
steps:
- name: build
image: maven:3.8-openjdk-17
commands:
- mvn clean install
- name: sonar
image: maven:3.8-openjdk-17
depends_on:
- build
environment:
SONAR_HOST:
from_secret: sonar_cloud_host
SONAR_TOKEN:
from_secret: sonar_cloud_token
commands:
- mvn -X sonar:sonar
-Dsonar.host.url=$${SONAR_HOST}
-Dsonar.login=$${SONAR_TOKEN}
-Dsonar.organization=myOrganisation
-Dsonar.projectKey=parent-project
-Dsonar.branch.name=$DRONE_BRANCH
-Dsonar.projectName=parent-project
when:
event:
exclude:
- pull_request
- Languages of the repository: Java
P.S.
I’ve tried the solution mentioned by this post but it didn’t work. It sounds a bit irrational anyway.