How to manage quality of Groovy / Grails code using Sonar, Quality Management Platform

By Jayita, Gaea News Network
Thursday, December 16, 2010

Java developer are well-acquainted with the two terms Groovy and Sonar. While Groovy enables them to write far more expressive and readable code, Sonar helps them manage code quality.

More specifically, Sonar is a one-stop shop for code quality metrics. Groovy/Grail Code Quality management can be easily achieved using Sonar. If you are using Maven to generate code quality metrics with tools like Checkstyle, PMD, FindBugs, Cobertura, and/or Clover, forget the standard Maven reports - use Sonar instead! Sonar gives you a dynamic view of both snapshot and historical data coming from all of these tools.

Groovy is by nature a dynamic language and you don’t develop code in the same way you develop it with a static language. Groovy is both weakly and strongly typed, as you wish, but is very often used with weak typing.

Tools available in Groovy

Groovy eco-system is far smaller than in Java.

a) GMetrics: It’s available either standalone or as a Grails or Griffon (Grails twin brother for RDA) plugin. It computes very basic metrics on your code. By default those are lines per method, lines per class and cyclomatic complexity.

b) CodeNarc: Available as standalone, as a Grails/Griffon plugin and also as a Gradle (a groovier build system) plugin, it performs static source code analysis, thanks to a set of rules. It performs checks similar to what CheckStyle and PMD do. It

c) Sonar: Sonar is the most updated plugin , released in August this year. Version 0.2 added support for test reports and test coverage. The plugin relies mainly on CodeNarc and GMetrics as well as on Squid for some basic metrics.

Categories of Tools

Basically, there are two categories of tools.

1) those that work on source code (CheckStyle, PMD etc…): they won’t work at all because their parser will surely fail on Groovy code.

2) those that work on bytecode (XDepend, FindBugs etc…): they will be able to work but will generate all kind of noises. For example you will get plenty of warnings due to the Groovy framework if you run FindBugs on a Groovy application.

So, whats the best tool for Groovy /Grail Code Management ?

Although its still a new plugin, Sonar seems to be the most appropriate one for Groovy/Grail code management. If you are planning to install Sonar you could setup most of the tools CodeNarc, CPD, Cobertura etc. in your Hudson. You can install Sonar on CentOS.

Just follow few easy steps given below.

Requirements for Sonar Download

  • Server
  • Browser
  • Technical architecture

Server:

Sonar runs on any operating system that support Java and Maven.

  • Java Development Kit v1.5 or later
  • Maven 2.0.9+, 2.1.+, 2.2.+ or 3.+ (Since Sonar 2.4)

Sonar requires a relational database for storage of measures data. Sonar supports :

  • MySQL 5.x+
  • Oracle 10g+
  • PostgreSQL 8.3+ (8.2 since Sonar 1.12)
  • MS SQLServer 2005 since Sonar 2.0

Browser:

Sonar works decently with any of the following browsers.

  • Firefox 2.x and 3.x
  • Internet Explorer 6.x, 7.x and 8.x
  • Safari (latest version)
  • Opera (latest version)
  • Chrome (latest version)

Technical architecture

archi

Step 1:

Install Sonar from Sonar web site.
#unzip sonar.zip
#mv sonar/usr/local

Step 2:

Then you download Groovy plugin for Sonar and copy the downloaded JAR in [sonar]/extensions/plugins where [sonar] is the directory where you extracted Sonar ZIP file.

Step 3:

Lastly, run Sonar server, using sonar.sh (for Linux) or StartSonar.bat (for Windows).

Location of sonar.sh
#/usr/local/[sonar]/bin/linux[platform]

Starting Sonar:
#./sonar.sh start

For Windows execute the following steps.

*Download and unzip the distribution

On command promt
bin\windows-x86-32\StartSonar.bat

*Execute the following command on your maven2 projects.

mvn clean install sonar:sonar

*Browse to http://localhost:9000
*For administration features, default login/password is admin/admin.

Step 4:

Now you configure your Grails project

Setting up a Grails project is different from that of Groovy project. First you go to the plugin home page.

You are left with two choices:

1. Not-mavenized Grails project

Infact, this the best option recommended for you. For this you need;

a) to install Grails Test Code Coverage plugin by running:

