FindBugs Maven插件教程
FindBugs是一種靜態(tài)代碼分析工具,可識別從Java代碼中發(fā)現(xiàn)的問題。
我們可以使用FindBugs Maven插件將FindBugs集成到我們的構(gòu)建過程中。 這篇博客文章確定了四個典型的用例,并描述了我們?nèi)绾闻渲肍indBugs Maven插件以支持每個用例。
描述的用例是:
讓我們開始吧。
用例1:創(chuàng)建Findbugs報告作為項目報告的一部分
有時,我們不想在每次編譯項目時都運行靜態(tài)代碼分析。 相反,我們希望在需要時手動運行它。 在這種情況下,最好的選擇是在創(chuàng)建項目站點時創(chuàng)建FindBugs報表。
我們可以按照以下步驟進行操作:
pom.xml文件的相關(guān)部分如下所示:
<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)問題,如何使構(gòu)建失敗。
用例2:如果發(fā)現(xiàn)問題,則構(gòu)建失敗
如果我們希望確保我們的代碼甚至沒有一個小問題,那么在每次編譯項目時都運行靜態(tài)代碼分析可能是個好主意。 當然,僅當發(fā)現(xiàn)問題時構(gòu)建失敗時,這才有意義。
換句話說,如果發(fā)現(xiàn)問題,我們必須配置FindBugs Maven插件使構(gòu)建失敗。 我們可以按照以下步驟進行操作:
pom.xml文件的相關(guān)部分如下所示:
<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)問題,則構(gòu)建失敗。
讓我們繼續(xù)前進,了解如何創(chuàng)建XML報告而不創(chuàng)建站點或構(gòu)建失敗。
用例3:在不失敗的情況下創(chuàng)建XML報告
如果我們想將Jenkins與FindBugs集成在一起 ,我們需要找到一種在不使構(gòu)建失敗的情況下創(chuàng)建XML報告的方法。
我們可以按照以下步驟配置FindBugs Maven插件來做到這一點:
pom.xml文件的相關(guān)部分如下所示:
<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)建站點或構(gòu)建失敗的情況下創(chuàng)建XML和HTML報告。 讓我們看看這是如何完成的。
用例4:創(chuàng)建XML和HTML報告而不創(chuàng)建站點
如果我們要創(chuàng)建XML和HTML報表而不創(chuàng)建項目站點或構(gòu)建失敗,則必須執(zhí)行以下步驟:
pom.xml文件的相關(guān)部分如下所示:
<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獲得此博客文章的示例應(yīng)用程序。
翻譯自: 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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么吹萨克斯 吹萨克斯的方法
- 下一篇: 杭州市的市花有哪些 有关杭州市的市花介绍