maven-代碼風格檢查工具

checkstyle

checkstyle 用於對代碼風格進行檢查
checkstyle-maven插件
操做示例java

mvn clean compile checkstyle:checkstyle

輸出(target/site/checkstyle.html)git

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進行定製,可經過配置文件定製規則
以下命令:github

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文件指定配置:apache

<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配置解析app

findbugs

findbugs 項目存在已久,主要用於自動化掃描並提早分析判斷代碼中可能存在的問題,該項目包含一個gui界面。eclipse

findbugs-maven插件
執行以下命令:maven

mvn clean compile findbugs:findbugs

掃描後會生成target/findbugsXml.xml,該文件記錄了掃描後的全部異常。
因爲xml文件可讀性不高,咱們能夠藉助gui工具完成分析:wordpress

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
相關文章
相關標籤/搜索