我有一个编译 Scala 代码的多模块 Gradle 构建。我想扫描代码,收集代码覆盖率并将结果输入 Sonarqube。
我的“你好,世界!” 对于这个问题位于此处。
我正在运行以下命令:
$ ./gradlew clean build reportScoverage aggregateScoverage
Starting a Gradle Daemon (subsequent builds will be faster)
> Task :a:compileScala
Pruning sources from previous analysis, due to incompatible CompileSetup.
> Task :a:compileTestScala
Pruning sources from previous analysis, due to incompatible CompileSetup.
> Task :b:compileScala
Pruning sources from previous analysis, due to incompatible CompileSetup.
> Task :b:compileTestScala
Pruning sources from previous analysis, due to incompatible CompileSetup.
> Task :a:compileScoverageScala
Pruning sources from previous analysis, due to incompatible CompileSetup.
> Task :a:reportScoverage
[scoverage] Generating scoverage reports...
[scoverage] Written Cobertura XML report to /java/opensource/gradle/gradle-scoverage-multi-module/a/build/reports/scoverage/cobertura.xml
[scoverage] Written XML report to /java/opensource/gradle/gradle-scoverage-multi-module/a/build/reports/scoverage/scoverage.xml
[scoverage] Written HTML report to /java/opensource/gradle/gradle-scoverage-multi-module/a/build/reports/scoverage/index.html
[scoverage] Coverage reports completed
> Task :b:compileScoverageScala
Pruning sources from previous analysis, due to incompatible CompileSetup.
> Task :b:reportScoverage
[scoverage] Generating scoverage reports...
[scoverage] Written Cobertura XML report to /java/opensource/gradle/gradle-scoverage-multi-module/b/build/reports/scoverage/cobertura.xml
[scoverage] Written XML report to /java/opensource/gradle/gradle-scoverage-multi-module/b/build/reports/scoverage/scoverage.xml
[scoverage] Written HTML report to /java/opensource/gradle/gradle-scoverage-multi-module/b/build/reports/scoverage/index.html
[scoverage] Coverage reports completed
> Task :aggregateScoverage
[info] Found 2 subproject report files [/java/opensource/gradle/gradle-scoverage-multi-module/a/build/reports/scoverage/scoverage.xml,/java/opensource/gradle/gradle-scoverage-multi-module/b/build/reports/scoverage/scoverage.xml]
[scoverage] Generating scoverage reports...
[scoverage] Written Cobertura XML report to /java/opensource/gradle/gradle-scoverage-multi-module/build/scoverage-aggregate/cobertura.xml
[scoverage] Written XML report to /java/opensource/gradle/gradle-scoverage-multi-module/build/scoverage-aggregate/scoverage.xml
[scoverage] Written HTML report to /java/opensource/gradle/gradle-scoverage-multi-module/build/scoverage-aggregate/index.html
[scoverage] Coverage reports completed
BUILD SUCCESSFUL in 13s
21 actionable tasks: 21 executed
当我打开/java/opensource/gradle/gradle-scoverage-multi-module/build/scoverage-aggregate/index.html
时,我可以看到:
那里有明确的代码覆盖率数字。
所以,然后我运行sonar-scanner
以将信息发送到 Sonarqube:
$ sonar-scanner -Dsonar.projectName=multi-module \
-Dsonar.projectKey=multi-module \
-Dsonar.projectVersion=1.0-SNAPSHOT \
-Dsonar.sources=src/main/scala \
-Dsonar.modules=a,b \
-Dsonar.sourceEncoding=UTF-8 \
-Dsonar.scala.version=2.11 \
-Dsonar.scala.scoverage.reportPath=build/scoverage-aggregate/scoverage.xml \
-Dsonar.host.url=http://localhost:80/
在上面的示例中,我针对来自 的默认 Docker 映像运行它https://github.com/mwz/sonar-scala-docker
,但我也尝试使用本地安装的 Sonarqube 6.7.6
withsonar-scala_2.12-6.7.0-assembly.jar
和 Sonar 7.4
with sonar-scala_2.12-7.3.1-assembly.jar
。
在下图中,没有代码覆盖率数字(在上述 Sonarqube 的任一版本中)。
不过,我有有效的报告代码问题(如预期的那样)。
我做错了什么,为什么没有任何代码覆盖率?这仅支持 SBT 吗?我没有调用正确的 Gradle 目标吗?我缺少参数吗?
正如您在 sonar-scala 的 Sonarqube 实例中看到的那样,显然有一个“覆盖”部分显示了一些值(我没有得到):
有人可以解释一下吗?很长一段时间以来,我一直在努力做到这一点。如果它是一个错误或未完成的功能,那么找出这一点也会有所帮助。如果它仅在 SBT 中受支持,那么很高兴知道。
无论哪种方式,我都非常感谢您的帮助!谢谢!