Skip to content

checkstyle

checkstyle documentation

checkstyle - GitHub

Configuration in Mega-Linter

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_FILE_EXTENSIONS Allowed file extensions. "*" matches any extension, "" matches empty extension. Empty list excludes all files
Ex: [".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_CONFIG_FILE checkstyle configuration file name
Use 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 Mega-Linter default rules
JAVA_CHECKSTYLE_DISABLE_ERRORS Run linter but consider errors as warnings false

IDE Integration

Use checkstyle in your favorite IDE to catch errors before Mega-Linter !

IDE Extension Name Install
Eclipse eclipse-cs Visit Web Site
IDEA checkstyle-idea
Visual Studio Code vscode-checkstyle Install in VsCode

Mega-Linter Flavours

This linter is available in the following flavours

Flavor Description Embedded linters Info
all Default Mega-Linter Flavor 83 Docker Image Size (tag) Docker Pulls
java Optimized for JAVA based projects 37 Docker Image Size (tag) Docker Pulls

Behind the scenes

How are identified applicable files

  • File extensions: .java

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://nvuillam.github.io/mega-linter/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://nvuillam.github.io/mega-linter/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.