Jacoco Maven 多模块项目覆盖

Jacoco Maven multi module project coverage

似乎有几个问题,这些问题已经很老了,并且从 Java 8 对 Jacoco 的支持发生了变化。

我的项目包含以下结构

pom.xml
|
|
-----sub module A pom.xml
|
|
-----sub module B pom.xml
|
|
-----sub module C pom.xml

我已经这样配置了main pom

主要POM.xml

<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.test</groupId>
    <artifactId>jacoco-multi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>projectA</module>
        <module>projectB</module>
    </modules>

    <properties>
        <jacoco.version>0.5.7.201204190339</jacoco.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit-dep</artifactId>
            <version>4.10</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.hamcrest</groupId>
                    <artifactId>hamcrest-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>1.3.RC2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
            <version>1.3.RC2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.5.201505241946</version>
                <configuration>
                    <destFile>${project.basedir}/../target/jacoco.exec</destFile>
                    <append>true</append>
                </configuration>
                <executions>
                    <execution>
                        <id>jacoco-initialize</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>jacoco-site</id>
                        <phase>package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.16</version>
                <executions>
                    <execution>
                        <id>default-integration-test</id>
                        <goals>
                            <goal>integration-test</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.16</version>
            </plugin>
        </plugins>
    </build>

</project>

一个Pom.xml

<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>
    <parent>
        <artifactId>jacoco-multi</artifactId>
        <groupId>com.test</groupId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>..</relativePath>
    </parent>
    <artifactId>projectA</artifactId>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.12</version>
                </plugin>

            </plugins>
        </pluginManagement>
    </build>

</project>

乙pom.xml

<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>
<parent>
    <artifactId>jacoco-multi</artifactId>
    <groupId>com.test</groupId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>..</relativePath>
</parent>
<artifactId>projectB</artifactId>

<dependencies>
    <dependency>
        <groupId>com.test</groupId>
        <artifactId>projectA</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

我正在执行这个命令mvn clean package。我可以看到 jacoco.exec 正在生成,但是我无法看到任何 HTML 报告正在验证数据。

请帮我解决这个问题。 另一点是,我的配置对 Multi-Module 个项目正确吗?

更新

确定的问题。 <destFile>${project.basedir}/../target/jacoco.exec</destFile> 变成 <destFile>${project.basedir}/target/jacoco.exec</destFile>

现在它正在为各个模块生成报告。 知道如何生成综合报告

JaCoCo 版本 0.7.7 可以 generate an aggregate coverage report 从多个 Maven 模块通过一个新目标 jacoco:report-aggregate

多模块项目中的一个问题是,如果聚合器 pom 被用作模块的父 pom,如上例所示:

- parentAggregator pom
---sub module A pom.xml -> parentAggregator pom
---sub module B pom.xml -> parentAggregator pom
---sub module C pom.xml -> parentAggregator pom

在这种情况下,构建顺序是:

- parentAggregator
- sub module A
- sub module B
- sub module C

这意味着,父聚合器无法收集完整的信息。在我的例子中,通过 mvn sonar:sonar 将数据传输到 sonarQube 导致了意外和不完整的结果。

将模块结构更改为:

- aggregator pom
-- parent pom
---sub module A pom.xml -> parent pom
---sub module B pom.xml -> parent pom
---sub module C pom.xml -> parent pom

将构建顺序更改为:

- parent
- sub module A
- sub module B
- sub module C
- aggregator

在这种情况下,聚合器将是最后一个并使用模块的结果。就我而言,SonarQube 中的结果符合预期。

扫描了许多解决方案后,我创建了一个简单但完整的 Jacoco 演示项目,显示:

  • 多模块项目
  • 单元测试(通过mvn clean install
  • 集成测试(通过mvn clean install -P integration-test
  • Jacoco - 测试覆盖率(聚合数据文件和聚合报告)
  • FindBugs - 代码质量

享受simple demo project。在这个简单的项目中,README.md 文件包含您要查找的信息。一个例子是:

简单的演示工程包含3个分支:

  1. 主分支 - 包含上述功能
  2. Multi-module-only-unit-tests - 包含只有单元测试的模块
  3. Multi-module-unit-tests-try2 - 包含带有不同单元测试的模块。

按照以下说明进行操作

  1. 创建一个新的子项目。这将用作报告聚合器。 父 pom 将像:

<modules>
        <module>A</module>
        <module>B</module>
        <module>C</module>
        <module>ReportAggregator</module>
    </modules>

  1. 在聚合器分支 pom- 添加其他子项目依赖项。

<dependency>
            <groupId>xyz</groupId>
            <artifactId>A</artifactId>
            <version>${project.version}</version>
        </dependency>

  1. 在聚合器分支 pom- 配置 jacoco 插件

<plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.6</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report-aggregate</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>report-aggregate</goal>
                        </goals>
                        <configuration>
                            <dataFileIncludes>
                                <dataFileInclude>**/jacoco.exec</dataFileInclude>
                            </dataFileIncludes>
                            <outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

  1. 在聚合器分支 pom- 配置 surefire 插件为

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <systemPropertyVariables>
                        <jacoco-agent.destfile>**/jacoco.exec</jacoco-agent.destfile>
                    </systemPropertyVariables>
                </configuration>
            </plugin>

  1. (可选步骤)如果有人面对 warning/error 这样的情况: 包“*”中的 类 与执行数据不匹配。对于报告生成,必须使用与 运行 时相同的 class 文件。**

然后在聚合器分支 pom 中添加下面提到的行

 <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.6</version>
                <executions>
                    <execution>
                        <id>instrument-ut</id>
                        <goals>
                            <goal>instrument</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>restore-ut</id>
                        <goals>
                            <goal>restore-instrumented-classes</goal>
                        </goals>
                    </execution>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report-aggregate</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>report-aggregate</goal>
                        </goals>
                        <configuration>
                            <dataFileIncludes>
                                <dataFileInclude>**/jacoco.exec</dataFileInclude>
                            </dataFileIncludes>
                            <outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

  1. 运行 mvn clean install

运行 Coverage as... Junit 分别针对每个模块。然后创建一个启动组并添加每个 运行 配置。有一个弹出窗口“启动模式”,其中包含继承、配置文件、覆盖范围、调试或 运行 选项。为所有这些选择 Coverage。您可能还想 select“等待直到终止”。

您现在可以 运行 一键完成所有覆盖率测试。在它们完成后,您需要进入 Coverage View 和 select 合并会话(双 red/green 栏)并将它们全部合并到一个报告中。