Search

Dark theme | Light theme

January 10, 2011

Gradle Goodness: Use Groovy Ruleset File with Code Quality Plugin

The code-quality plugin supports CodeNarc for Groovy projects. The default configuration file is XML based with the name codenarc.xml and must be placed in the directory config/codenarc. But CodeNarc also supports a Groovy DSL for writing configuration files. Suppose we have a configuration file with the name rules.groovy and we put it in the directory config/codenarc. In our build.gradle file we reference this file with the property codeNarcConfigFileName. The code-quality plugin will pass this value on to CodeNarc and the rules defined in our Groovy ruleset file are used.

// File: config/codenarc/rules.groovy

ruleset {
    description 'Rules Sample Groovy Gradle Project'

    ruleset('rulesets/basic.xml')
    ruleset('rulesets/braces.xml')
    ruleset('rulesets/exceptions.xml')
    ruleset('rulesets/imports.xml')
    ruleset('rulesets/logging.xml') {
        'Println' priority: 1
        'PrintStackTrace' priority: 1
    }
    ruleset('rulesets/naming.xml')
    ruleset('rulesets/unnecessary.xml')
    ruleset('rulesets/unused.xml')
}

// File: build.gradle
['groovy', 'code-quality'].each { 
    apply plugin: it 
}

repositories {
    mavenCentral()
}

dependencies {
    groovy group: 'org.codehaus.groovy', name: 'groovy', version: '1.7.6'
}

codeNarcConfigFileName = 'config/codenarc/rules.groovy'