1. mvn checkstyle plugin 的使用
可以不做任何设置, 直接运行mvn checkstyle:checkstyle即可得到checkstyle的报告于 target/site/checkstyle.html and target/checkstyle-result.xml. 所使用的checkstyle的检查规则是sun_checks.xml - Sun Microsystems Definition (default).
如果想使用自定义的规则, 在maven3.0以上的版本需要在pom.xml的<build>标签下增加checkstyle plugin的设置, 不能使用官网上介绍的在pom.xml的<reporting>标签的设置:
而应该是
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<configuration>
<configLocation>YOUR_checkstyle.xml</configLocation>
<failsOnError>false</failsOnError>
<linkXRef>false</linkXRef>
</configuration>
</plugin>
</plugins>
</build>
上面是对于单module的project而言的。
对于有多个module的project, 需要做额外的设置,参考https://maven.apache.org/plugins/maven-checkstyle-plugin/examples/multi-module-config.html
同样的在根目录的pom.xml添加
+ <artifactId>maven-checkstyle-plugin</artifactId>
+ <version>2.17</version>
+ <configuration>
+ <configLocation>rules/YOUR_checkstyle.xml</configLocation>
+ </configuration>
+ <dependencies>
+ <dependency>
+ <groupId>com.example.build-tools</groupId>
+ <artifactId>build-tools</artifactId>
+ <version>1.0</version>
+ </dependency>
+ </dependencies>
+ </plugin>
然后在运行的时候,使用mvn compile -U checkstyle:checkstyle即可在各个module下面找到target/site/checkstyle.html and target/checkstyle-result.xml
的结果了。
另一种解决方法如下,
http://rolf-engelhard.de/2013/07/using-a-custom-checkstyle-ruleset-on-a-maven-multi-module-project/
同样的findbugs & PMD
<plugin>
<groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-pmd-plugin</artifactId>
+ <version>3.6</version>
+ <configuration>
+ <rulesets>
+ <ruleset>pmd-rules.xml</ruleset> // 自定义的规则集
+ </rulesets>
+ </configuration>
+
+ </plugin>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>findbugs-maven-plugin</artifactId>
+ <version>3.0.3</version>
+ <configuration>
+ <excludeFilterFile>findbugs-exclude.xml</excludeFilterFile>
+ </configuration>
+ </plugin>
然后运行 mvn compile -U findbugs:findbugs & mvn compile -U pmd:pmd