CodeNarc - Ant Task


The CodeNarc Ant Task is implemented by the org.codenarc.ant.CodeNarcTask class.


Attribute Description Required
ruleSetFiles The paths to the Groovy or XML RuleSet definition files. This can be a single file path, or multiple paths separated by commas. By default, the paths specified are relative to the classpath. But each path may be optionally prefixed by any of the valid prefixes, such as "file:" (to load from a relative or absolute filesystem path), or "http:". YES
maxPriority1Violations The maximum number of priority 1 violations allowed before failing the build (throwing a BuildException). NO
maxPriority2Violations The maximum number of priority 2 violations allowed before failing the build (throwing a BuildException). NO
maxPriority3Violations The maximum number of priority 3 violations allowed before failing the build (throwing a BuildException). NO
excludeBaseline The path to a baseline violations report (report type "baseline") If set, then all violations specified within that report are excluded (filtered) from the current CodeNarc run. If null/empty, then do nothing. See Baseline Report. NO
classpathRef The reference to a path element which is to be used as classpath when compiling analysed sources (useful with Enhanced Classpath Rules). NO

Report Nested Element

The report nested element defines the format and output file for the analysis report.

Attribute Description Required
type The type of the output report. Must be either one of the predefined type names: "html", "xml", "text", "console", "ide" or else the fully-qualified class name of a class (accessible on the classpath) that implements the interface. Yes
toFile (Deprecated) The path and filename for the output report file. This is deprecated. Use the <option> nested element instead. No
title (Deprecated) The title for the output report. This is deprecated. Use the <option> nested element instead. No


  • The "ide" report type creates an IdeTextReportWriter and sets its writeToStandardOut property to true. The generated report includes IDE-compatible (Eclipse, Idea) hyperlinks to source code for violations.
  • The "console" report type creates a TextReportWriter and sets its writeToStandardOut property to true.

Option Nested Element

The <option> element is a child of the <report> element and defines a report-specific option for a report. You specify the option name> and <<value as attributes within the <option> element. See the Example below.

Fileset Nested Element

At least one fileset nested element is required, and is used to specify the source files that CodeNarc should analyze. This is the standard Ant FileSet, and is quite powerful and flexible. See the Apache Ant Manual for more information on FileSets.

Classpath Nested Element

An optional classpath nested element can be utilized to specify contents of the classpath to be used when CodeNarc compiles classes it analyses. Having control over that classpath is essential when using Enhanced Classpath Rules. It is a standard Ant Path element.


Here is an example Ant XML build file.

<taskdef name="codenarc" classname="org.codenarc.ant.CodeNarcTask"/>
<target name="runCodeNarc">

        <report type="html">
            <option name="outputFile" value="reports/CodeNarcAntReport.html" />
            <option name="title" value="My Sample Code" />

        <fileset dir="src">
            <include name="**/*.groovy"/>

Things to note:

  • Three RuleSet files are specified (basic, exceptions and imports).
  • The fileset specifies that all ".groovy" files are analyzed.
  • Remember that you need the log4j jar (and a "" file) on the classpath.

Logging and Troubleshooting

Be sure to have a "" or "log4j.xml" file on the classpath so that any errors are logged.