我一直在努力让 Checkstyle 在 Eclipse Indigo IDE 的 Maven 中工作一段时间。最后,我想我应该就此寻求一些专家的建议。
我正在使用 Eclipse Indigo 并尝试配置 Checkstyle 以在 Maven 中运行。
下面是我的 pom.xml 的片段。只有
checkstyle:checkstyle
正在工作并创建报告。
<profile>
<id>checkstyle-profile</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
<configLocation>${basedir}/src/main/resources/maven_checks.xml</configLocation>
</configuration>
<executions>
<execution>
<id>checkstyle-check</id>
<goals>
<goal>check</goal>
</goals>
<phase>compile</phase> <!-- Default is "verify" -->
<configuration>
<violationSeverity>error</violationSeverity>
<maxAllowedViolations>7000</maxAllowedViolations>
<failOnViolation>true</failOnViolation>
<failsOnError>true</failsOnError>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<configLocation>${basedir}/src/main/resources/maven_checks.xml</configLocation>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>2.3</version>
</plugin>
</plugins>
</reporting>
一些不起作用的事情是:
checkstlye:check
。我收到以下错误。我应该运行什么目标才能运行 checkstyle:check
。
无法在 zzz-web 项目上执行目标 org.apache.maven.plugins:maven-checkstyle-plugin:2.9.1:check
(default-cli):您有 5950 个 Checkstyle 违规希望尽快得到答复。
提前致谢。 瓦尔玛
您已将
check
的 maven checkstyle plugin
目标绑定到 compile
阶段。在这种情况下,您需要运行 mvn compile
才能使用您的配置。运行 mvn checkstyle:check
将使用默认配置。这看起来是上述第 1 项和第 2 项最有可能的情况。
即使您要运行
mvn compile
,上述配置仍然会因两个配置条目failOnViolation
和failOnError
而导致构建失败,因为它们都设置为true
。只要违规数量少于 mvn compile
,删除这些条目并运行 7000
就应该通过构建。
googe_checks.xml 必须位于存在 pom.xml 的项目中。 mvn checkstyle:检查
<properties>
<checkstyle.config.location>google_checks.xml</checkstyle.config.location>
<checkstyle.violationSeverity>warning</checkstyle.violationSeverity>
<checkstyle.consoleOutput>true</checkstyle.consoleOutput>
</properties>
<build>
<pluginManagement>
<plugins>
<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>`
<properties>
<checkstyle.config.location>google_checks.xml</checkstyle.config.location>
<checkstyle.violationSeverity>warning</checkstyle.violationSeverity>
<checkstyle.consoleOutput>true</checkstyle.consoleOutput>
</properties>
<build>
<pluginManagement>
<plugins>
<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>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
`
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
1.自定义检查样式的配置位置将被忽略,并且始终默认为 Sun 检查样式。
为此,请使用以下标签:
<properties<checkstyle.config.location>properties/checkstyle.xml</checkstyle.config.location> </properties>
在您使用 checkstyle 的项目的 POM.xml 中。此行将位于 pom.xml 标签的顶部和下方。
<version>0.0.1-SNAPSHOT</version>
您需要将此 xml 粘贴到您的项目中 https://github.com/checkstyle/checkstyle/blob/checkstyle-8.21/src/main/resources/sun_checks.xml + 添加此插件。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<configuration>
<configLocation>sun_checks.xml</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
</configuration>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>