Jacoco Maven多模块项目覆盖范围

Red*_*ddy 12 html java report jacoco jacoco-maven-plugin

似乎有几个问题,这些问题已经过时了,并且从Jacoco的Java 8支持改变了一些问题.

我的项目包含以下结构

pom.xml
|
|
-----sub module A pom.xml
|
|
-----sub module B pom.xml
|
|
-----sub module C pom.xml
Run Code Online (Sandbox Code Playgroud)

我已经配置了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>
Run Code Online (Sandbox Code Playgroud)

一个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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

请帮我解决一下这个.另一点是,我的配置对于Multi-Module项目是否正确?

更新

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

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

Rog*_*rio 13

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

  • 从 0.7.7 版本开始添加了对多模块项目的支持。对于任何想要了解有关 Tycho 构建的使用细节的人,我发现 Lorenzo Bettini 的这篇博客文章非常有帮助:[JaCoCo 代码覆盖率和多个 Eclipse 插件项目的报告](http://www.lorenzobettini .it/2017/02/jacoco-code-coverage-and-report-of-multiple-eclipse-plug-in-projects/#comment-13287) (2认同)

Sar*_*ang 8

请遵循以下说明

  1. 创建一个新的子项目(通常称为maven模块)。这将用作报告聚合器。
    父 pom 将会是这样的:
   <modules>
        <module>A</module>
        <module>B</module>
        <module>C</module>
        <module>ReportAggregator</module>
    </modules>
Run Code Online (Sandbox Code Playgroud)
  1. 在聚合器模块 pom- 添加其他子项目依赖项。
<dependency>
    <groupId>xyz</groupId>
    <artifactId>A</artifactId>
    <version>${project.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
  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>
Run Code Online (Sandbox Code Playgroud)
  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>
Run Code Online (Sandbox Code Playgroud)
  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>
Run Code Online (Sandbox Code Playgroud)
  1. 跑步mvn clean install

  • 不要运行安装,仅运行验证。不要用快照版本污染您的存储库。 (2认同)

tm1*_*701 5

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

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

享受简单的演示项目