如何使Gradle,Scoverage和Sonarqube正常工作?

时间:2019-02-24 03:53:23

标签: scala gradle sonarqube scoverage

我有一个编译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时,我可以看到:

Screenshot 1

其中显然有代码覆盖率编号。

所以,然后我运行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和Sonar {{ 1}}和7.4

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

No code coverage

尽管如此,我还是报告了有效的代码问题。

我在做什么错,为什么没有代码覆盖率数字?仅SBT支持此功能吗?我不是在调用正确的Gradle目标吗?我是否缺少参数?

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

What a valid Coverage section looks like

请问有人可以阐明这一点吗?我一直在努力解决这个问题。如果这是一个错误或未完成的功能,那么找出这一点也将有所帮助。如果仅在SBT中受支持,那么很高兴知道。

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

1 个答案:

答案 0 :(得分:0)

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

  • sonar-scala项目中现在有单模块项目和多模块项目here的示例。
  • 如果在Windows下执行构建,由于声纳标度存在开放问题(64#65),因此代码覆盖将无法正常工作。