maven - 在 Maven 3.0.5 中汇总 findbugs 报告

标签 maven maven-3 findbugs

我正在使用多模块 Maven 项目(超过 10 个模块)。我正在尝试在单个 html 页面中创建所有模块的 findbugs 报告。有什么办法吗?

为了为每个模块创建单独的报告,我使用以下

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>findbugs-maven-plugin</artifactId>
    <version>3.0.0</version>
    <configuration>
        <!--
            Enables analysis which takes more memory but finds more bugs.
            If you run out of memory, changes the value of the effort element
            to '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>false</xmlOutput>
        <skip>${skipFindbugs}</skip>
        <!-- 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>

            <phase>compile</phase>
            <goals>
                <goal>findbugs</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>${project.parent.basedir}/default.xsl</stylesheet>
                <!--<stylesheet>plain.xsl</stylesheet>-->
                <!--<stylesheet>fancy.xsl</stylesheet>-->
                <!--<stylesheet>summary.xsl</stylesheet>-->
                <fileMappers>
                    <!-- Configures the file extension of the output files. -->
                    <fileMapper implementation="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>3.0.0</version>
        </dependency>
    </dependencies>
</plugin>

最佳答案

根据插件的official documentation(问题n.1),这是不可能的。

但是,这是我用来实现它的方法:

  • 向现有的多模块项目添加一个附加模块。此附加模块将仅用于报告
  • 配置Buildhelper Maven Plugin,将其他模块的源代码动态添加到上报模块中。注意:如果需要,您可以对资源执行相同的操作。
  • 仅在报告模块上配置 Findbugs 插件
  • 添加其他模块作为报告模块的依赖项,以便让Maven react 器构建仅在最后构建它。
  • 如果需要:您不希望报告模块成为默认构建的一部分,请在聚合器/父模块中创建一个配置文件,重新定义 modules 元素并将报告模块添加到其中。因此,只有当配置文件将被激活(即通过命令行,按需)时,才会添加报告模块并创建汇总报告。

  • 例如,在聚合器/父模块中,您可以定义如下:
    <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.sample</groupId>
        <artifactId>findbugs-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>pom</packaging>
    
        <modules>
            <module>findbugs-module1</module>
            <module>findbugs-module2</module>
        </modules>
    
        <profiles>
            <profile>
                <id>findbugs-reporting</id>
                <modules>
                    <module>findbugs-module1</module>
                    <module>findbugs-module2</module>
                    <module>findbugs-reporting</module>
                </modules>
            </profile>
        </profiles>
    </project>
    

    注意:findbugs-reporting 模块仅添加在 findbugs-reporting 配置文件中。默认情况下,构建将忽略它。

    在 findbugs-reporting 模块中,使用您发布的配置(findbugs 和 XML maven 插件)配置 POM,并添加以下内容:
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.9.1</version>
        <executions>
            <execution>
                <id>add-source</id>
                <phase>generate-sources</phase>
                <goals>
                    <goal>add-source</goal>
                </goals>
                <configuration>
                    <sources>
                        <source>..\findbugs-module1\src\main\java</source>
                        <source>..\findbugs-module2\src\main\java</source>
                    </sources>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    注意来自其他模块的添加源(根据您的项目进行更改)。

    此外,我们还需要向报告模块添加依赖项。它必须依赖于其他模块才能最终构建(因此请确保从其他模块获取最新的更改/来源)。举个例子:
    <dependencies>
        <dependency>
            <groupId>com.sample</groupId>
            <artifactId>findbugs-module1</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.sample</groupId>
            <artifactId>findbugs-module2</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
    

    最后,您可以从聚合器/父目录调用报告构建,如下所示:
    mvn clean install -Pfindbugs-reporting
    

    因此,您将构建整个项目并另外激活报告模块,该模块将动态包含来自其他模块(如配置)的源并生成汇总报告。

    根据您的需要,您可以避免配置文件步骤(如果您希望将其作为默认构建的一部分)或默认激活配置文件(以便您可以跳过报告构建通过 -P!findbugs-reporting 停用它)或使用 skipFindbugs 属性您已经已配置(在这种情况下,没有配置文件)。

    关于maven - 在 Maven 3.0.5 中汇总 findbugs 报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34829200/

    相关文章:

    java - findbugs-maven-plugin 什么都不做

    java - 在 Eclipse 中右键单击项目或文件时,查找错误不显示

    java - 参数化 JUnit 测试是否正确使用 `mvn test` ?

    maven - 如何配置多模块 Maven + Sonar + JaCoCo 来提供合并的覆盖率报告?

    Maven:为什么我得到 servlet-api-2.5

    java - Maven依赖版本范围

    java - 逐步抑制 findbugs 警告

    java - 无法执行 maven-surefire-plugin 但仅使用 circleci

    java - log4j - 指向多个 log4j.properties 文件之一

    java - 当代码具有注释和泛型时,maven 3 + pmd 失败