CodeNarc - Ant Task

Description

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

Parameters

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 java.net.URL 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

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"
or else the fully-qualified class name
of a class (accessible on the classpath) that implements the
org.codenarc.report.ReportWriter 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

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.

Example

Here is an example Ant XML build file.

<taskdef name="codenarc" classname="org.codenarc.ant.CodeNarcTask"/>
<target name="runCodeNarc">
    <codenarc
            ruleSetFiles="rulesets/basic.xml,rulesets/exceptions.xml,rulesets/imports.xml"
            maxPriority1Violations="0">

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

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

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 "log4j.properties" file) on the classpath.

Logging and Troubleshooting

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