简体   繁体   中英

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

I have a multi-module Gradle build that compiles Scala code. I would like to scan the code, collect code coverage and feed the results into Sonarqube.

My "Hello, World!" for this question is located here .

I am runnig the following command:

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

When I open /java/opensource/gradle/gradle-scoverage-multi-module/build/scoverage-aggregate/index.html , I can see:

截图1

There are clearly code coverage numbers in there.

So, then I run the sonar-scanner in order to send the information to 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/

In the example above, I am running it against the default Docker image from https://github.com/mwz/sonar-scala-docker , but I have also tried this with local installations of Sonarqube 6.7.6 with sonar-scala_2.12-6.7.0-assembly.jar and Sonar 7.4 with sonar-scala_2.12-7.3.1-assembly.jar .

In the image below, there are no code coverage numbers (in either version of the above-mentioned Sonarqubes).

没有代码覆盖

I have valid reported code issues though, (as expected).

What am I doing wrong and why aren't there any code coverage numbers? Is this only supported for SBT? Am I not invoking the right Gradle targets? Am I missing parameters?

As you can see here on sonar-scala's instance of Sonarqube, there's clearly a "Coverage" section showing some values (which I am not getting):

有效的Coverage部分是什么样的

Could somebody, please shed some light on this? I've been trying to get this right for a very long time. If it's a bug, or unfinished features, then finding this out would also be helpful. If it's only supported in SBT, then it would be good to know.

Either way, I'd really appreciate the help! Thanks!

To answer my question, two things:

  • There are now examples in the sonar-scala project for both single and multi-module projects here .
  • If executing the build under Windows, the code coverage will not work, due to open issues ( 64 and #65 ) with sonar-scala.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM