尝试根据guidelines on the apache site在maven pom中设置自定义checkstyle配置在非常简单的情况下不起作用。
我使用 Maven 推荐的目录布局(即 src/main/java/、src/main/resources)创建了一个项目 MyProject,单个文件 MyClass.java:
package com.myproject;
public class MyClass {
public static void main(String[] args) {
System.out.println("This line is longer than 80 characters which returns an error in sun_checks.xml, however my_checks.xml allows for much longer lines and will not return a long line error.");
}
}
空的 checkstyle 文件 my_checks.xml:
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.2//EN"
"http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
<module name="Checker">
<property name="severity" value="warning"/>
<module name="TreeWalker">
</module>
</module>
以及根据指南中的规范的 pom 文件:
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myproject</groupId>
<artifactId>A_Project</artifactId>
<name>A Project</name>
<version>1.0.0</version>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.8</version>
<configuration>
<configLocation>my_checks.xml</configLocation>
</configuration>
</plugin>
</plugins>
</reporting>
</project>
运行“mvn -X checkstyle:checkstyle”使用 sun_checks.xml(默认),而不是使用 my_checks.xml 文件中的配置,这可以通过生成的 checkstyle 错误和调试输出(例如'[DEBUG] request.getConfigLocation() config/sun_checks.xml')。
我知道 my_checks.xml 是有效的,因为可以使用 Carboni 在 previous stack overflow post 中概述的策略在属性中更改 checkstyle.config.location ,但这会在迁移到多模块项目时导致问题,并且与“官方”apache maven checkstyle 指令不同。
最佳答案
是的,我发现 apache 网站上的指南已经过时了。通过在互联网上搜寻并将一些碎片拼凑在一起,我成功地解决了这个问题。
移至<build>
标签工作了一段时间,但当更新到最新的检查样式时,它再次被忽略。我发现我必须设置一个属性:
<project ...>
....
<properties>
<checkstyle.config.location>properties/checkstyle-configuration.xml</checkstyle.config.location>
</properties>
<build>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.9.1</version>
</plugin>
</plugins>
</build>
...
</project>
这是我如何创建自己的 checkstyle 自定义检查示例的一部分:
http://blog.blundellapps.com/create-your-own-checkstyle-check/
取自此处的源代码:
https://github.com/blundell/CreateYourOwnCheckStyleCheck
关于Maven Checkstyle configLocation 被忽略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8975096/