ant+findbugs 掃描代碼生成報告


1. 下載安裝ant、findbugs

    下載ant、findbugs最新壓縮包解壓到本地磁盤合適位置,比如:

    D:\Program Files\apache-ant-1.9.7

    D:\Program Files\findbugs-3.0.1

2.  配置環境變量

    

    

    

3.  編寫build.xml

    

<?xml version="1.0" encoding="UTF-8"?>
<project name="hsacct">
    <path id="findbugs.path">
        <fileset dir="D:/Program Files/findbugs-3.0.1">
            <include name="**/*.jar" />
        </fileset>
    </path>
    <taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask"
        classpathref="findbugs.path" />
    <property name="findbugs.home" value="D:/Program Files/findbugs-3.0.1" />
    <target name="findbugs">
        <findbugs home="D:/Program Files/findbugs-3.0.1" jvmargs="-Xmx1024m" output="html"
            outputFile="D:/Program Files/findbugs-3.0.1/mytest.html">
            <class location="D:/Offsite/Sources/mytest_main/mytest-account" />
            <class location="D:/Offsite/Sources/mytest_main/mytest-acpt" />
            <class location="D:/Offsite/Sources/mytest_main/mytest-act-account" />
            <class location="D:/Offsite/Sources/mytest_main/mytest-act-common" />
            <class location="D:/Offsite/Sources/mytest_main/mytest-arch" />
            <class location="D:/Offsite/Sources/mytest_main/mytest-autotask" />
            <class location="D:/Offsite/Sources/mytest_main/mytest-bankacct" />
            <class location="D:/Offsite/Sources/mytest_main/mytest-cert" />
            <class location="D:/Offsite/Sources/mytest_main/mytest-csdc" />
            <class location="D:/Offsite/Sources/mytest_main/mytest-elig" />
            <class location="D:/Offsite/Sources/mytest_main/mytest-ofsecu" />
            <class location="D:/Offsite/Sources/mytest_main/mytest-preengage" />
            <class location="D:/Offsite/Sources/mytest_main/mytest-prod" />
            <class location="D:/Offsite/Sources/mytest_main/mytest-sdk" />
            <class location="D:/Offsite/Sources/mytest_main/mytest-system" />
            <auxClasspath path="D:/Program Files/findbugs-3.0.1/lib/findbugs-ant.jar" />
            <auxClasspath>
                <fileset dir="D:/Offsite/Sources/mytest_main/mytest/target/mytest-1.1.0/WEB-INF/lib" includes="**/*.jar" />
            </auxClasspath>
            <sourcePath path="D:/Offsite/Sources/mytest" />
        </findbugs>
    </target>
</project>

 

4.  生成報告

     打開命令行,進入到build.xml文件所在文件夾,鍵入ant findbugs命令回車即可。

5.  部分屬性參數說明

    關於findbugs任務的詳細說明,如下: 

  class 嵌套元素指定要分析的類。這個元素必須指定一個location屬性,location屬性的名字為archive文件(jar,zip等)、目錄或者class文件。
      可以為一個findbugs元素指定多個class元素。
  auxClasspath 可選的嵌套元素,用於指定要分析的類所引用的類,但是並不對引用的類進行分析。
  sourcePath 可選的嵌套元素,指定Java源代碼的目錄。
  home 必須的屬性,findbugs的安裝目錄。
  quietErrors 可選的布爾型屬性。如果是true的話,報告嚴重的分析錯誤和丟失的類。默認情況下為false。
  reportLevel 可選的屬性。指定優先級別。如果是low的話,那么報告所有的bug,如果是medium(缺省值),報告medium和high優先級的bug。
  output 可選屬性,設置輸出格式
  stylesheet 可選屬性,指定生成html時使用的樣式表。
  sort 可選屬性,如果輸出屬性設置為text,該屬性指定是否對輸出結果根據class進行排序,默認為true。
  outputFile 可選屬性,指定輸出文件。
  debug 可選的布爾型屬性,是否打印分析過程中的日志。默認值為false。
  effort 設置分析工作的等級,可以為min、default和max。
  conserveSpace 和min effort一樣的功能。
  workHard 和max effort一樣的功能。
  visitors 可選屬性,指定逗號分隔的列表,指定要運行的detectors。
  omitVisitors 可選屬性,忽略detectors。折合visitors屬性類似,只是不指定不運行哪些detectors。
  excludeFilter 可選屬性,指定排除的Filter。
  includeFilter 可選屬性,指定包含的Filter。
  projectFile 可選屬性,指定項目的名稱。
  jvmargs 可選屬性,指定JVM變量。
  systemProperty 系統屬性。
  timeout 可選屬性,指定超市的時間,默認為600,000毫秒,即10分鍾。
  failOnError 可選屬性,指定是否在運行FindBugs出現異常時停止構建過程,默認為false。
  errorProperty 可選屬性,如果在運行FindBugs時發生錯誤,指定屬性的值為true。
  warningsProperty 可選屬性,如果在運行FindBugs時發生警告,指定屬性的值為true。

6.  可能遇到的問題

     由於工程比較大,所以在導出html過程中可能會生成一個空白的html文件。此時將jvmargs參數值調大,比如jvmargs="-Xmx1024m"。


免責聲明!

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



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