maven-代码风格检查工具

[TOC]

checkstyle

checkstyle 用于对代码风格进行检查
checkstyle-maven插件
操作示例

mvn clean compile checkstyle:checkstyle

输出(target/site/checkstyle.html)

Summary
Files   Info    Warnings    Errors
24  0   15  0
Files
File    I   W   E
org/foo/base/mongoop/core/CommandAnalysisListener.java  0   2   0
org/foo/base/mongoop/core/op/CommandOp.java 0   8   0
org/foo/base/mongoop/report/OpResult.java   0   4   0
org/foo/base/mongoop/support/HttpApiClient.java 0   1   0

根据结果页面的提示,可对checkstyle异常进行修复。
如果希望对checkstyle进行定制,可通过配置文件定制规则
如下命令:

mvn clean compile checkstyle:checkstyle -Dcheckstyle.config.location=checkStyleConfig.xml

通过-Dcheckstyle.config.location制定checkStyleConfig.xml作为规则配置文件。
以下示例展示了如何屏蔽代码扫描:

  1. checkStyleConfig.xml添加suppression模块:

    <module name="SuppressionFilter">
    <property name="file" value="checkStyleSuppression.xml"/>
    </module>
  2. checkStyleSuppression.xml配置:

    <?xml version="1.0"?>
    <!DOCTYPE suppressions PUBLIC
    "-//Puppy Crawl//DTD Suppressions 1.1//EN"
    "http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
    <suppressions>
    <suppress checks="." files="org[\\/]eclipse1[\\/]californium[\\/]scandium[\\/].*\.java$"/>
    </suppressions>

    此外,还可以通过pom文件指定配置:

    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-checkstyle-plugin</artifactId>
     <version>2.17</version>
     <configuration>
      <configLocation>checkStyleConfig.xml</configLocation>
      <suppressionsLocation>checkStyleSuppressions.xml</suppressionsLocation>
     </configuration>
    </plugin>

    参考文档
    checkstyle的配置说明
    checkstyle配置解析

findbugs

findbugs 项目存在已久,主要用于自动化扫描并提前分析判断代码中可能存在的问题,该项目包含一个gui界面。

findbugs-maven插件
执行如下命令:

mvn clean compile findbugs:findbugs

扫描后会生成target/findbugsXml.xml,该文件记录了扫描后的所有异常。
由于xml文件可读性不高,我们可以借助gui工具完成分析:

clean compile findbugs:findbugs findbugs:findbugsgui

以上命令在执行扫描后会自动打开一个findbugsgui工具,并展现异常结果。
如果希望屏蔽某类代码的检查,可执行:

clean compile findbugs:findbugs -Dfindbugs.excludeFilterFile=findBugsExcludeFilter.xml

findBugsExcludeFilter.xml配置:

<Match>
    <Package name="~org[.]eclipse[.]californium[.]scandium([.].*)?"/>
</Match>

~开头表示采用正则表达式

pmd

pmd可按照一组最佳实践规则对代码进行扫描,并输出针对项目代码中的多个改进建议。

pmd-maven插件
执行如下命令:

mvn clean compile pmd:pmd

扫描后会生成target/site/pmd.html,该文件记录了所有异常。

Files
org/foo/base/mongoop/support/ApplicationContextSupport.java
Violation   Priority    Line
Avoid modifiers which are implied by the context    3   93–95
Avoid modifiers which are implied by the context    3   94

根据结果页面的提示,对pmd扫描结果进行修复。
默认情况下pmd使用内置的5个ruleset规则进行扫描,可以在pom.xml中定制规则:

<build>
  <plugins>
 <plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-pmd-plugin</artifactId>
  <configuration>
   <rulesets>
    <ruleset>/rulesets/java-basic.xml</ruleset>
    <ruleset>/rulesets/java-imports.xml</ruleset>
    <ruleset>/rulesets/java-unusedcode.xml</ruleset>
   </rulesets>
   <excludes>
    <exclude>**org/eclipse/californium/scandium/**</exclude>
   </excludes>
  </configuration>
 </plugin>
  </plugins>
</build>

多模块中的配置

其他

在每次构建时全部执行检查,可串联运行

mvn clean compile checkstyle:checkstyle pmd:pmd findbugs:findbugs findbugs:gui
img_9b09a36f6de95886f52ce82fa1e89c88.jpe

作者: zale

出处: http://www.cnblogs.com/littleatp/, 如果喜欢我的文章,请关注我的公众号

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出 原文链接  如有问题, 可留言咨询.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值