Using CodeNarc with Maven

1.9k Views Asked by At

I am trying to integrate CodeNarc with a Maven-based Groovy project. The documentation on the site for the CodeNarc Maven plugin is minimal. The usage aspects I am trying to understand are:

  • How to point to the custom rule sets and where in the project to place them?
  • How to fail the Jenkins build if any of the rules are violated.

Currently I am able to run CodeNarc using command

mvn codenarc:codenarc

When I add the 'reporting' section to the POM file (as described at http://www.mojohaus.org/codenarc-maven-plugin/usage.html) and run

mvn site

no CodeNarc report is generated. I get this warning

[WARNING] No URL defined for the project - decoration links will not be resolved

but it is not clear where it is related to CodeNarc.

What is the proper way of using CodeNarc with Maven?

1

There are 1 best solutions below

0
On

I just did it, in case you still need the tip. You can hook the execution of the plugin by creating a "plugin" entry under "build"->"plugins"->"plugin". Here is what I have.

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>codenarc-maven-plugin</artifactId>
      <version>0.18-1</version>
      <configuration>
        <sourceDirectory>${project.basedir}/src/main/groovy</sourceDirectory>
        <maxPriority1Violations>0</maxPriority1Violations>
        <maxPriority2Violations>0</maxPriority2Violations>
        <maxPriority3Violations>0</maxPriority3Violations>
      </configuration>
      <executions>
        <execution>
          <phase>prepare-package</phase>
          <goals>
            <goal>codenarc</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    ...
  </plugins>
</build>

Note the "maxPriority_Violations" values. This is what makes the build fail in case of violations.

I dont use any custom rules, but it seems you can define your own rules by setting the "rulesetfiles" configuration option. See configuration options here: http://www.mojohaus.org/codenarc-maven-plugin/codenarc-mojo.html

Example of project with this configuration: https://github.com/tveronezi/faceid/tree/master/faceid-web