maven-checkstyle-plugin:3.0.0:check: checkstyle 配置期间失败:无法初始化模块 StrictDuplicateCode

maven-checkstyle-plugin:3.0.0:check: Failed during checkstyle configuration: cannot initialize module StrictDuplicateCode

这是完整的错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:3.0.0:check (default) on project abc-123-micro-service: Failed during checkstyle configuration: cannot initialize module StrictDuplicateCode - Unable to instantiate 'StrictDuplicateCode' class, it is also not possible to instantiate it as .StrictDuplicateCode, StrictDuplicateCodeCheck, .StrictDuplicateCodeCheck. Please recheck that class name is specified as canonical name or read how to configure short name usage http://checkstyle.sourceforge.net/config.html#Packages. Please also recheck that provided ClassLoader to Checker is configured correctly. -> [Help 1]

我的 checkstyle 插件的 maven 配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>3.0.0</version>
    <dependencies>
        <dependency>
            <groupId>com.puppycrawl.tools</groupId>
            <artifactId>checkstyle</artifactId>
            <version>8.8</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <id>validate</id>
            <phase>validate</phase>
            <configuration>
                <configLocation>checkstyle.xml</configLocation>
                <encoding>UTF-8</encoding>
                <consoleOutput>true</consoleOutput>
                <failsOnError>true</failsOnError>
            </configuration>
            <goals>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
</plugin>

我的 checkstyle.xml 配置文件不包含错误中提到的任何 'duplicate' 规则。我怎样才能解决这个问题并让所有检查通过?

编辑: checkstyle.xml的内容:https://pastebin.com/kuJAFiiP

表示不支持StrictDuplicateCodeCheck

您可以编辑配置文件 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="TreeWalker">
        <module name="AvoidStarImport"/>
        <module name="ConstantName"/>
        <module name="EmptyBlock"/>
    </module>
</module>

更新二:

验证 checkstyle.xml 的位置。它可能应该在项目根目录中。

更新 3:

很高兴您已经找到了解决方案。这里是pom文件,供参考。

<modelVersion>4.0.0</modelVersion>
    <groupId>clucinvt</groupId>
    <artifactId>s02checkstyle</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>validate</id>
                        <phase>validate</phase>
                        <configuration>
                            <configLocation>checkstyle.xml</configLocation>
                            <encoding>UTF-8</encoding>
                            <consoleOutput>true</consoleOutput>
                            <failsOnError>true</failsOnError>
                            <linkXRef>false</linkXRef>
                        </configuration>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

错误消息 cannot initialize module StrictDuplicateCode 表示 Checkstyle 无法初始化它在遍历配置文件时发现的检查。此外,StrictDuplicateCode 检查在 Checkstyle 6.2 中是 removed

既然你的配置文件没有引用这个检查,那肯定是 Maven Checkstyle 插件读取了一些其他的配置文件。 Its docsconfiguration 应该直接在 plugin 下。尝试将 configuration XML 元素向上移动。

我刚刚将版本从 3.0.0 更新到 3.1.0,它对我有用。

maven-checkstyle-plugin 3.1.0

非常感谢

如果有人仍然面临这个问题 - 请尝试检查您的 JAVA_HOME 路径。在我的例子中,使用了 JRE 而不是 JDK。当我修复它时 - maven-checkstyle-plugin:3.0.0:check 的问题已经消失

我遇到了类似的问题,我在 pom.xml 中将 org.apache.maven.plugins 插件的版本从 3.0.0 更改为 3.1.0 并使用 clean install -Dcheckstyle.skip 作为从 eclipse 生成 jar 的命令,它对我有用。