Checkstyle¶
Java | Home | Release Notes | Plugin
By default, plugin activates if java sources available (src/main/java
).
Default config contains all possible checks, but some of them are disabled (note that some checkstyle rules are opposite and never intended to be used together). Uncomment check to enable it.
Warning
Checkstyle 10 requires java 11 or above. In order to preserve java 8 compatibility,
plugin would automatically switch to backport.
Such backport is loaded from custom repository (limited to checkstyle only!).
You'll see a warning in logs about backport activation.
If required, this behaviour could be disabled with quality.checkstyleBackport
configuration option.
In case you will use a custom config: checkstyle config must describe all used rules. So when new version release with new rules, config must be manually updated to add new rules (otherwise they would not be used).
Output¶
8 Checkstyle rule violations were found in 2 files
[Misc | NewlineAtEndOfFile] sample.(Sample.java:1)
File does not end with a newline.
http://checkstyle.sourceforge.net/config_misc.html#NewlineAtEndOfFile
...
Config¶
Tool config options with defaults:
quality {
checkstyleVersion = '10.12.7'
checkstyle = true // false to disable automatic plugin activation
// true for java 8-10 by default, set to false to disable fallback to backport
checkstyleBackport = !JavaVersion.current().java11Compatible
}
Suppress¶
@SuppressWarnings("NewlineAtEndOfFile")
Or with prefix (but require lower cased name):
@SuppressWarnings("checkstyle:newlineatendoffile")
To suppress all violations:
@SuppressWarnings("all")
Or using comments:
// CHECKSTYLE:OFF
..anything..
// CHECKSTYLE:ON
Suppressions file¶
You can also use external suppressions file. It may be a good option for disabling entire rules without overriding the default config file.
Suppressions config could: 1. disable some checks in all files 2. disable checks only in some files (or file pattern) 3. disable checks in exact file and under exact lines 4. disable checks by error message
The default config already configures optional suppressions file usage, so to use it just drop in
suppressions file into checkstyle configurations dir: gradle/config/checkstyle/suppressions.xml
Alternatively, you can use configs init task which will bring the default (empty) suppressions.xml:
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
"-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
"https://checkstyle.org/dtds/suppressions_1_2.dtd">
<!-- Empty suppressions file: copy it into gradle/config/checkstyle and specify required suppressions
(https://checkstyle.sourceforge.io/config_filters.html#SuppressionFilter)-->
<suppressions>
<!--<suppress checks="NewlineAtEndOfFileCheck" files="Sample.java"/>-->
<!--<suppress message="Missing a Javadoc comment."/>-->
</suppressions>
Warning
Pay attention that check names in the file are all have postfix 'Check', whereas violations in the console omit this postfix.
There are many configuration examples in the checkstyle documentation