# The CodeNarc Plugin

version 9.8.0-20260810002921+0000

The CodeNarc plugin performs quality checks on your project’s Groovy source files using CodeNarc and generates reports from these checks.

## Usage

To use the CodeNarc plugin, include the following in your build script:

build.gradle.kts

    plugins {
        codenarc
    }

build.gradle

    plugins {
        id 'codenarc'
    }

The plugin adds a number of tasks to the project that perform the quality checks when used with the Groovy Plugin. You can execute the checks by running `gradle check`.

## Tasks

The CodeNarc plugin adds the following tasks to the project:

`codenarcMain` — CodeNarc

  Runs CodeNarc against the production Groovy source files.

`codenarcTest` — CodeNarc

  Runs CodeNarc against the test Groovy source files.

`codenarcSourceSet` — CodeNarc

  Runs CodeNarc against the given source set’s Groovy source files.

### Dependencies added to other tasks

The CodeNarc plugin adds the following dependencies to tasks defined by the Groovy plugin.

`check`

  Depends on: All CodeNarc tasks, including `codenarcMain` and `codenarcTest`.

## Project layout

The CodeNarc plugin expects the following project layout:

    <root>
    └── config
        └── codenarc           (1)
            └── codenarc.xml   (2)

1 | CodeNarc configuration files go here
2 | Primary CodeNarc configuration file

| The default configuration file is resolved relative to the root project directory, so a single `config/codenarc/codenarc.xml` is shared by all projects in the build unless the `config` property is overridden.

## Dependency management

The CodeNarc plugin adds the following dependency configurations:

Table 1. CodeNarc plugin - dependency configurations
Name | Meaning
--- | ---
`codenarc` | The CodeNarc libraries to use

| If CodeNarc requires a different Groovy version than that used to compile Groovy source, you can supply one using the `codenarc` configuration.

build.gradle.kts

    dependencies {
        "codenarc"("org.codehaus.groovy:groovy-all:3.0.3")
        "codenarc"("org.codenarc:CodeNarc:1.6.1")
    }

build.gradle

    dependencies {
        codenarc 'org.codehaus.groovy:groovy-all:3.0.3'
        codenarc 'org.codenarc:CodeNarc:1.6.1'
    }

## Configuration

The CodeNarc plugin is configured through the CodeNarcExtension (the `codenarc {}` block). The most commonly used properties and their default values are:

Table 2. Gradle defaults for CodeNarc properties
Property | Description | Gradle default
--- | --- | ---
`toolVersion` | The version of CodeNarc to use. | `3.7.0-groovy-4.0`
`config` | The CodeNarc configuration to use. | `config/codenarc/codenarc.xml` (relative to the root project)
`reportFormat` | The format of the generated report. One of `html`, `xml`, `text`, or `console`. | `html`
`maxPriority1Violations` | The maximum number of priority 1 violations allowed before failing the build. | `0`
`maxPriority2Violations` | The maximum number of priority 2 violations allowed before failing the build. | `0`
`maxPriority3Violations` | The maximum number of priority 3 violations allowed before failing the build. | `0`
`reportsDirectory` | The directory where reports are generated. | `layout.buildDirectory.dir("reports/codenarc")`

See the CodeNarcExtension class in the API documentation for the full set of options.