Checkstyle 在构建或验证阶段不起作用

Checkstyle not working in build or verify phase

尽管直接从 checkstyle website.

复制和粘贴代码,但我无法在构建或验证时将 maven checkstyle 设置为 运行

我可以看到 checkstyle 工作正常并在 运行 宁 mvn checkstyle:check 时发现问题,但在 clean compile verify 等任何阶段都没有。这些阶段只是编译好,没有发现问题,完全忽略了 checkstyle。我建立了一个最小的例子 here。 pom.xml如下:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <groupId>checkstyle-test</groupId>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>15</java.version>
        <maven.compiler.source>15</maven.compiler.source>
        <maven.compiler.target>15</maven.compiler.target>
    </properties>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>core</artifactId>
    <version>0.0.2</version>
    <packaging>jar</packaging>

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

</project>

您需要将 Checkstyle 执行绑定到 verify 阶段,如下所示:

...
    <execution>
        <id>checkstyle-check</id>
        <phase>verify</phase>
        <configuration>
            <sourceDirectories>${project.build.sourceDirectory}</sourceDirectories>
            <configLocation>config/checkstyle_checks.xml</configLocation>
            <encoding>UTF-8</encoding>
        </configuration>
        <goals>
            <goal>check</goal>
        </goals>
    </execution>
...

此片段摘自我的一个项目,您可以查看整个 pom.xml here

想通了:我在 <pluginManagement> 中找到了块。将它移出那里它立即起作用。我希望 Maven 在他们的网站上有更完整的示例。