如何让Gradle、Scoverage和Sonarqube正常工作?

How to get Gradle, Scoverage and Sonarqube to work properly?

我有一个编译 Scala 代码的多模块 Gradle 构建。我想扫描代码,收集代码覆盖率并将结果输入 Sonarqube。

我对这个问题的 "Hello, World!" 位于 here

我正在运行执行以下命令:

$ ./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/

在上面的示例中,我运行将其与 https://github.com/mwz/sonar-scala-docker 中的默认 Docker 图像相对照,但我也尝试过使用本地安装的 Sonarqube 6.7.6sonar-scala_2.12-6.7.0-assembly.jar 和声纳 7.4sonar-scala_2.12-7.3.1-assembly.jar.

在下图中,没有代码覆盖率数字(在上述 Sonarqubes 的任一版本中)。

虽然我报告了有效的代码问题,(正如预期的那样)。

我哪里做错了,为什么没有任何代码覆盖率数字?这仅支持 SBT 吗?我没有调用正确的 Gradle 目标吗?我缺少参数吗?

正如您在 Sonar-scala 的 Sonarqube 实例上看到的 here,显然有一个 "Coverage" 部分显示了一些值(我没有得到):

有人可以解释一下吗?很长一段时间以来,我一直在努力解决这个问题。如果它是错误或未完成的功能,那么找出它也会有所帮助。如果它仅在 SBT 中受支持,那么很高兴知道。

无论哪种方式,我都非常感谢您的帮助!谢谢!

为了回答我的问题,有两件事:

  • sonar-scala 项目中现在有针对单个项目和 multi-module 项目的示例 here
  • 如果在 Windows 下执行构建,由于 sonar-scala.
  • 的未解决问题 (64 and #65),代码覆盖将不起作用