SonarQube is an open platform to manage code quality. It covers many aspects of code quality like Complexity, Coding rules, Duplications, Unit tests coverage etc.
Its really easy to configure SonarQube with your Grails project.
Download and Install
Download SonarQube and SonarQube Runner from http://www.sonarqube.org/downloads/
Extract the archived files in your preferred directory. I have extracted them in
Configure SonarQube properties by editing file on following path
In this file you will configure settings for database that will be used by SonarQube. I have MySql installed locally so uncomment following lines.
# User credentials. # Permissions to create tables, indices and triggers must be granted to JDBC user. # The schema must be created first. 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
As mentioned in the file you need to first create a database with name
create database sonar;
and then create a user that has full permission on the sonar database.
CREATE USER 'sonar'@'localhost' IDENTIFIED BY 'sonar'; GRANT ALL PRIVILEGES ON sonar.* TO 'sonar'@'%' WITH GRANT OPTION;
Now start SonarQube server using following command.
As I'm using OSX thats why I have chosen
macosx-universal-64 architecture, use the one suitable based on your operating system.
Now you need to install some plug-ins for SonarQube to work for Grails.
In your browser navigate to
http://localhost:9000/. Click on login link on top right corner of the page.
Enter user-name and 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. From Update center you can install / un-install and upgrade your plug-ins, first page shows all the plug-ins already installed with SonarQube.
Click on Available Plugins, you need to install 3 plugins for the scope of this article.
- Groovy - Enables analysis of Groovy projects.
- Cobertura - Get code coverage with Cobertura.
- SCM Activity - Collects and reports information from SCM.
Search each of these plugins on available plugins page and install them. Once installation is complete restart SonarQube for the plugins to take affect.
Configuring Sonar Runner
Configure Sonar Runner properties by editing file on following path
Uncomment following line for Sonar Runner to connect to MySql database.
#----- MySQL sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
Configuring Grails project
sonar-project.properties at the root of your Grails project with following values.
sonar.projectKey=com.domain.my-project sonar.projectName=My Project sonar.projectVersion=1.0 sonar.sources=src/groovy,grails-app/services,grails-app/controllers,grails-app/domain,grails-app/utils,test/unit sonar.language=grvy sonar.sourceEncoding=UTF-8 # Path to the Cobertura XML report sonar.groovy.cobertura.reportPath=target/test-reports/cobertura/coverage.xml
sonar.projectName as required. For the coverage to work properly make sure you specify all directories where you groovy code exists, your can exclude and include other directories as you need.
Analyzing the Grails project
Before you analyze your Grails project using Sonar Runner, make sure code coverage plugin is installed in your project (http://grails.org/plugin/code-coverage) and you will have to execute following command to generate cobertura xml file to be used by sonar.
grails test-app -coverage -xml
Now analyze your project by running following command from the root of your Grails project
If everything went well you should be able to see data related to your project in SonarQube at