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在一下两种情况下会被触发:
- goal绑定到了某个phase,执行这个phase时会自动执行;
- 通过命令明确执行
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
配置步骤
-
首先需要在项目下添加一个用来聚合的模块,例如可以叫report。添加聚合模块后的代码结构如下:
├── module1 │ └── pom.xml ├── module2 │ └── pom.xml ├── module3 │ └── pom.xml ├── report │ └── pom.xml ├── pom.xml
-
项目根目录的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>
-
聚合模块的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>
参考
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