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:
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):
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.
Related
I'm facing a weird issue while generating coverage report for a Scala 3 project.
Following is an example of how report looks like a method:
because of some reason it is marking statements as uncovered (especially when they are in for comprehension )
NOTE: s3.list returns Try[Vector[S3ObjectSummary]] and my test-case generates both failure and success case.
I'm using sbt-scoverage plugin (v 2.0.0). My scala version is 3.2.2.
Report is generated via sbt clean coverage test coverageReport command.
I have a single-module Gradle build that compiles Scala code. I would like to scan the code, collect code coverage and feed the results into Sonarqube.
I am running the below command:
gradle --no-daemon -Dsonar.host.url=http://localhost:9000 -Dsonar.login=sqp_b*****7575 -Dsonar.sources=src/main/scala -Dsonar.scoverage.reportPath=central-intg-validation-core/build/reports/scoverage/scoverage.xml clean test reportScoverage sonarqube
which is resulting the output in the command line as below:
To honour the JVM settings for this build a single-use Daemon process will be forked. See https://docs.gradle.org/7.4.2/userguide/gradle_daemon.html#sec:disabling_the_daemon.
Daemon will be stopped at the end of the build
> Task :central-intg-validation-core:reportTestScoverage
[info] Found 1 subproject scoverage data directories [central-intg-validation/central-intg-validation-core/build/scoverage]
> Task :central-intg-validation-core:reportScoverage
[info] Found 1 subproject scoverage data directories [/central-intg-validation/central-intg-validation-core/build/scoverage]
BUILD SUCCESSFUL in 25s
10 actionable tasks: 10 executed
When I open central-intg-validation/central-intg-validation-core/build/scoverage-aggregate/index.html, I can see:
here are clearly code coverage numbers in there.
So, when I run the sonar-scanner in order to send the information to Sonarqube, I don't see anything the sonarqube as below.
What am I doing wrong and why aren't there any code coverage numbers? 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.
I have configured one SBT multi-module project with scoverage plugin, which is working fine.
To generate test coverage, I am using > SBT clean coverage test coverageReport but is there any way to create a new task which chains internally coverage test coverageReport.
I have tried
Run custom task automatically before/after standard task to create a custom task, but it seems not working with multimodule project.
And one more - http://eed3si9n.com/sequencing-tasks-with-sbt-sequential
Try addCommandAlias like so
addCommandAlias("coverageAll", ";clean;coverage;test;coverageReport")
Now executing sbt coverageAll should generate coverage report for all the sub-projects.
I want to do test coverage on my Scala project, for which I use Maven as a build tool.
So I found this:
https://github.com/scoverage/scoverage-maven-plugin
And I looked here:
http://scoverage.github.io/scoverage-maven-plugin/1.3.0/check-mojo.html
So now to check test coverage, I run this:
mvn test
And then:
mvn scoverage:check
However, this only makes the tests to be run. I get no information about coverage.
Also, I tried:
mvn scoverage:report
But the result is the same.
So how can I use this tool or another to get test coverage info in a Scala/Maven project?
I have only used Scoverage with SBT, but chances are the usage is the same.
mvn scoverage:check will only generate some metadata - XML - and compare the generated coverage values against any coverage minimum you might have set up.
With mvn scoverage:report you will get some formatted reports. More docs here.
I would like to know whats the easiest way to plug JaCoCo4sbt's data into Sonar,
In Jenkins I have installed Sonar & JaCoCo's plugins. I have also installed JaCoCo's plugin in Sonar.
My sonar-project.properties file contains :
sonar.jacoco.reportPath=target/jacoco/jacoco.exec
And Jenkins's job execute these commands :
sbt jacoco:cover
/opt/sonar-runner/bin/sonar-runner
SBT_OPTS="-Dsbt.log.noformat=true"
sbt clean update compile test doc
For now I don't get any code coverage data into Sonar
Do you want to report code coverage on Scala code using the Scala plugin for Sonar (http://docs.codehaus.org/display/SONAR/Scala+Plugin)?
Unfortunately it does not yet provide a sensor for code coverage.
It's on the roadmap for future versions.
At least jacoco4sbt successfully generates the file jacoco.exec but it is just not picket up by the Scala plugin.
You'll need the following properties:
sonar.dynamicAnalysis=reuseReports
sonar.java.coveragePlugin=jacoco
sonar.jacoco.reportPath=${build.dir}/jacoco.exec
I don't use sbt, but the following is an ANT example:
Add ant plugins dynamically at buildtime?
Check the properties file at the end for all the Sonar related stuff.