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文件中增加如下插件:

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中 增加配置。

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

过滤类:

1
2
3
4
5
6
过滤包:(老项目在接入findbugs时,尽量不要过滤整个包,而是把现有的类逐个过滤即可,这样不妨碍新增加的文件参与扫描)

1
2
3
4
5
6
过滤方法:

1
2
3
4
5
6
7
过滤异常:

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

Original: https://www.cnblogs.com/suizhikuo/p/15969063.html
Author: 勤学如春起之苗
Title: maven项目集成findbugs详解

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/546346/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球