maven項目集成findbugs詳解


文章目錄
0、概述
一、接入方式
二、如何使用
方式一、在控制台中執行打包命令
方式二、使用IntelliJ IDEA的maven工具(其他IDE用戶忽略)
三、bug詳情查看
四、忽略指定的包、類、類中的方法
步驟一、在pom.xml中 增加配置。
步驟二、增加配置文件,用於忽略指定的包、類、方法、異常。
五、參考鏈接:
0、概述
  FindBugs是一個靜態分析工具,它將字節碼(因此需要先編譯)與一組缺陷模式進行對比以發現可能的問題。有了靜態分析工具,就可以在不實際運行程序的情況對軟件進行分析。簡而言之,FindBugs其實就是對編譯后的class進行掃描,藉以發現一些隱藏的bug。比較典型的,如引用了空指針(null pointer), 特定的資源(db connection)未關閉,等等。如果用人工檢查的方式,這些bug可能很難才會被發現,或許直到運行時才發現…所以當我們用findbugs除掉了這些典型的bug后,我們系統的穩定度將會上一個新的台階。

  另一方面,對於一個初入職場的新coder而言,適應findbugs不僅能減少bug的數量,更有利於提升編碼能力,寫出高質量的代碼,從而養成較好的編程習慣。

一、接入方式
  在maven工程的pom.xml文件中增加如下插件:

<!-- findbugs插件 -->
<plugins>
<build>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.5</version>
<configuration>
<!-- 設置分析工作的等級,可以為Min、Default和Max -->
<effort>Low</effort>
<!-- Low、Medium和High (Low最嚴格) High只掃描嚴重錯誤。建議用Medium-->
<threshold>Medium</threshold>
<failOnError>true</failOnError>
<includeTests>true</includeTests>
</configuration>
<executions>
<execution>
<id>run-findbugs</id>
<!-- 在package(也可設為compile) 階段觸發執行findbugs檢查,比如執行 mvn clean package -->
<phase>package</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
二、如何使用
方式一、在控制台中執行打包命令
  在項目的根目錄下執行如下命令:

mvn clean package // 只有打包才觸發findbugs掃碼,由上面的配置設定。
1
方式二、使用IntelliJ IDEA的maven工具(其他IDE用戶忽略)

  如果出現下面的信息,說明findbugs沒有發現bug,打包成功。(上圖是演示 打包失敗的案例)

[INFO] <<< findbugs-maven-plugin:3.0.4:check (run-findbugs) < :findbugs @ pmp-proscenium <<<
[INFO]
[INFO]
[INFO] --- findbugs-maven-plugin:3.0.4:check (run-findbugs) @ pmp-proscenium ---
[INFO] BugInstance size is 0
[INFO] Error size is 0
[INFO] No errors/warnings found
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.627 s
[INFO] Finished at: 2019-04-09T21:38:35+08:00
[INFO] ------------------------------------------------------------------------
[WARNING] The requested profile "nexus" could not be activated because it does not exist.

Process finished with exit code 0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
三、bug詳情查看
  如果在打包的過程中發現bug,則控制台會輸出bug的數量和查看bug詳情的方式。下面是博主開發中的日志樣例:

[INFO]

To see bug detail using the Findbugs GUI, use the following command "mvn findbugs:gui"

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13.037 s
[INFO] Finished at: 2019-04-09T18:50:48+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:findbugs-maven-plugin:3.0.4:check (run-findbugs) on project pmp-proscenium: failed with 1 bugs and 0 errors -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
上面的日志提示,如果想查看bug詳情,可以使用如下命令:“mvn findbugs:gui”
打開一個終端,切換到項目的根目錄下,執行"mvn findbugs:gui",會出現如下的窗口。

按照bug詳情中的解釋,修改相應的代碼。
注意點:如果bug數太多,有些bug根本不需要findbugs掃碼。可以通過配置文件的方式過濾掉相應的包、類 、方法和異常 。下面介紹。
四、忽略指定的包、類、類中的方法
步驟一、在pom.xml中 增加配置。
<!-- findbugs插件 -->
<plugins>
<build>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.5</version>
<configuration>
<!-- 設置分析工作的等級,可以為Min、Default和Max -->
<effort>Low</effort>
<!-- Low、Medium和High (Low最嚴格) High只掃描嚴重錯誤。建議用Medium-->
<threshold>Medium</threshold>
<failOnError>true</failOnError>
<includeTests>true</includeTests>
<!--findbugs需要忽略的錯誤的配置文件-->
<excludeFilterFile>conf/findbugs-exclude-filter.xml</excludeFilterFile>
</configuration>
<executions>
<execution>
<id>run-findbugs</id>
<!-- 在package(也可設為compile) 階段觸發執行findbugs檢查,比如執行 mvn clean package -->
<phase>package</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
步驟二、增加配置文件,用於忽略指定的包、類、方法、異常。
新建conf/findbugs-exclude-filter.xml 文件,路徑與src同級。


配置文件的用法如下:
詳細的過濾規則可以參見官網: http://findbugs.sourceforge.net/manual/filter.html

過濾類:
<?xml version="1.0" encoding="UTF-8"?>
<FindBugsFilter>
<Match>
<Class name="com.missxxxx.proscenium.plugin.misconf.ProsceniumConfig" />
</Match>
</FindBugsFilter>
1
2
3
4
5
6
過濾包:(老項目在接入findbugs時,盡量不要過濾整個包,而是把現有的類逐個過濾即可,這樣不妨礙新增加的文件參與掃描)
<?xml version="1.0" encoding="UTF-8"?>
<FindBugsFilter>
<Match>
<Package name="com.missxxxx.proscenium.plugin.misconf" />
</Match>
</FindBugsFilter>
1
2
3
4
5
6
過濾方法:
<?xml version="1.0" encoding="UTF-8"?>
<FindBugsFilter>
<Match>
<Class name="com.missxxxx.proscenium.service.CartShowServiceImpl" />
<Method name="getResultData"></Method>
</Match>
</FindBugsFilter>
1
2
3
4
5
6
7
過濾異常:
<?xml version="1.0" encoding="UTF-8"?>
<FindBugsFilter>
<Match>
<!--裝箱后拆箱緊接着裝箱,忽略不處理 -->
<!-- Boxed value is unboxed and then immediately reboxed-->
<Package name="~.*" />
<Bug pattern="BX_UNBOXING_IMMEDIATELY_REBOXED" />
</Match>
</FindBugsFilter>
1
2
3
4
5
6
7
8
9
如果有多個包/類/方法需要過濾,就加多個Match標簽即可。

五、參考鏈接:
官網:https://gleclaire.github.io/findbugs-maven-plugin/usage.html
bug描述:http://findbugs.sourceforge.net/bugDescriptions.html
https://www.cnblogs.com/xuehanyu/p/4520816.html
https://blog.csdn.net/jokes000/article/details/7872849
https://blog.csdn.net/rainbow702/article/details/54138155
————————————————
版權聲明:本文為CSDN博主「GNG」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/so_geili/article/details/89169845


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM