// Copyright 2017 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

[[checkstyle_plugin]]
== The Checkstyle Plugin

The Checkstyle plugin performs quality checks on your project's Java source files using http://checkstyle.sourceforge.net/index.html[Checkstyle] and generates reports from these checks.


[[sec:checkstyle_usage]]
=== Usage

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

++++
<sample id="useCheckstylePlugin" dir="codeQuality" title="Using the Checkstyle plugin">
            <sourcefile file="build.gradle" snippet="use-checkstyle-plugin"/>
        </sample>
++++

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.

[[sec:checkstyle_tasks]]
=== Tasks

The Checkstyle plugin adds the following tasks to the project:

.Checkstyle plugin - tasks
[cols="a,a,a,a", options="header"]
|===
| Task name
| Depends on
| Type
| Description

| `checkstyleMain`
| `classes`
| api:org.gradle.api.plugins.quality.Checkstyle[]
| Runs Checkstyle against the production Java source files.

| `checkstyleTest`
| `testClasses`
| api:org.gradle.api.plugins.quality.Checkstyle[]
| Runs Checkstyle against the test Java source files.

| `checkstyle__SourceSet__`
| `__sourceSet__Classes`
| api:org.gradle.api.plugins.quality.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.

.Checkstyle plugin - additional task dependencies
[cols="a,a", options="header"]
|===
| Task name
| Depends on
| `check`
| All Checkstyle tasks, including `checkstyleMain` and `checkstyleTest`.
|===


[[sec:checkstyle_project_layout]]
=== Project layout

By default, the Checkstyle plugin expects configuration files to be placed in the root project, but this can be changed.

.Checkstyle plugin - root project layout
[cols="a,a", options="header"]
|===
| File
| Meaning

| `config/checkstyle`
| Other Checkstyle configuration files (e.g., `suppressions.xml`)

| `config/checkstyle/checkstyle.xml`
| Checkstyle configuration file
|===


[[sec:checkstyle_dependency_management]]
=== Dependency management

The Checkstyle plugin adds the following dependency configurations:

.Checkstyle plugin - dependency configurations
[cols="a,a", options="header"]
|===
| Name
| Meaning

| `checkstyle`
| The Checkstyle libraries to use
|===


[[sec:checkstyle_configuration]]
=== Configuration

See the api:org.gradle.api.plugins.quality.CheckstyleExtension[] class in the API documentation.


[[sec:checkstyle_built_in_variables]]
==== 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`.

++++
<sample id="useConfigLoc" dir="codeQuality" title="Using the config_loc property">
                <sourcefile file="config/checkstyle/checkstyle.xml" snippet="use-config-loc"/>
            </sample>
++++


[[sec:checkstyle_customize_xsl]]
=== Customizing the HTML report

The HTML report generated by the api:org.gradle.api.plugins.quality.Checkstyle[] task can be customized using a XSLT stylesheet, for example to highlight specific errors or change its appearance:

++++
<sample id="customizeCheckstyleReport" dir="codeQuality" title="Customizing the HTML report">
            <sourcefile file="build.gradle" snippet="customize-checkstyle-report"/>
        </sample>
++++

https://github.com/checkstyle/contribution/tree/master/xsl[View a sample Checkstyle stylesheet.]
