28

似乎有几个问题,这些问题已经很老了,并且从 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>

B 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>

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

4

5 回答 5

32

JaCoCo 0.7.7 版可以通过一个新目标从多个 Maven 模块生成聚合覆盖率报告jacoco:report-aggregate

于 2015-10-14T19:22:27.070 回答
5

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

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

享受简单的演示项目。在这个简单的项目中,README.md 文件包含您正在寻找的信息。一个例子是:

在此处输入图像描述

简单的演示项目包含 3 个分支:

  1. Master 分支 - 包含上述功能
  2. Multi-module-only-unit-tests - 包含只有单元测试的模块
  3. Multi-module-unit-tests-try2 - 不同地包含带有单元测试的模块。
于 2018-06-02T10:53:01.133 回答
4

请遵循以下说明

  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. (可选步骤)如果有人面临警告/错误,例如: 捆绑“ *”中的类与执行数据不匹配。对于报告生成,必须使用与运行时相同的类文件。**

然后在聚合器分支 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
于 2020-12-09T21:24:15.157 回答
2

如果将聚合器 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 中的结果与预期的一样。

于 2017-01-19T13:47:48.210 回答
1

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

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

于 2021-01-07T14:47:27.727 回答