使用 report-aggregate 报告和合并多模块 jacoco 报告

kin*_*415 10 java qa code-coverage jacoco jacoco-maven-plugin

试图获得一份 jacoco 报告,该报告将显示来自多个模块的所有结果。

在构建项目后,我能够看到每个子模块都有一个 jacoco.exec 但不确定如何让它输出一份报告,该报告将组合每个模块的所有结果。

这是我在 Root pom.xml 中包含的内容:

                <plugin>
                    <groupId>@project.groupId@</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>@project.version@</version>
                </plugin>



             <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
Run Code Online (Sandbox Code Playgroud)

我为报告目的明确创建了一个新模块。(例如报告聚合模块)

在此示例中删除了组 ID 并使用了通用工件 ID:

这是我在这个报告聚合子模块的 pom.xml 中放入的内容:

<?xml version="1.0" encoding="UTF-8"?>
<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>report-aggregate</artifactId>
        <groupId>com.name.group</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>


    <artifactId>report-aggregate</artifactId>
    <name>Report Aggregate</name>

    <properties>
        <wildfly.version>10.0.0.Final</wildfly.version>
        <wildfly.artifactId>wildfly-dist</wildfly.artifactId>
    </properties>

    <dependencies>
        <dependency>
            <groupId></groupId>
            <artifactId>sub-module1</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>sub-module2</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>sub-module3</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>sub-module4</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>report-aggregate</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>report-aggregate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
Run Code Online (Sandbox Code Playgroud)

一切似乎都编译正常,似乎没有为报告聚合模块创建 jacoco exec。任何人都知道解决方案,或者我是否做错了?

Iva*_*sco 20

对不起,如果这是一个迟到的答案。

我假设你的根pom.xml的是一个aggregater,与<modules>包括module1module2,和report-aggregate。这就是你遇到麻烦的原因:因为你的根 pom.xml 是一个聚合器,它在你的子模块之前运行 JaCoCo,所以你的最终报告是空的。你应该:

  • 移动的目标配置report-aggregatejacoco-maven-plugin从根POM的report-aggregatePOM。这应该可以解决问题,因为您的report-aggregatePOM 使用<dependencies>,而不是<modules>.
  • 保留的目标配置prepare-agentjacoco-maven-plugin根POM。

我建议您查看 JaCoCo 论坛https://groups.google.com/forum/#!topic/jacoco/FpdLbxsXSTY。它指的是一个完整的演示集成测试https://github.com/jacoco/jacoco/tree/master/jacoco-maven-plugin.test/it/it-report-aggregate

  • 抱歉,但这似乎正是问题中的配置方式。主 pom 有“prepare-agent”,报告模块有“report-aggregate”目标。应该如何帮助呢。顺便说一句,有同样的问题,由于某种原因聚合不起作用 (6认同)