FindBugs™手册 http://findbugs.sourceforge.net/manual/index.html
FindBugs是一个静态分析工具,它将**字节码(因此需要先编译)**与一组缺陷模式进行对比以发现可能的问题。有了静态分析工具,就可以在不实际运行程序的情况对软件进行分析。简而言之,FindBugs其实就是对编译后的class进行扫描,藉以发现一些隐藏的bug。比较典型的,如引用了空指针(null pointer), 特定的资源(db connection)未关闭,等等。如果用人工检查的方式,这些bug可能很难才会被发现,或许直到运行时才发现…所以当我们用findbugs除掉了这些典型的bug后,我们系统的稳定度将会上一个新的台阶。
pom.xml 添加插件plugin
<build> <plugins> <!-- findbugs插件 --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>findbugs-maven-plugin</artifactId> <version>3.0.4</version> <configuration> <!-- 设置分析工作的等级,可以为min、default和max --> <effort>default</effort> <!-- Low、Medium和High (Low最严格) --> <threshold>Medium</threshold> <failOnError>true</failOnError> <includeTests>true</includeTests> <!--findbugs需要忽略的错误的配置文件--> <excludeFilterFile>findbugs/findbugs-exclude-filter.xml</excludeFilterFile> </configuration> <executions> <execution> <id>run-findbugs</id> <!-- 在package 阶段触发执行findbugs检查,比如执行 mvn clean package --> <phase>package</phase> <goals> <goal>check</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
findbugs-exclude-filter.xml 内容示例
<?xml version="1.0" encoding="UTF-8"?> <FindBugsFilter> <Match> <Class name="~.*\.(model|entity|vo)\..*" /> <Method name="~(get.*|set.*)" /> <!-- <Bug pattern="EI_EXPOSE_REP" /> --> <Bug code="EI,EI2" /> </Match> <Match> <Class name="~.*\.Authorization" /> <Bug code="EI,EI2" /> </Match> </FindBugsFilter>
可以添加findbugs检查规则文件来使用用户自己的规则
<configuration> <excludeFilterFile>findbugs-exclude.xml</excludeFilterFile> <includeFilterFile>findbugs-include.xml</includeFilterFile> </configuration>
mvn 命令
mvn clean package // 只有打包才触发findbugs扫码,由上面的配置设定。
mvn findbugs:help 查看findbugs插件的帮助
mvn findbugs:check 检查代码是否通过findbugs检查,如果没有通过检查,检查会失败,但检查不会生成结果报表
mvn findbugs:findbugs 检查代码是否通过findbugs检查,如果没有通过检查,检查不会失败,会生成结果报表保存在target/findbugsXml.xml文件中
mvn findbugs:gui 检查代码并启动gui界面来查看结果
如果出现下面的信息,说明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
如果在打包的过程中发现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
<!-- findbugs插件 --> <plugins> <build> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>findbugs-maven-plugin</artifactId> <version>3.0.5</version> <configuration> ... <excludeFilterFile>conf/findbugs-exclude-filter.xml</excludeFilterFile> </configuration> <executions> <execution> ... </execution> </executions> </plugin> </plugins> </build>
过滤类:
<?xml version="1.0" encoding="UTF-8"?> <FindBugsFilter> <Match> <Class name="com.missxxxx.proscenium.plugin.misconf.ProsceniumConfig" /> </Match> </FindBugsFilter>
过滤包:
(老项目在接入findbugs时,尽量不要过滤整个包,而是把现有的类逐个过滤即可,这样不妨碍新增加的文件参与扫描)
<?xml version="1.0" encoding="UTF-8"?> <FindBugsFilter> <Match> <Package name="com.missxxxx.proscenium.plugin.misconf" /> </Match> </FindBugsFilter>
过滤方法:
<?xml version="1.0" encoding="UTF-8"?> <FindBugsFilter> <Match> <Class name="com.missxxxx.proscenium.service.CartShowServiceImpl" /> <Method name="getResultData"></Method> </Match> </FindBugsFilter>
过滤异常:
如果有多个包/类/方法需要过滤,就加多个Match标签即可。
<?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>
如果有多个包/类/方法需要过滤,就加多个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
ven-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
本文原链接:https://blog.csdn.net/so_geili/article/details/89169845