[TOC]
checkstyle
checkstyle 用于对代码风格进行检查 checkstyle-maven插件 操作示例
mvn clean compile checkstyle:checkstyle
输出(target/site/checkstyle.html)
Summary
FilesInfoWarningsErrors
240150
Files
FileIWE
org/foo/base/mongoop/core/CommandAnalysisListener.java020
org/foo/base/mongoop/core/op/CommandOp.java080
org/foo/base/mongoop/report/OpResult.java040
org/foo/base/mongoop/support/HttpApiClient.java010
根据结果页面的提示,可对checkstyle异常进行修复。 如果希望对checkstyle进行定制,可通过配置文件定制规则。 如下命令:
mvn clean compile checkstyle:checkstyle -Dcheckstyle.config.location=checkStyleConfig.xml
通过***-Dcheckstyle.config.location***制定checkStyleConfig.xml作为规则配置文件。 以下示例展示了如何屏蔽代码扫描:
checkStyleConfig.xml添加suppression模块:
checkStyleSuppression.xml配置:
/p>
"-//Puppy Crawl//DTD Suppressions 1.1//EN"
"http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
此外,还可以通过pom文件指定配置:
org.apache.maven.plugins
maven-checkstyle-plugin
2.17
checkStyleConfig.xml
checkStyleSuppressions.xml
findbugs
findbugs 项目存在已久,主要用于自动化扫描并提前分析判断代码中可能存在的问题,该项目包含一个gui界面。
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配置:
~开头表示采用正则表达式
pmd
pmd可按照一组最佳实践规则对代码进行扫描,并输出针对项目代码中的多个改进建议。
mvn clean compile pmd:pmd
扫描后会生成target/site/pmd.html,该文件记录了所有异常。
Files
org/foo/base/mongoop/support/ApplicationContextSupport.java
ViolationPriorityLine
Avoid modifiers which are implied by the context393–95
Avoid modifiers which are implied by the context394
根据结果页面的提示,对pmd扫描结果进行修复。 默认情况下pmd使用内置的5个ruleset规则进行扫描,可以在pom.xml中定制规则:
org.apache.maven.plugins
maven-pmd-plugin
/rulesets/java-basic.xml
/rulesets/java-imports.xml
/rulesets/java-unusedcode.xml
**org/eclipse/californium/scandium/**
其他
在每次构建时全部执行检查,可串联运行
mvn clean compile checkstyle:checkstyle pmd:pmd findbugs:findbugs findbugs:gui