【问题标题】:How to get Gradle, Scoverage and Sonarqube to work properly?如何让 Gradle、Scoverage 和 Sonarqube 正常工作?
【发布时间】:2019-07-17 19:35:30
【问题描述】:

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

我的“你好,世界!”这个问题位于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 映像运行它,但我也尝试使用带有 sonar-scala_2.12-6.7.0-assembly.jar 的 Sonarqube 6.7.6 和带有 sonar-scala_2.12-7.3.1-assembly.jar 的 Sonar 7.4 的本地安装。

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

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

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

您可以在 sonar-scala 的 Sonarqube 实例上看到 here,显然有一个“覆盖”部分显示了一些值(我没有得到):

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

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

【问题讨论】:

  • 此时,覆盖率传感器在模块级别上工作,因此您不需要构建聚合报告(但在未来的版本中可能会改变)。无论如何,我相信在plugin repository 上打开一个issue 会更好。
  • 感谢您的建议!我提出了github.com/mwz/sonar-scala/issues/154。我有一种我配置错误的感觉。 sonar-scala 项目似乎是使用 SBT 构建的,现在这让我有点担心 scoverage 插件真正支持什么以及它的工作程度......

标签: scala gradle sonarqube scoverage


【解决方案1】:

回答我的问题,有两点:

  • 现在sonar-scala 项目中提供了单模块和多模块项目here 的示例。
  • 如果在 Windows 下执行构建,由于 sonar-scala 的未解决问题(64#65),代码覆盖率将不起作用。

【讨论】:

    猜你喜欢
    • 2016-08-09
    • 2015-05-08
    • 2020-08-27
    • 2018-06-11
    • 2016-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多