自定义 checkstyle 配置忽略标准规则?

Custom checkstyle configuration ignores standard rules?

我正在为我的项目设置代码风格检查。我想自定义一些标准规则。为此,我创建了一个名为 checkstyle.xml:

的文件
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
    "-//Puppy Crawl//DTD Check Configuration 1.3//EN" 
    "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
    <module name="SuppressionFilter">
        <property name="file" value="${checkstyle.suppressions.file}"/>
    </module>
    <module name="TreeWalker">
        <module name="HiddenField">
            <property name="ignoreSetter" value="true"/>
            <property name="ignoreConstructorParameter" value="true"/>
        </module>
    </module>
</module>

在项目的pom.xml中,我配置了maven checkstyle插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>2.17</version>
    <executions>
        <execution>
            <phase>install</phase>
            <goals>
                <goal>checkstyle</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <consoleOutput>${checkstyle.printErrors}</consoleOutput>
        <configLocation>checkstyle.xml</configLocation>
        <suppressionsLocation>checkstyle-suppressions.xml</suppressionsLocation>
        <suppressionsFileExpression>checkstyle.suppressions.file</suppressionsFileExpression>
    </configuration>
</plugin>

现在,仅应用 HiddenField 检查,sun_checks.xml 中的所有其他检查均已关闭。如果我在插件设置中关闭自定义 checkstyle.xml,则会再次应用标准检查。

问题是,如何应用标准检查,修改其中一项或多项的设置?

您可以通过在本地复制 standard configuration file 作为您自己的 checkstyle.xml,然后修改它来做到这一点。

请务必使用适合您的 Checkstyle 版本的版本。您使用的 Maven Checkstyle 插件 2.17 使用 Checkstyle 6.11.2 (reference)。只需修改配置文件URL中的版本号即可下载正确的版本。