我无法让maven checkstyle在构建或验证时运行,尽管直接从checkstyle website复制和粘贴代码。
我可以看到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>发布于 2021-03-09 23:51:07
弄清楚了:我把代码块放在了<pluginManagement>中。把它移出那里,它立刻就起作用了。我希望,maven能在他们的网站上有更完整的例子。
发布于 2021-03-05 10:24:30
您需要将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。
https://stackoverflow.com/questions/66480990
复制相似问题