Search code examples
pluginsconfigurationgradlecodenarc

Configuring CodeNarc in Gradle


I am trying to wrap my head around using CodeNarc inside of Gradle.

According to the CodeNarc docs, my project needs to have a config/codenarc/codenarc.xml config file defined. But then I see plenty of example config files (such as this StarterRuleSet) that seem to use a Groovy DSL.

So I ask:

  1. Is it possible to use a Groovy DSL, and if so what does the name of the file have to be, where does it have to be located in my project, and how do I wire it up to my Gradle build?; and
  2. Where is the documentation on the Groovy DSL?; and
  3. By default, CodeNarc outputs a report called main.html; how could I change the name of this file to, say, codenarc.html?

Solution

  • It looks like in your build.gradle you can just put:

    codenarc {
        toolVersion = "0.20"
    }
    
    codenarcMain {
        configFile = rootProject.file("path/to/CodeNarcMain.groovy")
    }
    
    codenarcTest {
        configFile = rootProject.file("path/to/CodeNarcTest.groovy")
    }
    

    Where the file names (CodeNarcMain, CodeNarcTest) are whatever you want them to be.

    Groovy DSL documentation is here. If you need to configure a specific rule, look it up in the documentation and if the rule has public properties that it exposes, they'll be listed in a table under the specific rule's documentation.

    And it looks like you can only change the report file format (HTML, XML, text, console) but not the actual file name produced.