# Chapter 24. The Code Quality Plugin

The code quality plugin adds tasks which perform code quality checks and generate reports from these checks. The following tools are supported:

- Checkstyle

- CodeNarc

To use the plugin, add the following to your build script:

    usePlugin 'code-quality'

## 24.1. Tasks

When used with the Java plugin, the code quality plugin adds the following tasks to the project:

Table 24.1. Code quality plugin - Java tasks

Task name | Depends on | Type | Description
`checkstyleMain` | - | `Checkstyle` | Runs Checkstyle against the production Java source files.
`checkstyleTest` | `compile` | `Checkstyle` | Runs Checkstyle against the test Java source files.
`checkstyle`SourceSet`` | - | `Checkstyle` | Runs Checkstyle against the given source set's Java source files.

When used with the Groovy plugin, the code quality plugin adds the following tasks to the project:

Table 24.2. Code quality plugin - tasks

Task name | Depends on | Type | Description
codenarcMain | - | `CodeNarc` | Runs CodeNarc against the production Groovy source files.
codenarcTest | - | `CodeNarc` | Runs CodeNarc against the test Groovy source files.
codenarc`SourceSet` | - | `CodeNarc` | Runs CodeNarc against the given source set's Groovy source files.

The Code quality plugin adds the following dependencies to tasks added by the Java plugin.

Table 24.3. Code quality plugin - additional task dependencies

Task name | Depends on
check | All Checkstyle and CodeNarc tasks, including `checkstyleMain`, `checkstyleTest`, `codenarcMain` and `codenarcTest`

Figure 24.1. Code quality plugin - tasks

## 24.2. Project layout

The code quality plugin expects the following project layout:

Table 24.4. Code quality plugin - project layout

File | Meaning
`config/checkstyle/checkstyle.xml` | Checkstyle configuration file
`config/codenarc/codenarc.xml` | CodeNarc configuration file

## 24.3. Dependency management

The code quality plugin does not add any dependency configurations.

## 24.4. Convention properties

When used with the Java plugin, the code quality plugin adds the following convention properties to the project:

Table 24.5. Code quality plugin - convention properties

Property name | Type | Default value | Description
`checkstyleConfigFileName` | `String` | `config/checkstyle/checkstyle.xml` | The location of the Checkstyle configuration file, relative to the project directory.
`checkstyleConfigFile` | `File` (read-only) | ``projectDir`/`checkstyleConfigFileName`` | The Checkstyle configuration file.
`checkstyleResultsDirName` | `String` | `checkstyle` | The name of the directory to generate Checkstyle results into, relative to the build directory.
`checkstyleResultsDir` | `File` (read-only) | ``buildDir`/`checkstyleResultsDirName`` | The directory to generate Checkstyle results into.

These convention properties are provided by a convention object of type `JavaCodeQualityPluginConvention` .

When used with the Groovy plugin, the code quality plugin adds the following convention properties to the project:

Table 24.6. Code quality plugin - convention properties

Property name | Type | Default value | Description
`codeNarcConfigFileName` | `String` | `config/codenarc/codenarc.xml` | The location of the CodeNarc configuration file, relative to the project directory.
`codeNarcConfigFile` | `File` (read-only) | ``projectDir`/`codeNarcConfigFileName`` | The CodeNarc configuration file.
`codeNarcReportsDirName` | `String` | `codenarc` | The name of the directory to generate CodeNarc reports into, relative to the reports directory.
`codeNarcReportsDir` | `File` (read-only) | ``reportsDir`/`codeNarcReportsDirName`` | The directory to generate CodeNarc reports into.

These convention properties are provided by a convention object of type `GroovyCodeQualityPluginConvention` .