问题
After we upgraded our SonarQube to Version 7.9.2 (build 30863), Community Edition
, running the GitLab CI Pipeline results in showing 0.0%
coverage (dropping from about 86.2%
), although Sonar is showing all unit tests.
Maven build executed in .gitlab-ci.yml
looks like this:
- mvn test sonar:sonar -Dsonar.host.url=${SONAR_HOST_URL} -Dsonar.login=${SONAR_TOKEN}
Obviosuly this has something to deal with notorious sonar.coverage.jacoco.xmlReportPaths
and sonar.jacoco.reportPaths
Sonar-JaCoCo plugin settings (see https://docs.sonarqube.org/pages/viewpage.action?pageId=1442166).
After looking into the pipeline jobs logs, I found the following (project name obfuscated to myProject
):
Before SonarQube upgrade:
[INFO] Sensor JaCoCo XML Report Importer [jacoco]
[INFO] Sensor JaCoCo XML Report Importer [jacoco] (done) | time=3ms
[INFO] Sensor SurefireSensor [java]
[INFO] parsing [/builds/myProject/target/surefire-reports]
[INFO] Sensor SurefireSensor [java] (done) | time=87ms
[INFO] Sensor JaCoCoSensor [java]
[WARNING] Property 'sonar.jacoco.reportPaths' is deprecated (JaCoCo binary format). 'sonar.coverage.jacoco.xmlReportPaths' should be used instead (JaCoCo XML format). Please check that the JaCoCo plugin is installed on your SonarQube Instance.
[INFO] Analysing /builds/myProject/target/jacoco.exec
[INFO] Sensor JaCoCoSensor [java] (done) | time=206ms
After SonarQube upgrade:
[INFO] Sensor JaCoCo XML Report Importer [jacoco]
[INFO] Sensor JaCoCo XML Report Importer [jacoco] (done) | time=3ms
[INFO] Sensor SurefireSensor [java]
[INFO] parsing [/builds/myProject/target/surefire-reports]
[INFO] Sensor SurefireSensor [java] (done) | time=86ms
[INFO] Sensor JavaXmlSensor [java]
[INFO] 1 source files to be analyzed
[INFO] Sensor JavaXmlSensor [java] (done) | time=302ms
Therefore the are obviously some differences in regards to sonar.jacoco.reportPaths
setting.
Notably, I don't explicitly set any of the two settings.
How to fix parsing of the JaCoCo report so that it works (regardless with old or new setting)?
回答1:
Quick answer
Replace mvn test
with mvn verify
. No explicit settings declaration required.
My investigation process
I've tried to execute the builds with the explicit setting values default settings.
1) Explicit default value of the deprecated setting: -Dsonar.jacoco.reportPaths=target/jacoco.exec
.
This results in the following log, indicating that this setting won't work anymore for the new Sonar:
[INFO] Sensor JaCoCo XML Report Importer [jacoco] (done) | time=57ms
[INFO] Sensor SurefireSensor [java]
[INFO] parsing [/builds/myProject/target/surefire-reports]
[INFO] Sensor SurefireSensor [java] (done) | time=23ms
[INFO] Sensor Removed properties sensor [java]
[WARNING] Property 'sonar.jacoco.reportPaths' is no longer supported. Use JaCoCo's xml report and sonar-jacoco plugin.
[INFO] Sensor Removed properties sensor [java] (done) | time=1ms
Obviously, the coverage stays at 0.0%
.
See also a SO related Ant question on this topic: Display Sonar Code Coverage with jacoco.exec file in Sonar LTS 7.9.2.
2) Explicit default value of the new setting: -Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml
Note: although this default value is not (by some reason) documented on https://docs.sonarqube.org/pages/viewpage.action?pageId=1442166, I've just found it by searching for jacoco.xml
in my local target
directory.
Coverage stayed at 0.0%
, and the GitLab CI job log started to finally show something clear:
[INFO] Sensor SonarCSS Rules [cssfamily] (done) | time=1ms
[INFO] Sensor JaCoCo XML Report Importer [jacoco]
[WARNING] Report doesn't exist: '/builds/myProject/target/site/jacoco/jacoco.xml'
[INFO] Sensor JaCoCo XML Report Importer [jacoco] (done) | time=4ms
[INFO] Sensor SurefireSensor [java]
! It is a pity that this log is not written when running without the explicit settings. It will simplify the investigation a lot.
So now we have an understanding: /target/site
is not generated on my GitLab CI pipeline. Obviously, this is because the script contains only mvn test
, which does not get far enough through the Maven Build Lifecycle.
Looking into Maven + Sonar examples: they contain mvn verify sonar:sonar
or mvn install sonar:sonar
.
Therefore here we go:
3) mvn verify
+ explicit default value of the new setting: -Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml
This worked! Coverage is shown on SonarQube and no errors present in the JaCoCo-related log part:
[INFO] Sensor JaCoCo XML Report Importer [jacoco]
[INFO] Sensor JaCoCo XML Report Importer [jacoco] (done) | time=94ms
[INFO] Sensor SurefireSensor [java]
[INFO] parsing [/builds/myProject/target/surefire-reports]
[INFO] Sensor SurefireSensor [java] (done) | time=92ms
[INFO] Sensor JavaXmlSensor [java]
[INFO] Sensor JavaXmlSensor [java] (done) | time=7ms
4) mvn verify
without explicit setting.
So finally I tried without any settings values, and it works:
- mvn verify sonar:sonar -Dsonar.host.url=${SONAR_HOST_URL} -Dsonar.login=${SONAR_TOKEN}
Summary
- Old
sonar.jacoco.reportPaths
setting does not work on new SonarQube versions (7.9.2+). - Default value of new
sonar.coverage.jacoco.xmlReportPaths
setting istarget/site/jacoco/jacoco.xml
. - The root cause was that
/target/site/jacoco
directory was not generated (includingjacoco.xml
and all other files of the JaCoCo report) - All I have to do is to replace
mvn test
withmvn verify
. - Main Sonar pitfall is not writing an error log about "report file not found" when the value of
sonar.coverage.jacoco.xmlReportPaths
is not explicitly set. It would be good for SonarQube developers to fix it.
来源:https://stackoverflow.com/questions/61235045/sonarqube-maven-jacoco-gitlab-ci-sonar-started-to-show-0-code-coverage-a