This blog is all about configuring scoverage plugin with SonarQube for tracking statement coverage as well as static code analysis for Scala project. SonarQube has support for many languages but it doesn’t have support for Scala- so this blog will guide through configuring sonar-scala and scoverage plugins to generate code analysis and code coverage reports.
The scoverage plugin for SonarQube reads the coverage reports generated by sbt coverage test and displays those in sonar dashboard.
Here are the steps to configure Scala projects with SonarQube for code coverage as well as static code analysis.
- Install sonarqube and start the server.
- Go to the Sonarqube marketplace and install `SonarScala` plugin.
This plugin provides static code analyzer for Scala language. It supports all the standard metrics implemented by sonarQube including Cognitive complexity.
- Add `Scoverage` plugin to Sonarqube from the marketplace
This plugin provides the ability to import statement coverage generated by Scoverage for scala projects. Also, this plugin reads XML report generated by Scoverage and populates several metrics in Sonar.
i. SonarQube 5.1
ii. Scoverage 1.1.0
4. Now add the `sbt-sonar` plugin dependency to your scala project addSbtPlugin(“com.github.mwz” % “sbt-sonar” % “1.6.0”)
This sbt plugin can be used to run sonar-scanner launcher to analyze a Scala project with SonarQube.
i. sbt 0.13.5+
ii. Scala 2.11/2.12
iii. SonarQube server.
iv. sonar-scanner (See point#5 for installation)
5. Configure `sonar-scanner` executable
- Download sonar scanner from
- Define `SONAR_SCANNER_HOME` environment variable pointing to `sonar-scanner` installation directory
- Update ` $SONAR_SCANNER_HOME/conf/sonar-scanner.properties` file to point to your sonarQube instance.
6. Now, configure the sonar-properties in your project. This can be done in 2 ways
- Use sonar-project.properties file:
This file has to be placed in your root directory. To use an external config file you can set the sonarUseExternalConfig to true.
sonarUseExternalConfig := true
- Configure Sonar-properties in build file:
- By default, the plugin expects the properties to be defined in the sonarProperties setting key in sbt
import sbtsonar.SonarPlugin.autoImport.sonarProperties sonarProperties ++= Map( "sonar.sources" -> "src/main/scala", "sonar.tests" -> "src/test/scala", "sonar.modules" -> "module1,module2")
- Now run the below commands to publish code analysis and code coverage reports in your sonarQube server.
- sbt coverage test
- sbt coverageReport
- sbt sonarScan
SonarQube integration is really useful to perform an automatic review of code to detect bugs, code smells and security vulnerabilities. SonarQube can also track history and provide the visual representation of it.