Chapter 58. The Checkstyle Plugin

Table of Contents

58.1. Usage
58.2. Tasks
58.3. Project layout
58.4. Dependency management
58.5. Configuration
58.6. Customizing the HTML report

The Checkstyle plugin performs quality checks on your project's Java source files using Checkstyle and generates reports from these checks.

58.1. Usage

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

Example 58.1. Using the Checkstyle plugin

build.gradle

apply plugin: 'checkstyle'

The plugin adds a number of tasks to the project that perform the quality checks. You can execute the checks by running gradle check.

Note that Checkstyle will run with the same Java version used to run Gradle.

58.2. Tasks

The Checkstyle plugin adds the following tasks to the project:

Table 58.1. Checkstyle plugin - tasks

Task name Depends on Type Description
checkstyleMain classes Checkstyle Runs Checkstyle against the production Java source files.
checkstyleTest testClasses Checkstyle Runs Checkstyle against the test Java source files.
checkstyleSourceSet sourceSetClasses Checkstyle Runs Checkstyle against the given source set's Java source files.

The Checkstyle plugin adds the following dependencies to tasks defined by the Java plugin.

Table 58.2. Checkstyle plugin - additional task dependencies

Task nameDepends on
check All Checkstyle tasks, including checkstyleMain and checkstyleTest.

58.3. Project layout

By default, the Checkstyle plugin expects the following project layout, but this can be changed:

Table 58.3. Checkstyle plugin - project layout

File Meaning
config/checkstyle Other Checkstyle configuration files (e.g., suppressions.xml)
config/checkstyle/checkstyle.xml Checkstyle configuration file

58.4. Dependency management

The Checkstyle plugin adds the following dependency configurations:

Table 58.4. Checkstyle plugin - dependency configurations

Name Meaning
checkstyle The Checkstyle libraries to use

58.5. Configuration

See the CheckstyleExtension class in the API documentation.

58.5.1. Built-in variables

The Checkstyle plugin defines a config_loc property that can be used in Checkstyle configuration files to define paths to other configuration files like suppressions.xml.

Example 58.2. Using the config_loc property

config/checkstyle/checkstyle.xml

<module name="SuppressionFilter">
    <property name="file" value="${config_loc}/suppressions.xml"/>
</module>

58.6. Customizing the HTML report

The HTML report generated by the Checkstyle task can be customized using a XSLT stylesheet, for example to highlight specific errors or change its appearance:

Example 58.3. Customizing the HTML report

build.gradle

tasks.withType(Checkstyle) {
    reports {
        xml.enabled false
        html.enabled true
        html.stylesheet resources.text.fromFile('config/xsl/checkstyle-custom.xsl')
    }
}

View a sample Checkstyle stylesheet.