日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

FindBugs Maven插件教程

發(fā)布時間:2023/12/3 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 FindBugs Maven插件教程 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

FindBugs是一種靜態(tài)代碼分析工具,可識別從Java代碼中發(fā)現(xiàn)的問題。

我們可以使用FindBugs Maven插件將FindBugs集成到我們的構建過程中。 這篇博客文章確定了四個典型的用例,并描述了我們?nèi)绾闻渲肍indBugs Maven插件以支持每個用例。

描述的用例是:

  • 創(chuàng)建FindBugs報告作為項目報告的一部分。
  • 如果FindBugs從源代碼中發(fā)現(xiàn)問題,則構建失敗。
  • 創(chuàng)建XML報告而不會使構建失敗。
  • 在不創(chuàng)建站點的情況下創(chuàng)建XML和HTML報告。
  • 讓我們開始吧。

    用例1:創(chuàng)建Findbugs報告作為項目報告的一部分

    有時,我們不想在每次編譯項目時都運行靜態(tài)代碼分析。 相反,我們希望在需要時手動運行它。 在這種情況下,最好的選擇是在創(chuàng)建項目站點時創(chuàng)建FindBugs報表。

    我們可以按照以下步驟進行操作:

  • 將FindBugs Maven插件的聲明添加到pom.xml文件的報告部分。
  • 通過執(zhí)行以下步驟配置FindBugs Maven插件:
  • 確保執(zhí)行最準確的分析。
  • 確保報告了所有錯誤。
  • 確保已生成XML報告。
  • pom.xml文件的相關部分如下所示:

    <reporting><plugins><plugin><groupId>org.codehaus.mojo</groupId><artifactId>findbugs-maven-plugin</artifactId><version>2.5.2</version><configuration><!--Enables analysis which takes more memory but finds more bugs.If you run out of memory, changes the value of the effort elementto 'low'.--><effort>Max</effort><!-- Reports all bugs (other values are medium and max) --><threshold>Low</threshold><!-- Produces XML report --><xmlOutput>true</xmlOutput></configuration></plugin></plugins> </reporting>

    僅在編譯項目時才創(chuàng)建報告。

    換句話說,當我們要創(chuàng)建FindBugs報表時,我們必須在命令提示符處運行以下命令:

    mvn clean compile site

    讓我們繼續(xù)前進,看看如果FindBugs從源代碼中發(fā)現(xiàn)問題,如何使構建失敗。

    用例2:如果發(fā)現(xiàn)問題,則構建失敗

    如果我們希望確保我們的代碼甚至沒有一個小問題,那么在每次編譯項目時都運行靜態(tài)代碼分析可能是個好主意。 當然,僅當發(fā)現(xiàn)問題時構建失敗時,這才有意義。

    換句話說,如果發(fā)現(xiàn)問題,我們必須配置FindBugs Maven插件使構建失敗。 我們可以按照以下步驟進行操作:

  • 將插件聲明添加到pom.xml文件的plugins部分。
  • 通過執(zhí)行以下步驟配置FindBugs Maven插件:
  • 確保執(zhí)行最準確的分析。
  • 確保報告了所有錯誤。
  • 確保已生成XML報告。
  • 配置插件以創(chuàng)建到目錄$ {project.build.directory} / findbugs的XML報告。
  • 添加一個執(zhí)行,該執(zhí)行在編譯 Maven生命周期階段運行插件的檢查目標。
  • pom.xml文件的相關部分如下所示:

    <build><plugins><plugin><groupId>org.codehaus.mojo</groupId><artifactId>findbugs-maven-plugin</artifactId><version>2.5.2</version><configuration><!--Enables analysis which takes more memory but finds more bugs.If you run out of memory, changes the value of the effort elementto 'Low'.--><effort>Max</effort><!-- Reports all bugs (other values are medium and max) --><threshold>Low</threshold><!-- Produces XML report --><xmlOutput>true</xmlOutput><!-- Configures the directory in which the XML report is created --><findbugsXmlOutputDirectory>${project.build.directory}/findbugs</findbugsXmlOutputDirectory></configuration><executions><!--Ensures that FindBugs inspects source code when project is compiled.--><execution><id>analyze-compile</id><phase>compile</phase><goals><goal>check</goal></goals></execution></executions></plugin></plugins> </build>

    此配置可確保在編譯Maven生命周期階段調(diào)用Maven FindBugs插件的檢查目標。 如果FindBugs從源代碼中發(fā)現(xiàn)問題,則構建失敗。

    讓我們繼續(xù)前進,了解如何創(chuàng)建XML報告而不創(chuàng)建站點或構建失敗。

    用例3:在不失敗的情況下創(chuàng)建XML報告

    如果我們想將Jenkins與FindBugs集成在一起 ,我們需要找到一種在不使構建失敗的情況下創(chuàng)建XML報告的方法。

    我們可以按照以下步驟配置FindBugs Maven插件來做到這一點:

  • 如上一節(jié)(用例2)中所述配置FindBugs Maven插件。
  • 通過將failOnError配置屬性的值設置為false,確保在發(fā)現(xiàn)問題時構建不會失敗。
  • pom.xml文件的相關部分如下所示:

    <build><plugins><plugin><groupId>org.codehaus.mojo</groupId><artifactId>findbugs-maven-plugin</artifactId><version>2.5.2</version><configuration><!--Enables analysis which takes more memory but finds more bugs.If you run out of memory, changes the value of the effort elementto 'Low'.--><effort>Max</effort><!-- Build doesn't fail if problems are found --><failOnError>false</failOnError><!-- Reports all bugs (other values are medium and max) --><threshold>Low</threshold><!-- Produces XML report --><xmlOutput>true</xmlOutput><!-- Configures the directory in which the XML report is created --><findbugsXmlOutputDirectory>${project.build.directory}/findbugs</findbugsXmlOutputDirectory></configuration><executions><!--Ensures that FindBugs inspects source code when project is compiled.--><execution><id>analyze-compile</id><phase>compile</phase><goals><goal>check</goal></goals></execution></executions></plugin></plugins> </Build>

    現(xiàn)在,我們可以通過編譯項目來創(chuàng)建XML報告。

    最后一個用例描述了如何在不創(chuàng)建站點或構建失敗的情況下創(chuàng)建XML和HTML報告。 讓我們看看這是如何完成的。

    用例4:創(chuàng)建XML和HTML報告而不創(chuàng)建站點

    如果我們要創(chuàng)建XML和HTML報表而不創(chuàng)建項目站點或構建失敗,則必須執(zhí)行以下步驟:

  • 如上一節(jié)(用例3)中所述配置FindBugs Maven插件。
  • 將XML Maven插件的聲明添加到pom.xml文件的plugins部分。
  • 請按照以下步驟配置插件:
  • 創(chuàng)建一個轉(zhuǎn)換集,該轉(zhuǎn)換集將轉(zhuǎn)換從$ {project.build.directory} / findbugs目錄中找到的所有XML文件,并將XSLT轉(zhuǎn)換的結(jié)果寫入同一目錄。
  • 配置樣式表,該樣式表指定XSLT轉(zhuǎn)換的輸出。 FindBugs庫提供了五個樣式表可用于此目的。 可用的樣式表在示例配置中進行了描述。
  • 確保XSLT轉(zhuǎn)換的所有輸出文件都具有文件擴展名.html 。
  • 添加一個執(zhí)行,該執(zhí)行在編譯 Maven生命周期階段調(diào)用XML Maven插件的轉(zhuǎn)換目標。
  • 添加FindBugs(版本2.0.1)作為插件的依賴項。
  • pom.xml文件的相關部分如下所示:

    <build><plugins><plugin><groupId>org.codehaus.mojo</groupId><artifactId>findbugs-maven-plugin</artifactId><version>2.5.2</version><configuration><!--Enables analysis which takes more memory but finds more bugs.If you run out of memory, changes the value of the effort elementto 'Low'.--><effort>Max</effort><!-- Build doesn't fail if problems are found --><failOnError>false</failOnError><!-- Reports all bugs (other values are medium and max) --><threshold>Low</threshold><!-- Produces XML report --><xmlOutput>true</xmlOutput><!-- Configures the directory in which the XML report is created --><findbugsXmlOutputDirectory>${project.build.directory}/findbugs</findbugsXmlOutputDirectory></configuration><executions><!--Ensures that FindBugs inspects source code when project is compiled.--><execution><id>analyze-compile</id><phase>compile</phase><goals><goal>check</goal></goals></execution></executions></plugin><plugin><groupId>org.codehaus.mojo</groupId><artifactId>xml-maven-plugin</artifactId><version>1.0</version><configuration><transformationSets><transformationSet><!-- Configures the source directory of XML files. --><dir>${project.build.directory}/findbugs</dir><!-- Configures the directory in which the FindBugs report is written.--><outputDir>${project.build.directory}/findbugs</outputDir><!-- Selects the used stylesheet. --><!-- <stylesheet>fancy-hist.xsl</stylesheet> --><stylesheet>default.xsl</stylesheet><!--<stylesheet>plain.xsl</stylesheet>--><!--<stylesheet>fancy.xsl</stylesheet>--><!--<stylesheet>summary.xsl</stylesheet>--><fileMappers><!-- Configures the file extension of the output files. --><fileMapperimplementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper"><targetExtension>.html</targetExtension></fileMapper></fileMappers></transformationSet></transformationSets></configuration><executions><!-- Ensures that the XSLT transformation is run when the project is compiled. --><execution><phase>compile</phase><goals><goal>transform</goal></goals></execution></executions><dependencies><dependency><groupId>com.google.code.findbugs</groupId><artifactId>findbugs</artifactId><version>2.0.1</version></dependency></dependencies></plugin></plugins> </build>

    此解決方案最初是在此StackOverflow問題中描述的 。

    現(xiàn)在,我們可以通過編譯項目來創(chuàng)建HTML和XML報告。

    摘要

    現(xiàn)在,我們已經(jīng)確定了FindBugs Maven插件的四個典型用例,并了解了如何配置該插件以支持每個用例。

    如果您知道本教程未涵蓋的用例,請在此博客文章中發(fā)表評論以通知我。

    • 您可以從Github獲得此博客文章的示例應用程序。

    參考:來自Petri Kainulainen博客的JCG合作伙伴 Petri Kainulainen的FindBugs Maven插件教程 。

    翻譯自: https://www.javacodegeeks.com/2014/02/findbugs-maven-plugin-tutorial.html

    創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

    總結(jié)

    以上是生活随笔為你收集整理的FindBugs Maven插件教程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。