grails install-plugin code-coverage

b) to configure Cobertura to generate the XML report needed by Sonar. For this, add these lines to your grails-app/conf/BuildConfig.groovy file:

1 coverage {
2 enabledByDefault = false
3 xml = true
4 }

c) to add a pom.xml file at the root of your project with the following content:

01 <project xmlns=”http://maven.apache.org/POM/4.0.0″
02 xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
03 xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
04 <modelVersion>4.0.0</modelVersion>
05 <groupId>com.yourcompany</groupId>
06 <artifactId>yourproject</artifactId>
07 <version>1.0</version>
08 <packaging>pom</packaging>
09 <name>Your Project</name>
10 <build>
11 <sourceDirectory>grails-app</sourceDirectory>
12 <plugins>
13 <plugin>
14 <groupId>org.apache.maven.plugins</groupId>
15 <artifactId>maven-compiler-plugin</artifactId>
16 <configuration>
17 <source>1.6</source>
18 <target>1.6</target>
19 <excludes>
20 <exclude>**/*.*</exclude>
21 </excludes>
22 </configuration>
23 </plugin>
24 <plugin>
25 <groupId>org.codehaus.mojo</groupId>
26 <artifactId>build-helper-maven-plugin</artifactId>
27 <version>1.1</version>
28 <executions>
29 <execution>
30 <id>add-source</id>
31 <phase>generate-sources</phase>
32 <goals>
33 <goal>add-source</goal>
34 </goals>
35 <configuration>
36 <sources>
37 <source>src/groovy</source>
38 <source>src/java</source>
39 </sources>
40 </configuration>
41 </execution>
42 </executions>
43 </plugin>
44 </plugins>
45 </build>
46 <properties>
47 <sonar.language>grvy</sonar.language>
48 <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
49 <sonar.surefire.reportsPath>test/reports</sonar.surefire.reportsPath>
50 <sonar.cobertura.reportPath>test/reports/cobertura/coverage.xml</sonar.cobertura.reportPath>
51 <sonar.phase>generate-sources</sonar.phase>
52 </properties>
53 </project>

*d) to generate the tests report and coverage before running Sonar analysis

grails test-app -coverage

2. Mavenized Grails project

This setup is not difficult, but few limitations are there.You need to add the following code in your POM:

a) tell Sonar your are analyzing Groovy code

1 <properties>
2 <sonar.language>grvy</sonar.language>
3 </properties>

b) setup the path for your source code (more on this later)

1 <build>
2 <sourceDirectory>grails-app</sourceDirectory>
3 </build>

c) change the Surefire report path to match grails’ one

01 <build>
02 <plugins>
03 <plugin>
04 <groupId>org.apache.maven.plugins</groupId>
05 <artifactId>maven-surefire-plugin</artifactId>
06 <version>2.6</version>
07 <configuration>
08 <reportsDirectory>${project.build.directory}/test-reports</reportsDirectory>
09 </configuration>
10 </plugin>
11 </plugins>
12 </build>

Here you are done with it.

Step 5:

Thereafter you just need to run the following command at the root of your Grails project. Make sure that Sonar is running properly.

$ mvn sonar:sonar

Then go to Sonar web interface by browsing to http://localhost:9000/ and enjoy it!

You will get the standard Sonar dashboard there.

sonar-screenshot-1024x580

Do remember that you need to configure CodeNarc and CPD only to report warnings and errors you want your team to fix. It might appear as the toughest part of the project for you.

Limitations

However, Sonar plugin has few limitations, the greatest one being the problem in parsing some Groovy files.

If you use Maven, you get a few more limitations:

*Sonar Groovy plugin only locates your source files by the use of the sourceDirectory parameter therefore with the above setting (set to grails-app/), files in src/groovy and src/java won’t be included in the analysis results (for eg see SONARPLUGINS-737)

the analysis won’t work if one of your tests is failing (unfortunately Grails Maven plugin ignores the parameter -Dmaven.test.failure.ignore=true, see GRAILS-6850)

Note: To know more about Sonar go to this link.

You can learn more about Groovy/Grails here.

Filed under: Featured Article

Tags: , ,
Discussion
December 18, 2010: 3:33 am

Thanks for the update. I wasn’t aware of how to use the groovy grails.

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :