On 07/02/2011, at 1:29 PM, ajfwil wrote:

> 
> Hello all,
> 
> I am looking for some help in setting up a custom codenarc rule that works
> with the codenarc code-quality plugin.
> 
> My build.gradle has the following instruction: 
> 
> codeNarcConfigFileName = 'config/codenarc/rules.groovy'
> 
> My rules.groovy file looks like this:
> 
> 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') {
>               'ClassName' {
>                       enabled = false
>               }
>       }
>    ruleset('rulesets/unnecessary.xml') {
>        'UnnecessaryReturnKeyword' {
>            enabled = false
>        }
>    }
> 
>    ruleset('rulesets/unused.xml')
> 
>    rule('MyCustomRuleScript.groovy')
> }
> 
> The codenarcMain task executes successfully without the last instruction,
> however adding the 
> 
> rule('MyCustomRuleScript.groovy')
> 
> instruction is giving me problems. I can't seem to find a place to stick
> MyCustomRuleScript.groovy in my project so that the plugin will find it and
> not throw java.io.FileNotFoundException.
> 
> The codenarc documentation states "By default, the paths specified are
> relative to the classpath. But these paths may be optionally prefixed by any
> of the valid java.net.URL prefixes..." however I've tried various locations,
> and various prefixes to the instruction in rules.groovy, all with no luck.

There isn't any easy way to add classes to the classpath that CodeNarc will use 
to locate scripts, so you will have to use a URL:

rule('file:/some/absolute/path/MyCustomRuleScript.groovy')

To avoid hard-coding an absolute path in your ruleset file, you could use the 
Copy task to substitute a property reference into the ruleset file.

rules.groovy:

rule('@ruleUrl@')

build.gradle:

task codeNarcRuleSetFile(type: Copy) {
    from 'some/dir/rules.groovy'
    into 'build'
    filter(ReplaceTokens, tokens: [ruleUrl: 
uri('some/dir/MyCustomRuleScript.groovy')]
}

codeNarc {
    dependsOn codeNarcRuleSetFile
    configFile = file('build/rules.groovy')
}

We might add some way to more easily substitute properties into config files, 
to make this a little easier. Or we might give you some way to add stuff to the 
CodeNarc classpath.

Alternatively, you might ask the CodeNarc folks to change the default so that 
relative paths are resolved relative to the ruleset script that references them 
(which seems like a more useful default to me).


--
Adam Murdoch
Gradle Developer
http://www.gradle.org
CTO, Gradle Inc. - Gradle Training, Support, Consulting
http://www.gradle.biz

Reply via email to