Configuring SonarQube with Scoverage plug-in : The Complete Guide


This blog will guide you through the successful configuration of Scoverage plug-in with SonarQube for Scala source code statement coverage analysis.

How Does it Work?

The Scoverage plug-in for SonarQube reads the report generated by sbt scoverage plug-in and generate several reports like

  • Statement Coverage % Analysis
  • Lines covered by test
  • Drilling down report to the file level

The greatest advantage of SonarQube is the support for several programming languages for code quality rules. Unfortunately, it doesn’t support Scala. That is why scoverage plug-in exists for SonarQube. After adding Scoverage plug-in SonarQube will add Scala-specific rules for statement coverage. It can also support multi-language projects like Scala with Java and so on.

The Ingredients

The configuration requires the following plug-ins/pre-installs:-

1. SonarQube

SonarQube is a code quality management tool which provides great code analysis features with numerous  language support. If you don’t have SonarQube installed on your machine follow these steps to get it:-

  • Download the latest SonarQube from here.
  • Extract the zip in your favorite directory. Call it <extract_directory>
  • Go to and open/switch to the extracted folder of SonarQube.You must have the following directory structure:-

SonarQubeExtractFolder

  • Switch to bin directory. This directory contains several build directories for different OS s.
  • Select appropriate build directory according to your machine configuration. I selected linux-x86-64 for my Ubuntu 14.04 LTS.
  • Add this directory location in your PATH variable. If you are on Windows, create a new environment variable for this directory location and add it to the class path.
  • Open terminal and run the command $ sonar -h to check everything is all right till here.
  • You will see a list of usage for this command.
  • Run $ sonar start
  • You will see Starting SonarQube… on the prompt that confirms SonarQube has been started.

2. sbt scoverage

If your project is based on sbt and you have not configured scoverage plug-in, follow this link to get the scoverage plug-in.

3. SonarQube Runner

The SonarQube Runner is recommended as the default launcher to analyze a project with SonarQube. Get it from here.

  • Download and extract it to your favourite directory and call it.
  • Switch to extracted sonar-runner-x.x directory.
  • Switch to bin directory.
  • Add this directory location in your PATH variable. If you are on Windows, create a new environment variable for this directory location and add it to the class path.

4. Scoverage plug-in for SonarQube

This plug-in makes sonar reading the statement coverage report (xml) generated by sbt scoverage. Get it from the RadoBuransky plug-in repository. Pay attention on your SonarQube version while you select the jars. Copy [sonar-scoverage-plugin-x.x.x.jar] and paste it to  SonarQube /extensions/plugins directory.

The Method

Once you’ve got all things specified above, now you need to configure the sonar-runner. This will tell sonar-runner from where it can get the xml generated by sbt scoverage to get the report generated. Do the following:-

Add a property file named sonar-project.properties the root directory of your project for which you want the coverage report. sonar-project.properties will look like:-

sonar-project.properties

  • sonar.projectKey= Must be Unique Text
  • sonar.projectName=Project Name
    sonar.projectVersion=0.1
  • sonar.sources= (Path is relative to the sonar-project.properties file. Replace “\” by “/” on Windows)
  • sonar.scoverage.reportPath= Relative path to scoverage xml file, e.g. target/scala-2.10/scoverage-report/scoverage.xml)
  • sonar.projectBaseDir= Directory where the project is located

One last configuration you need to perform before you get your result and that is configuring sonar.properties. Follow the steps:-

  • Go to <extract directory> of SonarQube and open /conf folder.
  • Edit sonar.properties file.

This property file contains several configurations such as jdbc driver configuration for sonar web server, such as database username and password.

follow “Installing the Web Server” section of the page at http://docs.sonarqube.org/display/SONAR/Installing  you must complete the sonar.properties configuration before moving ahead.

Completed the configuration? All cooked-up now! You are just few step away from your report!

  • On the terminal, go to the project root directory
  • Start sonar by running $ sonar start If sonar is not running already. If it is running already you can confirm it by browsing the sonar home page at: http://localhost:9000/ . If it is running already you will get a home page like the following (you should not expect a project listing on your dashboard 🙂

sonar_homepage

  •  Run $ sbt coverage test to generate scoverage XML report.
  •  Go to the page http://localhost:9000/ sonar dashboard and log in to sonar server with default username “sonar” and password : “sonar”.
  • Click on “Quality Profiles” on the dashboard and create a quality Profile for Scala
  • Run $ sonar-runner to launch the report on SonarQube web server.

If all goes well, You will get the following result as success of the sonar-runner as:

sonar-runner_success

 

Now you can happily browse your report on SonarQube dashboard.Checkout Some of my report screen shots.

Overall Project Coverage

sonar_ex1

Unit Test Coverage

file_wise_coverage

Line Coverage

line_coverage

All the best for your Configurations!!!

Advertisements

About Manish Mishra

Manish is a Scala Developer at Knoldus Software LLP. He loves to learn and share about Functional Programming, Scala, Akka, Spark.
This entry was posted in Scala, Test, Tutorial and tagged , , , , , . Bookmark the permalink.

3 Responses to Configuring SonarQube with Scoverage plug-in : The Complete Guide

  1. Pingback: Configuring SonarQube with Scoverage plug-in : The Complete Guide | dotnetask

  2. ShaRath says:

    Hi, You told us to create a Scala profile in Quality profile. But I could see C#, Java, and JavaScript. Could you help us here? I’m stuck here. And the blog was informative. Thanks to you in advance.

  3. If you are logged in SonarQube, you can browse the current Quality Profiles already available. You can also create a new Quality Profile for your language such as Scala. The following link would be helpful: http://docs.sonarqube.org/display/SONAR/Quality+Profiles

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s