mvn checkstyle/findbugs/pmd 插件使用

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>标签的设置:

  1. <reporting>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.apache.maven.plugins</groupId>
  5. <artifactId>maven-checkstyle-plugin</artifactId>
  6. <version>2.17</version>
  7. <configuration>
  8. <configLocation>checkstyle.xml</configLocation>
  9. </configuration>
  10. </plugin>
  11. </plugins>
  12. </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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值