Name |
Groovy/Grails CodeNarc - GMetrics Bridge |
Authors |
|
Jira |
|
State |
Under Development (alpha testing) |
License |
??? |
Download Sources |
N/A |
Download JAR |
N/A |
Version Information
Application |
Version |
|---|---|
Sonar |
v1.12 |
CodeNarc |
v0.5 |
GMetrics |
v0.1.1 |
Description / Features
This plugin will consume output from CodeNarc, which produces violation information, and GMetrics, which produces metric information, both for Groovy (with some Grails specific violation information) and pull the data into Sonar.
Usage and installation
Installation
Place the JAR file of the plugin in the following Sonar folder of your Sonar installation
/extentions/plugins
You will also need to place the additional Groovy language support JAR file in the same folder as defined by this snippet from the pom file. You may use version 1.6.7 or above as of this writing.
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>1.6.7</version>
</dependency>
Simply restart your sonar instance once the JAR files are in place.
Overview
You will need to setup CodeNarc and Gmetrics for your Grails application. Most of this is done by editing the following files:
- Config.groovy
- Pom.xml
Details on installing the appropriate plugins and how to edit these files is explained in the sections below.
Basically once everything is setup you’ll be running the following commands(assuming you are using Maven):
mvn grails:exec --Dcommand=codenarc
mvn compile
mvn sonar:sonar
At which point your application metrics should be available within your Sonar instance.
CodeNarc
CoeNarc is used to report on violations in your code. Typical violations are things like an empty else clause, or an import that is never used. There are many more violation types but you get the general idea.
Install codenarc plugin
grails install-plugin codenarc (0.4 or 0.5 . Not testing with anything above that yet)
Or
mvn grails:install-plugin --DpluginName=codenarc
Note that I have had the problem where a Maven install “mvn” does NOT update the application.properties file with the codenarc plugin. In that case I have also had to do a “grails install-plugin…” or alternately just edit the application.properties file to add the appropriate current codenarc version.
Add report name/type to your Config.groovy
codenarc.reportName=’MyCodeNarcReport.xml’
codenarc.reportType = ‘xml’ (the Grails plugin 0.3 only recognizes HTML, >0.4 also recognizes xml & text. This plugin requires the xml output.)
codenarc.reportTitle=’YOUR TITLE’
Run codenarc against your project:
grails codenarc
or since you are using Maven you should use
mvn grails:exec --Dcommand=codenarc
CAUTION:
CodeNarc is known to not run properly as a grails plugin if your project includes an XML library that includes an older version of some of the XML Constants. For example the stacks.jar in Xfire. CodeNarc will run properly from the command line using a bat file as it picks up the proper classes, but when you run it using “grails codenarc” it tries to start up Grails and that in turn finds the improper library in your app. and it doesn’t run.
Gmetrics
For complexity metrics you’ll need to add the Gmetrics plugin to run. You can set it up to run automatically at compile time in your POM. Note that as of the writing of this Gmetrics version 0.1.1 is required, but it is not yet available via a Maven repository. Therefore you will need to download 0.1.1 and install it in your local repository if you are using Maven. You can add the maven-antrun-plugin and set it up as follows in your POM.
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<configuration>
<tasks>
<taskdef name="gmetrics" classname="org.gmetrics.ant.GMetricsTask"/>
<gmetrics>
<report>
<option name="outputFile" value="YOUR_REPORT_FILE_NAME.xml" />
<option value="YOUR TITLE HERE" />
</report>
<fileset dir="src">
<include name="*/.groovy"/>
</fileset>
<fileset dir="grails-app">
<include name="*/.groovy"/>
</fileset>
</gmetrics>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.gmetrics</groupId>
<artifactId>GMetrics</artifactId>
<version>0.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>1.6.7</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.13</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
</plugin>
To produce the Gmetrics report you can just invoke the compile task in Maven.
mvn compile
If you are not using Maven you can just run the Gmetrics class as an application from the command line. You can check out the Gmetrics web pages for how to do that.
SONAR
Add the following to your pom.xml to specify the project is Groovy.
<properties>
<sonar.language>grvy</sonar.language>
</properties>
To populate the Sonar database with your metrics you’ll need to run the sonar task in Maven:
mvn sonar:sonar
That should be everything. Enjoy.

