Using SonarQube with Code Coverage and CodeNarc Plugin to improve code quality

SonarQube is open platform to manage code quality. It Provides support for more than 20 programming languages. It shows analysis reports in fancy web frontend with green and red colors includes global dashboard, project issues, and some overall reports with detailed descriptions. You can read more here in documentation of SonarQube. SonarQube is formerly known as Sonar .A public live demo showing results for various open source project is available here

The sonar platform have three basic components Database, Web Server and Analyser. Read more here for detail about these three components and its configuration. I have selected MySql database with web server and SonarQube Runner analyser. SonarQube Eclipse plugin is also available which provides comprehensive integration of SonarQube in Eclipse for Java, C/C++ and Python projects.

 

Code Coverage

Grails has a wonderful plugin to generate reports for our source code, known by  Grails Code Coverage Plugin. This plugin generates code coverage reports using Cobertura.

Installation:

1. Install Plugin

test :code-coverage:2.0.3-2

2. To run use following command

grails test-app -coverage -xml

3. It will generate XML reports in the test/report/cobertura directory.

You can read more here in documentation of code coverage plugin for more configuration.

 

CodeNarc

Grails has a wonderful plugin which provides static analysis rules for our Groovy code, known by Grails CodeNarc Plugin. This plugin provides a script codenarc that will analyze our source code and write its results to an HTML/XML/TEXT file.

Installation:

1. Install Plugin

compile:codenarc:0.22

2. Add configuration in BuildConfig.groovy for XML reports.

codenarc.reports = {

    XMLReportName('xml') {

        outputFile = 'codeNarcReport.xml'

        title = 'Sample Test Report'

    }

}

3. Plugin provides script to analyse our code

grails codenarc

It will generate XML reports in the output file specified.

You can read more here in documentation of codenarc plugin for more configuration.

 

Installation and Configurations

Follow following steps to install SonarQube and configure SonarQube analyser with CodeNarc and CodeCoverage. Download SonarQube and SonarQube Runner from here.

Step 1: SonarQube

SonarQube installation steps are specified in its official document. Go through it and check prerequisites, configuration for database and web server property for more details. SonarQube platform is mode up of three main components as follows:

Database

  • Database used for security, plugin settings and storing analysis data of project.
  • SonarQube supports several database engines MicroSoft SQL Server, MySql, Oracle, PostgreSQL. I preferred MySql database.
  • To setup database we need to update sonar config file sonar.properties placed in SONAR_INSTALLATION_DIR/conf/sonar.properties.
  • By Default it contains database name as “sonar” and user with same username and password. Create database and register user. This is sample script for MySql.
  • Configuration file sonar.properties contains database configuration as follows
 ...

# DATABASE

sonar.jdbc.username=sonar

sonar.jdbc.password=sonar

#----- MySQL 5.x

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

...

Web-Server

  • By Default web server runs on port 9000. We can configure it in same configuration file.
  • Configuration file sonar.properties contains default web server configuration as follows
...

# WEB SERVER

sonar.web.port=9000

...

Analyser

  • SonarQube supports multiple analysers as SonarQube Runner, Maven, SonarQube Ant Task, Gradle, CI Engine. I have used SonarQube Runner for my project.
  • We need to install SonarQube Runner and and analyse project. Steps to install and configure it are given below. Start the SonarQube server by running following script through installation directory
/etc/sonarqube/bin/[OS]/sonar.sh console

Browse the server at http://localhost:9000 and see default empty SonarQube web server. Click on login link on top right corner of page. Enter username / password. Default is admin / admin.

Click on settings in the top right corner, then click on update center under System menu item on the left. Click on Available Plugins, You need to install three plugins as follows:

  • Groovy – Enables analysis of Groovy project.
  • Cobertura – Get Code Coverage with Cobertura.
  • SCM Activity – Collects and reports information from SCM.

Once Installation is complete restart SonarQube for the plugins to take affetc.

Step 2: SonarQube Runner (Analyser)

SonarQube Runner is recommended as the default launcher to analyse a project with SonarQube. Installation steps are specified in its official document. Go through it and check prerequisites, configuration for SonarQube Runner. After installation we are now ready to analyse project with SonarQube Runner by following steps specified in its official document. Configuration file sonar-project.properties contains settings for project name, version, language etc.

# sonar-project.properties

sonar.projectKey=project_key

sonar.projectName=project_name

sonar.projectVersion=2.3.5

sonar.sources=grails-app

sonar.groovy.cobertura.reportPath=target/test-reports/cobertura/coverage.xml

sonar.groovy.codenarc.reportPath=target/CodeNarc-Report.xml

sonar.language=grvy sonar.sourceEncoding=UTF-8

Following command is used from project base directory to launch the analysis.

sonar-runner

Browse the server at http://localhost:9000 and see SonarQube web server with added projects and its analysis reports.

Step 3: SonarQube Runner (Analyser) with Code Coverage

SonarQube Runner provides settings to integrate Code coverage reports with SonarQube.

# Import Cobertura code coverage report.

# Set the property to the path of the Cobertura report.

sonar.cobertura.reportPath=target/site/cobertura/coverage.xml

Browse the server at http://localhost:9000 and see SonarQube web server with project coverage reports integrated. Read more here to manage Code Coverage report.

 

Step 4: SonarQube Runner (Analyser) with CodeNarc

SonarQube Runner provides settings to integrate CodeNarc reports with SonarQube.

# Import CodeNarc report.

# Set the property to the path of the CodeNarc report.

sonar.groovy.codenarc.reportPath=path-to-codenarc-report/report.xml

Browse the server at http://localhost:9000 and see SonarQube web server with project CodeNarc reports integrated.

We are done with our power setup to analyse our projects. You can analyse multiple projects using SonarQube Runner by repeating steps 2-4.

About CauseCode: We are a technology company specializing in Healthtech related Web and Mobile application development. We collaborate with passionate companies looking to change health and wellness tech for good. If you are a startup, enterprise or generally interested in digital health, we would love to hear from you! Let's connect at bootstrap@causecode.com
Have you subscribed to our blogs and newsletter? If not, what are you waiting for?  Click Here

Leave a Reply

Your email address will not be published. Required fields are marked *

SUBSCRIBE!

Do you want to get articles like these in your inbox?

Email *

Interested groups *
Healthtech
Business
Technical articles

Archives