问题描述
在将SonarQube升级到Version 7.9.2 (build 30863), Community Edition
之后,尽管Sonar正在显示所有单元测试,但运行GitLab CI Pipeline仍会显示0.0%
覆盖率(从大约86.2%
下降).
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.
在.gitlab-ci.yml
中执行的Maven构建如下所示:
Maven build executed in .gitlab-ci.yml
looks like this:
- mvn test sonar:sonar -Dsonar.host.url=${SONAR_HOST_URL} -Dsonar.login=${SONAR_TOKEN}
这显然可以解决臭名昭著的sonar.coverage.jacoco.xmlReportPaths
和sonar.jacoco.reportPaths
Sonar-JaCoCo插件设置(请参阅 https://docs.sonarqube.org/pages/viewpage.action?pageId=1442166 ).
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).
查看管道作业日志后,发现以下内容(项目名称混淆为myProject
):
After looking into the pipeline jobs logs, I found the following (project name obfuscated to myProject
):
SonarQube升级之前:
[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
SonarQube升级后:
[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
因此,关于sonar.jacoco.reportPaths
设置显然存在一些差异.值得注意的是,我没有明确设置这两个设置中的任何一个.
Therefore the are obviously some differences in regards to sonar.jacoco.reportPaths
setting.Notably, I don't explicitly set any of the two settings.
如何解决JaCoCo报告的解析问题,使其能够正常工作(无论使用旧设置还是新设置)?
How to fix parsing of the JaCoCo report so that it works (regardless with old or new setting)?
推荐答案
快速解答
用mvn verify
替换mvn test
.无需明确的设置声明.
Quick answer
Replace mvn test
with mvn verify
. No explicit settings declaration required.
我尝试使用显式设置值默认设置执行构建.
I've tried to execute the builds with the explicit setting values default settings.
1):不建议使用的设置的默认默认值:-Dsonar.jacoco.reportPaths=target/jacoco.exec
.
1) Explicit default value of the deprecated setting: -Dsonar.jacoco.reportPaths=target/jacoco.exec
.
这将导致以下日志,表明该设置将不再适用于新的Sonar:
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
很明显,覆盖范围保持在0.0%
.
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)新设置的默认默认值:-Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml
2) Explicit default value of the new setting: -Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml
注意:尽管(由于某种原因)该默认值未记录在 https://docs.sonarqube.org/pages/viewpage.action?pageId=1442166 ,我是通过在本地target
目录中搜索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.
覆盖率保持在0.0%
,并且GitLab CI作业日志开始终于显示清楚了:
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.
因此,现在我们有了一个了解:/target/site
不是在我的GitLab CI管道上生成的.显然,这是因为该脚本仅包含mvn test
,而通过 Maven构建生命周期.
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.
以Maven + Sonar为例:它们包含mvn verify sonar:sonar
或mvn install sonar:sonar
.
Looking into Maven + Sonar examples: they contain mvn verify sonar:sonar
or mvn install sonar:sonar
.
因此,我们开始:
3) mvn verify
+新设置的明确默认值:-Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml
3) mvn verify
+ explicit default value of the new setting: -Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml
这行得通!覆盖率显示在SonarQube上,与JaCoCo相关的日志部分中没有错误:
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
无需显式设置.
所以最后我尝试了没有任何设置值的方法,并且有效:
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}
摘要
- 旧的
sonar.jacoco.reportPaths
设置不适用于新的SonarQube版本(7.9.2+). - 新的
sonar.coverage.jacoco.xmlReportPaths
设置的默认值为target/site/jacoco/jacoco.xml
. - 根本原因是未生成
/target/site/jacoco
目录(包括jacoco.xml
和JaCoCo报告的所有其他文件) - 我要做的就是将
mvn test
替换为mvn verify
. - 当未明确设置
sonar.coverage.jacoco.xmlReportPaths
的值时,主要的Sonar陷阱没有写入有关未找到报告文件"的错误日志. SonarQube开发人员对其进行修复将是一件好事. - 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.
Summary
这篇关于SonarQube + Maven + JaCoCo + GitLab CI:在升级到SonarQube 7.9.2后,Sonar开始显示0%的代码覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!