Analyzing your code with CodeNarc

CodeNarc analyzes Groovy code for defects, bad practices, inconsistencies, style issues and more. A flexible framework for rules, rulesets and custom rules means it’s easy to configure CodeNarc to fit into your project. Build tool, framework support, and report generation are all enterprise ready.

This is the description you find on the CodeNarc home page: https://codenarc.github.io/CodeNarc/

I came to Groovy / Grails from Java. Did no Groovy training, so of course I brought lots of habits.

When I first learned about CodeNarc, it was a no-brainer. There are so many good reasons for using this tool, from the start. I had written quite a bit of code before configuring CodeNarc the first time. The number of violations was overwhelming at first.

Adding codenarc to your grails project is done in build.gradle:

apply plugin:"codenarc"

dependencies {
    // codenarc 1.5 and grails 4.0.x
    codenarc "org.codenarc:CodeNarc:${codenarc.toolVersion}"
    codenarc "org.codehaus.groovy:groovy-all:2.5.6"
}

codenarc {
    toolVersion = '1.5'
    configFile = file("${rootProject.projectDir}/config/codenarc/rules.groovy")
    reportFormat = 'html'
//    ignoreFailures = true
    maxPriority1Violations = 0
    maxPriority2Violations = 5
    maxPriority3Violations = 9
}

Then you will need the configuration file – /config/codenarc/rules.groovy:

This is my standard configuration file. Most rulesets are included. And I have made two changes to the default formatting rules.

  • LineLength increased to 200, because I think the default (120) is too short
  • SpaceAroundMapEntryColon should be followed by one whitespace for readability.
ruleset {
    description 'Grails-CodeNarc Project RuleSet'

    ruleset('rulesets/basic.xml')
    ruleset('rulesets/braces.xml')
    ruleset('rulesets/comments.xml')
    ruleset('rulesets/concurrency.xml')
    ruleset('rulesets/convention.xml')
    ruleset('rulesets/design.xml')
    ruleset('rulesets/dry.xml')
//    ruleset('rulesets/enhanced.xml')
    ruleset('rulesets/exceptions.xml')
    ruleset('rulesets/formatting.xml') {
        LineLength {
            length = 200
        }
        SpaceAroundMapEntryColon {
            characterAfterColonRegex = /\s/
        }
    }
    ruleset('rulesets/generic.xml')
    ruleset('rulesets/grails.xml')
    ruleset('rulesets/groovyism.xml')
    ruleset('rulesets/imports.xml')
    ruleset('rulesets/jdbc.xml')
    ruleset('rulesets/junit.xml')
    ruleset('rulesets/logging.xml')
    ruleset('rulesets/naming.xml')
    ruleset('rulesets/security.xml')
    ruleset('rulesets/serialization.xml')
    ruleset('rulesets/size.xml')
    ruleset('rulesets/unnecessary.xml')
    ruleset('rulesets/unused.xml')
}

Build will fail if the number of violations are higher than the max settings in build.gradle.

The codenarc reports will be created every time you build or run check.

You will be warned about failing build with a link to the report. Cou can always find the reports in the build/reports/codenarc directory.