Configuring SonarQube with Scoverage plug-in : The Complete Guide

Reading Time: 4 minutes

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!!!

Written by 

Manish Mishra is Lead Software Consultant, with experience of more than 7 years. His primary development technology was Java. He fell for Scala language and found it innovative and interesting language and fun to code with. He has also co-authored a journal paper titled: Economy Driven Real Time Deadline Based Scheduling. His interests include: learning cloud computing products and technologies, algorithm designing. He finds Books and literature as favorite companions in solitude. He likes stories, Spiritual Fictions and Time Traveling fictions as his favorites.

3 thoughts on “Configuring SonarQube with Scoverage plug-in : The Complete Guide4 min read

  1. 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.

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading