maven-代码风格检查工具

摘要:
DOCTYPEsuppressionsPUBLIC"-//PuppyCrawl//DTDSuppressions1.1//EN""http://www.puppycrawl.com/dtds/suppressions_1_1.dtd"˃此外,还可以通过pom文件指定配置:org.apache.maven.pluginsmaven-checkstyle-plugin2.17checkStyleConfig.xmlcheckStyleSuppressions.xml参考文档checkstyle的配置说明checkstyle配置解析findbugsfindbugs项目存在已久,主要用于自动化扫描并提前分析判断代码中可能存在的问题,该项目包含一个gui界面。由于xml文件可读性不高,我们可以借助gui工具完成分析:cleancompilefindbugs:findbugsfindbugs:findbugsgui以上命令在执行扫描后会自动打开一个findbugsgui工具,并展现异常结果。pmd-maven插件执行如下命令:mvncleancompilepmd:pmd扫描后会生成target/site/pmd.html,该文件记录了所有异常。
目录

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>
  1. 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

免责声明:文章转载自《maven-代码风格检查工具》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇OpenStack是什么,OpenStack详解[转载]fullPage.js中文api 配置参数~下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

微信小程序 webview直接关闭所有回到小程序

解决方案:通过微信浏览器监控返回键和H5跳转小程序。...

django的优缺点(非原创)

Django做了很多。使用它快速开发一些Web应用程序是很好的。因此,在一些人眼中,Django只不过是一种灵丹妙药,但对一些人来说,它也是一种毒药和剧毒。Django开发人员也讨论并试图支持SQLAlchemy,但最终放弃了。据估计,成本太高,很难与Django的其他模块集成。尽管Django的ORM不如SQLAlchemy强大,但它并不弱。Django的...

Jmeter中获取返回结果中的值

在jmeter的测试中,通常需要在下一个请求中使用上一个请求的返回值。如何获得返回值非常重要。插件下载地址为:http://jmeter-plugins.org/wiki/JSONPathExtractor/下载后,将lib文件夹放在jmeter目录中。...

com.aliyun.openservices.shade.com.alibaba.fastjson.JSONException: exepct '[', but {, pos 1, line 1, column 2

错误报告的原因:您放置了一个非List对象,但希望从packagetest中取出List对象;导入java.text。SimpleDateFormat;导入java.util。阵列列表;导入java.util。日期导入java.util。列表importcom.alibaba.fastjson。JSON;导入com.alibaba.fastj...

Windows Server 2008 R2 备份与恢复详细实例

Windows ftp服务可以在百度内置,非常简单。)1.首先安装windows server 2008R2的备份功能。查找Windows的“服务器管理器”。下图显示了我的服务器的情况。双击它。备份完成后,我们卸载qq并删除磁盘F的数据。Linux服务器在没有密码的情况下构建Samba登录,并使用yum进行安装。...

sqlserver 计算 百分比

selectltrim+'%'As百分比NUMERIC(P,S)P的默认值是:38S的默认值是:-84~127numeric(a,b)函数有两个参数,前面一个为总的位数,后面一个参数是小数点后的位数,例如numeric(5,2)是总位数为5,小数点后为2位的数,也就是说这个字段的整数位最大是3位。...