Exclude checkstyle FileLength module/check for specific files?

111 views Asked by At

When I run checkstyle through pre-commit, it gives the following warnings:

checkstyle...............................................................Failed
- hook id: checkstyle
- exit code: 4

Starting audit...
[ERROR] /some_path/AnotherSomeFile.java:1: File length is 1,950 lines (max allowed is 1,000). [FileLength]
[ERROR] /some_path/someFile.java:1: File length is 1,676 lines (max allowed is 1,000). [FileLength]
Audit done.
Checkstyle ends with 4 errors.

I have disabled the other checkstyle errors for those files by including the line:

<module name="SuppressionCommentFilter"/>

inside the <module name="TreeWalker"> module (and including // CHECKSTYLE:OFF at the top of each file that I want to ignore with checkstyle. (As explained in this answer. However, the

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

check needs to be above/outside of the TreeWalker module inside the checkstyle.xml file. That means the SuppressionCommentFilter module does not apply to the FileLength module inside the checkstyle.xml.

Attempts

  • I tried moving the FileLength module inside the TreeWalker module, and that is not allowed.
  • I tried copying the SuppressionCommentFilter into the parent of TreeWalker/at the same level/below the FileLength check module, and that is not allowed.

Question

How can I exclude the FileLength module for 4 files at src/some_path/file_one.java, src/some_path/file_two.java etc. inside the checkstyle.xml?

1

There are 1 answers

1
devatherock On BEST ANSWER

You can use the SuppressionFilter to exclude the FileLength check for specific files. The suppression filter accepts a suppressions.xml config file. The files to be excluded will be supplied as a regex within this config file. The config files would look something like below:

checkstyle.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC
          "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
          "https://checkstyle.org/dtds/configuration_1_3.dtd">

<module name="Checker">
  <module name="SuppressionFilter">
    <property name="file" value="suppressions.xml"/>
    <property name="optional" value="false"/>
  </module>
  <module name="FileLength">
    <property name="max" value="1000"/>
  </module>
  <module name="TreeWalker">
    <module name="SuppressionCommentFilter"/>
  </module>
</module>

suppressions.xml:

<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
  "-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
  "https://checkstyle.org/dtds/suppressions_1_2.dtd">

<suppressions>
  <suppress checks="FileLengthCheck" files="file_one.*|file_two.*"/>
</suppressions>