天天看點

maven 使用findbugs 插件

項目采用maven建構,想使用findbugs-maven-plugin 插件進行代碼靜态分析

官網:http://findbugs.sourceforge.net/

項目目錄結構如下:

maven 使用findbugs 插件

 最下面的檔案就是maven 的配置檔案pom.xml,類似于ant的build.xml檔案,pom.xml内容如下:

maven 使用findbugs 插件

<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"  

    xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  

    <modelversion>4.0.0</modelversion>  

    <groupid>com.kunlunsoft</groupid>  

    <artifactid>ischinese</artifactid>  

    <version>0.0.1-snapshot</version>  

    <properties>  

        <checkstyle.config.location>checkstyle.xml</checkstyle.config.location>  

        <project.build.sourceencoding>utf-8</project.build.sourceencoding>  

    </properties>  

    <reporting>  

        <plugins>  

            <plugin>  

                <groupid>org.codehaus.mojo</groupid>  

                <artifactid>findbugs-maven-plugin</artifactid>  

                <version>2.5.2</version>  

            </plugin>  

        </plugins>  

    </reporting>  

</project>  

此時target目錄是空的。打開cmd,進入項目所在目錄,運作mvn findbugs:findbugs

運作結果如下:

maven 使用findbugs 插件

從上圖(mvn findbugs:findbugs的運作結果) 來看,maven 并沒有運作findbugs,為什麼會這樣呢?

我們看看findbugs官網是如何說明的:

findbugs雖然是一個靜态分析工具,但是它分析的不是java源代碼(字尾名為.java),而是class檔案(編譯後的檔案)。在運作mvn findbugs:findbugs 時,不會自動編譯項目,即沒有class檔案,是以findbugs沒有運作。

有的技術部落格說:“clean findbugs:findbugs install ,這種寫法是錯的,可以運作的,但是并不産生findbugs報告”,說法是對的,但是并沒有說明原因。

原因:運作clean後,class檔案都被删除了,是以不會運作findbugs,或者說findbugs沒有可分析的class檔案,自然就沒有産生分析結果。

使用maven運作findbugs前一定先編譯,一定要有class檔案!

解決方法:

(1)mvn clean compile findbugs:findbugs

(2)mvn clean test findbugs:findbugs (test會調用compile生命周期)

如下圖:

maven 使用findbugs 插件

 運作完之後,target目錄會增加如下檔案:findbugsxml.xml

測試結果是xml格式的,不友善檢視、展示.

我們希望以html格式來展示findbugs的運作結果(報告),如下圖:

maven 使用findbugs 插件