JaCoCo多模塊單元測試配置
在maven的多模塊項目中配置Jacoco插件,顯示多個模塊的單元測試覆蓋率報告
1.首先創建一個多模塊項目
目錄結構如下

JacocoMoudlesTest是root模塊,test-a,test-b,test-c,三個模塊中包含單元測試的代碼,test-common是個空模塊,主要存放全部進行單元測試模塊的總覆蓋率報告。
以上圖為例,common模塊主要存放a,b,c模塊的單元測試覆蓋率報告
2.父模塊的pom配置
導入junit和jacoco的依賴
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
</dependency>
配置jacoco的plugin插件
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
<configuration>
<!--<includes>-->
<!--<include>com/test/demo/*</include>-->
<!--</includes>-->
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>target/jacoco.exec</dataFile>
<outputDirectory>target/jacoco-site</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
3.在父模塊下運行mvn package
在父模塊中配置了goal為report,運行父模塊下的mvn package之后會在所有子模塊中生成單測覆蓋率報告
4.在common模塊中顯示覆蓋率報告總合
在common模塊的依賴中添加需要進行單元測試的模塊
<dependency>
<groupId>com.example</groupId>
<artifactId>test-a</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>test-b</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>test-c</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
配置common模塊下的jacoco插件,
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
<executions>
<execution>
<id>report-aggregate</id>
<phase>test</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
goal為 report-aggregate ,這個goal是jacoco 0.7.7版本以后,專門為多模塊覆蓋率顯示所設置,可以統計該模塊所依賴的所有其他模塊的覆蓋率

common下的單測覆蓋率報告如圖

