maven's phase and goal
Before talking about Jacobo configuration, let's talk about phase and goal of maven plug-in configuration.
The phase consists of a goal. Phase is actually the container of goal, which is actually executed by goal. When a phase is executed, the actual execution is the goal bound to the phase. For example, executing mvn package (the package here is phase) includes four goals (goals) of validate, compile, test and package.
A goal represents a specific task. Goal can belong to a phase or exist independently. For example, the following command:
mvn clean dependency:copy-dependencies package
Here, clean and package are a phase, and dependency: copy dependencies is an independent existence target. Here, the clean phase will be executed first, then the dependency: copy dependencies target will be executed, and finally the package phase will be executed.
It can be concluded from the above that goal will be triggered in the following two cases:
- goal is bound to a phase, which will be executed automatically when the phase is executed;
- Execute MVN < plugin name >: < goal > explicitly through the command, such as mvn clean:clean;
Unit test coverage report generated by single module
The single module scenario is relatively simple. It is in POM. Com in the code root directory XML add the following configuration:
<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>
Jacobo's report target is in the verify phase by default. Here, the report target is re bound to the test phase. When mvn test is executed, the coverage report of unit test will be generated in the target / site / Jacobo directory of the code.
Multi module generation unit test coverage report
Multi module scenario configuration is complex. Stack overflow Examples of best practices are given.
Assuming that there is a multi module project, the code structure is as follows:
├── module1 │ └── pom.xml ├── module2 │ └── pom.xml ├── module3 │ └── pom.xml ├── pom.xml
Configuration steps
-
First, you need to add a module for aggregation under the project, for example, it can be called report. The code structure after adding the aggregation module is as follows:
├── module1 │ └── pom.xml ├── module2 │ └── pom.xml ├── module3 │ └── pom.xml ├── report │ └── pom.xml ├── pom.xml
-
POM of the project root directory XML needs to add a Jacobo plug-in:
<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 of aggregation module XML file needs:
a. Introduce all dependent modules in the form of dependency;
b. Define the aggregation action of Jacobo and use the report aggregate target.<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>
After the configuration is completed, execute mvn test in the project root directory. After successful execution, the coverage test report of each module will be generated in the report / target / site / Jacobo aggregate directory. If you do not want the default report path, you can also configure the report output path in the 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>
reference resources
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