maven项目集成findbugs详解

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>

二、如何使用

方式一、在控制台中执行打包命令

  在项目的根目录下执行如下命令:

mvn clean package         // 只有打包才触发findbugs扫码,由上面的配置设定。

方式二、使用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

三、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
  • 上面的日志提示,如果想查看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. 新建conf/findbugs-exclude-filter.xml 文件,路径与src同级。
    在这里插入图片描述

  2. 配置文件的用法如下:
    详细的过滤规则可以参见官网: 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>
  • 过滤包:(老项目在接入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>
  • 过滤异常:
<?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标签即可。


五、参考链接:

  1. 官网:https://gleclaire.github.io/findbugs-maven-plugin/usage.html
  2. bug描述:http://findbugs.sourceforge.net/bugDescriptions.html
  3. https://www.cnblogs.com/xuehanyu/p/4520816.html
  4. https://blog.csdn.net/jokes000/article/details/7872849
  5. https://blog.csdn.net/rainbow702/article/details/54138155
GNG
发布了118 篇原创文章 · 获赞 389 · 访问量 68万+

猜你喜欢

转载自blog.csdn.net/so_geili/article/details/89169845