SonarQube plugin for static code analysis of Scala projects.
Intended for SonarQube 6.7+ LTS and Scala 2.11/2.12.
This plugin is not an evolution from the legacy sonar-scala-plugin of which versions can be found laying around such as 1and1/sonar-scala. The previous plugin used the scala compiler to create its metrics which had the disadvantage of requiring a specific plugin per each major version of Scala. Instead, this plugin uses the scala-ide/scalariform library to parse the source code in a version independent way.
This plugin currently supports the following SonarQube metrics:
- Number of classes (and objects) -
classes
- Number of lines containing either comments or commented-out code -
comment_lines
- Number of files -
files
- Lines of code -
ncloc
- Number of functions -
functions
- Number of lines of code which could be covered by unit tests -
lines_to_cover
- Number of lines of code which are not covered by unit tests -
uncovered_lines
- Percentage of line coverage -
line_coverage
In addition to the above, the plugin reports the following custom metrics, which are extracted from the Scoverage report:
- Number of all statements -
total_statements
- Number of statements covered by tests -
covered_statements
- Percentage of statement coverage -
scoverage
- Percentage of branch coverage -
branch_scoverage
This plugin integrates 75 quality checks from Scalastyle. 46 of them are quality rules without parameters which work out of the box and the remaining 29 are rule templates that allow you to set up custom rules which can be configured by various parameters.
The rules in the Scalastyle quality profile, created by this plugin, are almost all deactivated. In order to use all of the rules, you should clone the quality profile and you should be able to activate more rules, change rule severity and create more custom rules from the existing templates.
For more information about Scalastyle rules, please consult the upstream documentation.
Download the latest release jar into your SonarQube plugins folder /opt/sonarqube/extensions/plugins
and restart SonarQube either using the update center or manually.
For an out-of-the-box setup, you can use my docker-compose recipe or a docker image with SonarQube LTS which contains bundled sonar-scala and arthepsy/sonar-scala-extra (Scapegoat) plugins. Please see mwz/sonar-scala-docker for more details.
For automating the analysis of your Scala projects, check out my sbt plugin mwz/sbt-sonar.
The plugin exposes the following properties which can be passed to sonar-scanner when running an analysis:
- sonar.sources - Scala source directory relative to the root of your project (usually
src/main/scala
) - sonar.scala.version (optional) - defines the version of Scala used in your project (defaults to
2.11.0
) - sonar.scala.scoverage.reportPath (optional) - relative path to the scoverage report (defaults to
target/scala-${sonar.scala.version}/scoverage-report/scoverage.xml
)
See an example usage:
sonar-scanner -Dsonar.projectName=test \
-Dsonar.projectKey=test \
-Dsonar.sources=src/main/scala \
-Dsonar.sourceEncoding=UTF-8 \
-Dsonar.scala.version=2.12.5 \
-Dsonar.scoverage.reportPath=target/scala-2.12/scoverage-report/scoverage.xml
To build the project from sources, run the assembly
task in sbt shell and the jar assembled with all of the dependencies required by this plugin should be created in the target/scala-2.12
directory.
To debug the plugin, export the following environment variable before running sonar-scanner
:
export SONAR_SCANNER_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=8000"
Once you've done that, sonnar-scanner
should display the following message Listening for transport dt_socket at address: 8000
. You can now attach your IDE to the process on port 8000
, set breakpoints and debug the code.
This project is a continuation of sonar-scala plugin, which was originally developed by Sagacify and integrates code from two other SonarQube plugins Sonar Scalastyle Plugin and Sonar Scoverage Plugin.
Many other projects have been used as an inspiration, here is a list of the main ones:
For a list of changes and releases, please see CHANGELOG.
The project is licensed under the GNU LGPL v3. See the LICENSE file for more details.