# Chapter 26. 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

## 26.1. Usage

To use the code quality plugin, include in your build script:

Example 26.1. Using the code quality plugin

`build.gradle`

    apply plugin: 'code-quality'

## 26.2. Tasks

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

Table 26.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 26.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 26.3. Code quality plugin - additional task dependencies

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

Figure 26.1. Code quality plugin - tasks

## 26.3. Project layout

The code quality plugin expects the following project layout:

Table 26.4. Code quality plugin - project layout

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

## 26.4. Dependency management

The code quality plugin does not add any dependency configurations.

## 26.5. Convention properties

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

Table 26.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.
`checkstyleProperties` | `Map` | `[:]` | The properties to use when loading the Checkstyle configuration.

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 26.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`.