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

时间:2018-04-02 06:41:26

标签: java maven checkstyle maven-checkstyle-plugin

这是完整的错误:

[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配置文件不包含任何重复的&#39;错误中提到的规则。如何解决此问题并让所有检查通过?

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

4 个答案:

答案 0 :(得分:0)

这意味着不支持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>

更新2:

验证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>

答案 1 :(得分:0)

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

现在,由于您的配置文件未引用此检查,因此必须是Maven Checkstyle插件读取其他配置文件。 Its docsconfiguration应该直接在plugin下。尝试移动configuration XML元素。

答案 2 :(得分:0)

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

maven-checkstyle-plugin 3.1.0

非常感谢

答案 3 :(得分:0)

如果仍然有人遇到此问题,请尝试检查您的JAVA_HOME路径。在我的情况下,使用了JRE而不是JDK。当我修复它时-maven-checkstyle-plugin:3.0.0:check已消失