Checkstyle LineLength 配置不起作用

Checkstyle LineLength configuration not working

在尝试将 maven-checkstyle-plugin 添加到我的 Java 项目时,我遇到了一些奇怪的问题。 checkstyle 版本为 3.1.0,即使用 checkstyle 版本 8.19。下面是项目正在使用的checkstyle.xml

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
          "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="TreeWalker">
    <module name="LineLength">
        <property name="max" value="120"/>
    </module>
</module>

和我在 pom.xml

上的配置
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <configLocation>checkstyle.xml</configLocation>
    </configuration>
</plugin>

现在,当我 运行 mvn checkstyle:check 时显示以下消息:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:3.1.0:check (default-cli) on project myproject: Failed during checkstyle configuration: LineLength is not allowed as a child in Checker -> [Help 1]

如果我更新检查样式使其看起来像这样

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
          "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="LineLength">
        <property name="max" value="120"/>
</module>

我收到以下消息

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:3.1.0:check (default-cli) on project myproject: Failed during checkstyle configuration: Property 'max' does not exist, please check the documentation -> [Help 1]

我无法推断发生了什么,因为对我来说,错误消息具有误导性。我在这个配置上缺少什么?

与您的示例的 1.3 DTD 匹配的结构如下(即,您似乎缺少 Checker 模块)。

<module name="Checker">
    <module name="TreeWalker">
        <module name="LineLength">
            <property name="max" value="120"/>
        </module>
    </module>
</module>

实际上,在新版本中,LineLength 不应再位于 TreeWalker 之下,而应直接位于 Checker 之下。 看这里: https://github.com/checkstyle/eclipse-cs/issues/190

我在 Visual Studio 代码中收到此错误,我需要确保我的 checkstyle.xml 版本与 Visual Studio 代码使用的 checkstyle 版本匹配。

我转到扩展->CheckStyle->设置->版本并将其设置为匹配适当的版本。我只是将版本设置为与我的 build.gradle.kts 文件中定义的版本相匹配。