Jacoco Maven 多模块项目覆盖

新手上路,请多包涵

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

我的项目包含以下结构

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>

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

原文由 RaceBase 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.8k
2 个回答

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

原文由 Rogério 发布,翻译遵循 CC BY-SA 3.0 许可协议

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

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

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

在此处输入图像描述

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

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

原文由 tm1701 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题