FindBugs是一种静态代码分析工具,可识别从Java代码中发现的问题。
我们可以使用FindBugs Maven插件将FindBugs集成到我们的构建过程中。 这篇博客文章确定了四个典型的用例,并描述了我们如何配置FindBugs Maven插件以支持每个用例。
描述的用例是:
- 创建FindBugs报告作为项目报告的一部分。
- 如果FindBugs从源代码中发现问题,则构建失败。
- 创建XML报告而不会使构建失败。
- 在不创建站点的情况下创建XML和HTML报告。
让我们开始吧。
用例1:创建Findbugs报告作为项目报告的一部分
有时,我们不想在每次编译项目时都运行静态代码分析。 相反,我们希望在需要时手动运行它。 在这种情况下,最好的选择是在创建项目站点时创建FindBugs报表。
我们可以按照以下步骤进行操作:
- 将FindBugs Maven插件的声明添加到pom.xml文件的报告部分。
- 通过执行以下步骤配置FindBugs Maven插件:
- 确保执行最准确的分析。
- 确保报告了所有错误。
- 确保已生成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>
仅在编译项目时才创建报告。
换句话说,当我们要创建FindBugs报表时,我们必须在命令提示符处运行以下命令:
mvn clean compile site
让我们继续前进,看看如果FindBugs从源代码中发现问题,如何使构建失败。
用例2:如果发现问题,则构建失败
如果我们希望确保我们的代码甚至没有一个小问题,那么在每次编译项目时都运行静态代码分析可能是个好主意。 当然,仅当发现问题时构建失败时,这才有意义。
换句话说,如果发现问题,我们必须配置FindBugs Maven插件使构建失败。 我们可以按照以下步骤进行操作:
- 将插件声明添加到pom.xml文件的plugins部分。
- 通过执行以下步骤配置FindBugs Maven插件:
- 确保执行最准确的分析。
- 确保报告了所有错误。
- 确保已生成XML报告。
- 配置插件以创建到目录$ {project.build.directory} / findbugs的XML报告。
- 添加一个执行,该执行在编译 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生命周期阶段调用Maven FindBugs插件的检查目标。 如果FindBugs从源代码中发现问题,则构建失败。
让我们继续前进,了解如何创建XML报告而不创建站点或构建失败。
用例3:在不失败的情况下创建XML报告
如果我们想将Jenkins与FindBugs集成在一起 ,我们需要找到一种在不使构建失败的情况下创建XML报告的方法。
我们可以按照以下步骤配置FindBugs Maven插件来做到这一点:
- 如上一节(用例2)中所述配置FindBugs Maven插件。
- 通过将failOnError配置属性的值设置为false,确保在发现问题时构建不会失败。
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>
现在,我们可以通过编译项目来创建XML报告。
最后一个用例描述了如何在不创建站点或构建失败的情况下创建XML和HTML报告。 让我们看看这是如何完成的。
用例4:创建XML和HTML报告而不创建站点
如果我们要创建XML和HTML报表而不创建项目站点或构建失败,则必须执行以下步骤:
- 如上一节(用例3)中所述配置FindBugs Maven插件。
- 将XML Maven插件的声明添加到pom.xml文件的plugins部分。
- 请按照以下步骤配置插件:
- 创建一个转换集,该转换集将转换从$ {project.build.directory} / findbugs目录中找到的所有XML文件,并将XSLT转换的结果写入同一目录。
- 配置样式表,该样式表指定XSLT转换的输出。 FindBugs库提供了五个样式表可用于此目的。 可用的样式表在示例配置中进行了描述。
- 确保XSLT转换的所有输出文件都具有文件扩展名.html 。
- 添加一个执行,该执行在编译 Maven生命周期阶段调用XML Maven插件的转换目标。
- 添加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问题中描述的 。
现在,我们可以通过编译项目来创建HTML和XML报告。
摘要
现在,我们已经确定了FindBugs Maven插件的四个典型用例,并了解了如何配置该插件以支持每个用例。
如果您知道本教程未涵盖的用例,请在此博客文章中发表评论以通知我。
- 您可以从Github获得此博客文章的示例应用程序。
翻译自: https://www.javacodegeeks.com/2014/02/findbugs-maven-plugin-tutorial.html