SpringBoot+Maven+Jacoco完成本地项目测试覆盖率分析(解决Skipping JaCoCo execution due to missing execution data file)

SpringBoot+Maven+Jacoco完成本地项目测试覆盖率分析

Maven单模块

只需要在项目的pom.xml文件中添加如下的配置即可:

<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</id>
			<phase>prepare-package</phase>
			<goals>
				<goal>report</goal>
			</goals>
		</execution>
	</executions>
</plugin>

Maven多模块

Jacoco可以很好的支持对Maven多模块进行聚合分析测试覆盖率,可以从项目整体输出覆盖率报告非常方便。
假设现有的Project结构如下(大部分的多Maven模块是这种结构):

├── parent-project
├── pom.xml
├── business-module1
│   ├── pom.xml
│   └── src
│       ├── main
│       └── test
├── business-module2
    ├── pom.xml
    └── src
        ├── main
        └── test

需要添加一个test-module变成如下的结构

├── parent-project
├── pom.xml
├── business-module1
│   ├── pom.xml
│   └── src
│       ├── main
│       └── test
├── business-module2
│   ├── pom.xml
│   └── src
│       ├── main
│       └── test
└── test-module
    ├── pom.xml
    └── src
        ├── main
        └── test

接下来是配置的重点:
根目录的pom.xml配置:
1、 将test-module作为子模块
2、jacoco插件并且启动代理

// modules中增加test-module
  <modules>
    <module>business-module1</module>
    <module>business-module2</module>
    <module>test-module</module>
  </modules>
// plugin中增加
<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.8.6</version>
  <executions>
    <execution>
      <id>prepare-agent</id>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
    </execution>
    <execution>
      <id>report</id>
      <phase>verify</phase>
      <goals>
        <goal>report</goal>
      </goals>
    </execution>
  </executions>
</plugin>

test-module的pom.xml
1、添加现有Project的子module的依赖
2、引入jacoco插件,声明一个聚合分析任务

<dependency>
    <groupId>按照实际配置</groupId>
    <artifactId>business-module1</artifactId>
    <version>按照实际配置</version>
</dependency>
<dependency>
    <groupId>按照实际配置</groupId>
    <artifactId>business-module1</artifactId>
    <version>按照实际配置</version>
</dependency>


<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.6</version>
    <executions>
        <execution>
            <id>report-aggregate</id>
            <phase>verify</phase>
            <goals>
                <goal>report-aggregate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

business-module1的pom.xml
引入jacoco插件

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
</plugin>

business-module2的pom.xml
引入jacoco插件

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
</plugin>

运行

构建成功后可以在test-module模块下的target/site/jacoco-aggregate/index.html 查看覆盖率报告:

常见问题

1、business-module1模块或者business-module2模块下的target目录没有生成jacoco.exec文件
查看Maven的构建日志,看是否打印?

[INFO] --- jacoco-maven-plugin:0.8.6:prepare-agent (prepare-agent) @ business-module1 ---
[INFO] argLine set to -javaagent:C:\\Users\\bxu14\\.m2\\repository\\org\\jacoco\\org.jacoco.agent\\0.8.6\\org.jacoco.agent-0.8.6-runtime.jar=destfile=C:\\Users\\bxu14\\Desktop\\source\\parent-project\\business-module1\\target\\jacoco.exec

如果正常打印出来,则大概率就是对应的module下面并没有写Test案例导致。补充案例之后可以解决。
如果没有打印出来,那么问题就出在配置上面,按照上面的教程检查下每个module是否都改全了。
有如下几个思路:
a、每个业务module下面是否引入jacoco插件。
b、

2、Skipping JaCoCo execution due to missing execution data file.
基本上就是因为没有jacoco.exec文件导致的,按照方法1的解决方案即可。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值