0


jacoco插件配置生成单元测试覆盖率报告

maven的phase和goal

在讲jacoco配置之前,先讲一下maven插件配置的

phase

和

goal

。

phase

(插件阶段)由

goal

(插件目标)构成。

phase

其实就是

goal

的容器,实际被执行的都是

goal

。

phase

被执行时,实际执行的都是被绑定到该

phase

的

goal

。比如执行

mvn package

(这里的

package

为

phase

), 就包含了

validate、 compile、test、package

四个

goal

(目标)。

一个

goal

(目标)代表一个具体的

task

。

goal

可以属于某一个

phase

,也可以独立存在。例如,如下面的命令:

mvn clean dependency:copy-dependencies package

这里的

clean

和

package

是一个阶段,

dependency:copy-dependencies

是一个独立存在目标。这里

clean

阶段将会被首先执行,然后

dependency:copy-dependencies

目标会被执行,最终

package

阶段被执行。

由上可以总结出,goal在一下两种情况下会被触发:

  1. goal绑定到了某个phase,执行这个phase时会自动执行;
  2. 通过命令明确执行 mvn <plugin name>:<goal>,如 mvn clean:clean;

单模块生成单元测试覆盖率报告

单模块场景比较简单,在代码根目录的

pom.xml

添加以下配置即可:

<plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.8.5</version><executions><execution><goals><goal>prepare-agent</goal></goals></execution><execution><id>report</id><phase>test</phase><goals><goal>report</goal></goals></execution></executions></plugin>
jacoco

的

report

目标默认是在

verify

阶段,这里将

report

目标重新绑到了

test

阶段。当执行

mvn test

时,会在代码的

target/site/jacoco

目录下生成单元测试的覆盖率报告。

多模块生成单元测试覆盖率报告

多模块场景配置比较复杂。stackoverflow这篇回答给出了最佳实践样例。

假设当前有一个多模块项目,代码结构如下所示:

├── module1
│   └── pom.xml
├── module2
│   └── pom.xml
├── module3
│   └── pom.xml
├── pom.xml

配置步骤

  1. 首先需要在项目下添加一个用来聚合的模块,例如可以叫report。添加聚合模块后的代码结构如下:├── module1│ └── pom.xml├── module2│ └── pom.xml├── module3│ └── pom.xml├── report│ └── pom.xml├── pom.xml
  2. 项目根目录的pom.xml需要添加jacoco插件:<plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.8.5</version><executions><execution><goals><goal>prepare-agent</goal></goals></execution></executions></plugin>
  3. 聚合模块的pom.xml文件需要: a. 以dependency的形式引入所有的依赖模块; b. 定义jacoco的聚合目标,使用report-aggregate目标。<artifactId>report</artifactId><dependencies><dependency><groupId>@project.groupId@</groupId><artifactId>module1</artifactId><version>${project.version}</version></dependency><dependency><groupId>@project.groupId@</groupId><artifactId>module2</artifactId><version>${project.version}</version></dependency><dependency><groupId>@project.groupId@</groupId><artifactId>module3</artifactId><version>${project.version}</version></dependency></dependencies><build><plugins><plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.8.5</version><executions><execution><id>report-aggregate</id><phase>test</phase><goals><goal>report-aggregate</goal></goals></execution></executions></plugin></plugins></build>

配置完成后,在项目根目录下执行

mvn test

,执行成功后会在

report/target/site/jacoco-aggregate

目录下生成各个模块的覆盖测试报告。如果不想要默认的报告路径,也可以在

outputDirectory

中配置报告的输出路径:

<build><plugins><plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.8.5</version><executions><execution><id>report-aggregate</id><phase>test</phase><goals><goal>report-aggregate</goal></goals><configuration><outputDirectory>target/site/jacoco</outputDirectory></configuration></execution></executions></plugin></plugins></build>

报告解读

关于jacoco覆盖率报告的解读,可以参考下面的两篇博客,讲的比较明白了:
https://www.jianshu.com/p/cc4a3b5ee0b2
https://www.jianshu.com/p/ef987f1b6f2f

参考

https://stackoverflow.com/questions/26607834/maven-lifecycle-vs-phase-vs-plugin-vs-goal
https://www.runoob.com/maven/maven-build-life-cycle.html
https://www.jianshu.com/p/ccf358158dbc
https://juejin.cn/post/6863658483158532110


本文转载自: https://blog.csdn.net/weixin_38384296/article/details/122655287
版权归原作者 七个披萨 所有, 如有侵权,请联系我们删除。

“jacoco插件配置生成单元测试覆盖率报告”的评论:

还没有评论