Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 10 Next »


Groovy/Grails CodeNarc - GMetrics Bridge


Scott Kessler




Under Development (alpha testing)



Download Sources


Download JAR


Version Information









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


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.


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)


                mvn grails:install-plugin --DpluginName=codenarc

Note that I have had the problem where a Maven install “mvn” does NOT update the file with the codenarc plugin. In that case I have also had to do a “grails install-plugin…” or alternately just edit the file to add the appropriate current codenarc version.

Add report name/type to your Config.groovy


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


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.


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.

                       <taskdef name="gmetrics" classname="org.gmetrics.ant.GMetricsTask"/>
                                   <option name="outputFile" value="YOUR_REPORT_FILE_NAME.xml" />
                                   <option value="YOUR TITLE HERE" />
                               <fileset dir="src">
                                   <include name="*/.groovy"/>
                               <fileset dir="grails-app">
                                   <include name="*/.groovy"/>

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.


Add the following to your pom.xml to specify the project is Groovy.




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.

  • No labels