checkstyle
checkstyle documentation
- Version in MegaLinter: 9.1
- Visit Official Web Site
- See How to configure checkstyle rules
- If custom
sun_checks.xml
config file is not found, sun_checks.xml will be used
- If custom
- See How to disable checkstyle rules in files
- See Index of problems detected by checkstyle
Configuration in MegaLinter
- Enable checkstyle by adding
JAVA_CHECKSTYLE
in ENABLE_LINTERS variable - Disable checkstyle by adding
JAVA_CHECKSTYLE
in DISABLE_LINTERS variable
Variable | Description | Default value |
---|---|---|
JAVA_CHECKSTYLE_ARGUMENTS | User custom arguments to add in linter CLI call Ex: -s --foo "bar" |
|
JAVA_CHECKSTYLE_FILTER_REGEX_INCLUDE | Custom regex including filter Ex: (src\|lib) |
Include every file |
JAVA_CHECKSTYLE_FILTER_REGEX_EXCLUDE | Custom regex excluding filter Ex: (test\|examples) |
Exclude no file |
JAVA_CHECKSTYLE_CLI_LINT_MODE | Override default CLI lint mode - file : Calls the linter for each file- list_of_files : Call the linter with the list of files as argument- project : Call the linter from the root of the project |
{linter.cli_lint_mode} |
JAVA_CHECKSTYLE_FILE_EXTENSIONS | Allowed file extensions. "*" matches any extension, "" matches empty extension. Empty list excludes all filesEx: [".py", ""] |
[".java"] |
JAVA_CHECKSTYLE_FILE_NAMES_REGEX | File name regex filters. Regular expression list for filtering files by their base names using regex full match. Empty list includes all files Ex: ["Dockerfile(-.+)?", "Jenkinsfile"] |
Include every file |
JAVA_CHECKSTYLE_PRE_COMMANDS | List of bash commands to run before the linter | None |
JAVA_CHECKSTYLE_POST_COMMANDS | List of bash commands to run after the linter | None |
JAVA_CHECKSTYLE_CONFIG_FILE | checkstyle configuration file nameUse LINTER_DEFAULT to let the linter find it |
sun_checks.xml |
JAVA_CHECKSTYLE_RULES_PATH | Path where to find linter configuration file | Workspace folder, then MegaLinter default rules |
JAVA_CHECKSTYLE_DISABLE_ERRORS | Run linter but consider errors as warnings | false |
JAVA_CHECKSTYLE_DISABLE_ERRORS_IF_LESS_THAN | Maximum number of errors allowed | 0 |
IDE Integration
Use checkstyle in your favorite IDE to catch errors before MegaLinter !
IDE | Extension Name | Install | |
---|---|---|---|
Eclipse | eclipse-cs | Visit Web Site | |
IDEA | checkstyle-idea | ||
Visual Studio Code | vscode-checkstyle |
MegaLinter Flavours
This linter is available in the following flavours
Flavor | Description | Embedded linters | Info | |
---|---|---|---|---|
all | Default MegaLinter Flavor | 95 | ||
java | Optimized for JAVA based projects | 42 |
Behind the scenes
How are identified applicable files
- File extensions:
.java
How the linting is performed
- checkstyle is called one time by identified file
Example calls
java -jar /usr/bin/checkstyle myfile.java
java -jar /usr/bin/checkstyle -c sun_checks.xml myfile.java
Help content
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
-d32 use a 32-bit data model if available
-d64 use a 64-bit data model if available
-server to select the "server" VM
The default VM is server,
because you are running on a server-class machine.
-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
A : separated list of directories, JAR archives,
and ZIP archives to search for class files.
-D<name>=<value>
set a system property
-verbose:[class|gc|jni]
enable verbose output
-version print product version and exit
-version:<value>
Warning: this feature is deprecated and will be removed
in a future release.
require the specified version to run
-showversion print product version and continue
-jre-restrict-search | -no-jre-restrict-search
Warning: this feature is deprecated and will be removed
in a future release.
include/exclude user private JREs in the version search
-? -help print this help message
-X print help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
enable assertions with specified granularity
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
disable assertions with specified granularity
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
-agentlib:<libname>[=<options>]
load native agent library <libname>, e.g. -agentlib:hprof
see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:<pathname>[=<options>]
load native agent library by full pathname
-javaagent:<jarpath>[=<options>]
load Java programming language agent, see java.lang.instrument
-splash:<imagepath>
show splash screen with specified image
See http://www.oracle.com/technetwork/java/javase/documentation/index.html for more details.
Installation on mega-linter Docker image
- Dockerfile commands :
# Parent descriptor install
ENV JAVA_HOME=/usr/lib/jvm/java-1.8-openjdk
ENV PATH="$JAVA_HOME/bin:${PATH}"
# Linter install
RUN CHECKSTYLE_LATEST=$(curl -s https://api.github.com/repos/checkstyle/checkstyle/releases/latest \
| grep browser_download_url \
| grep ".jar" \
| cut -d '"' -f 4) \
&& curl --retry 5 --retry-delay 5 -sSL $CHECKSTYLE_LATEST \
--output /usr/bin/checkstyle
Example success log
Results of checkstyle linter (version 8.38)
See documentation on https://megalinter.github.io/descriptors/java_checkstyle/
-----------------------------------------------
[SUCCESS] .automation/test/java/java_good_1.java
Starting audit...
Audit done.
Example error log
Results of checkstyle linter (version 8.38)
See documentation on https://megalinter.github.io/descriptors/java_checkstyle/
-----------------------------------------------
[ERROR] .automation/test/java/java_bad_1.java
Starting audit...
[ERROR] .automation/test/java/java_bad_1.java:1:1: Utility classes should not have a public or default constructor. [HideUtilityClassConstructor]
[ERROR] .automation/test/java/java_bad_1.java:1:7: Name 'java_bad_1' must match pattern '^[A-Z][a-zA-Z0-9]*$'. [TypeName]
[ERROR] .automation/test/java/java_bad_1.java:2:1: '{' at column 1 should be on the previous line. [LeftCurly]
[ERROR] .automation/test/java/java_bad_1.java:4:29: Parameter args should be final. [FinalParameters]
[ERROR] .automation/test/java/java_bad_1.java:4:40: Array brackets at illegal position. [ArrayTypeStyle]
[ERROR] .automation/test/java/java_bad_1.java:5:5: '{' at column 5 should be on the previous line. [LeftCurly]
Audit done.
Checkstyle ends with 6 errors.