Content

How to set system property for CLI

System properties should be provided to java command. Example: java -D<property>=<value> -jar ...

Attention to Windows users: system properties should be wrapped with ". Example: -D"<property>=<value>"

Enable External DTD load

The property checkstyle.enableExternalDtdLoad defines the ability to use custom DTD files in config and load them from some location. The property type is boolean and defaults to false. Disabled by default due to security concerns.

Examples

The following is an example of including the contents of other xml files by using the ENTITY feature to keep common parts of configs in a single file and then creating composite configs from smaller parts.
Imagine we want to define different requirements for test sources than for main code.

Common part checkstyle-common.xml:

<module name="FileLength">
  <property name="max" value="1"/>
</module>
          

Main config checkstyle.xml:

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
          "https://checkstyle.org/dtds/configuration_1_3.dtd" [
    <!ENTITY common SYSTEM "checkstyle-common.xml">
]>
<module name="Checker">

    &common;

    <module name="TreeWalker">
        <module name="MemberName">
            <property name="format" value="^[a-z][a-zA-Z]+$"/>
        </module>
    </module>

</module>
          

Test config checkstyle-test.xml:

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
          "https://checkstyle.org/dtds/configuration_1_3.dtd" [
    <!ENTITY common SYSTEM "checkstyle-common.xml">
]>
<module name="Checker">

    &common;

    <module name="TreeWalker">
        <module name="MemberName">
            <property name="format" value="_[a-z]"/>
        </module>
    </module>

</module>
          

Target file for validation Test.java:

class Test {
  int i = 0;
}
          

Example of execution for checkstyle.xml. Violation from Check of common.xml is expected, validation of field name is done by main code rules:

$ java -Dcheckstyle.enableExternalDtdLoad=true -classpath checkstyle-XX.X-all.jar \
        com.puppycrawl.tools.checkstyle.Main -c checkstyle.xml Test.java
Starting audit...
[ERROR] Test.java:1: File length is 3 lines (max allowed is 1). [FileLength]
[ERROR] Test.java:2:7: 'i' must match pattern '^[a-z][a-zA-Z]+$'. [MemberName]
Audit done.
Checkstyle ends with 2 errors.
          

Example of execution for checkstyle-test.xml. Violation from Check of common.xml is expected, validation of field name is done by test code rules:

$ java -Dcheckstyle.enableExternalDtdLoad=true -classpath checkstyle-XX.X-all.jar \
          com.puppycrawl.tools.checkstyle.Main -c checkstyle-test.xml Test.java
Starting audit...
[ERROR] Test.java:1: File length is 3 lines (max allowed is 1). [FileLength]
[ERROR] Test.java:2:7: 'i' must match pattern '_[a-z]'. [MemberName]
Audit done.
Checkstyle ends with 2 errors.
          

Attention to Windows users: system properties should be wrapped with ". Example: -D"<property>=<value>"

Property chaining support

Checkstyle supports property expansion within property definitions, also known as property chaining. This feature allows you to define properties using other properties. For example:

checkstyle.dir=/home/user/checkstyle
config.dir=configs
checkstyle.suppressions.file=${checkstyle.dir}/${config.dir}/suppressions.xml
        
You can then use ${checkstyle.suppressions.file} in your checkstyle configuration, which will resolve to /home/user/checkstyle/configs/suppressions.xml.

Notes

Note that property variable expression must be of the form ${expression}.

It is not necessary to define chained properties sequentially.