如何使用jacoco在多maven模块上实现单元测试覆盖

如何使用jacoco在多maven模块上实现单元测试覆盖,maven,spring-boot,jacoco-maven-plugin,Maven,Spring Boot,Jacoco Maven Plugin,我有下面的multi-maven模块应用程序,并按照下面的步骤在tests模块中生成一个整合的jacoco单元测试覆盖率报告。然而,我们有一个政策来强制执行80%的最低总覆盖率,否则构建失败。这在单模块应用中非常有效。但是,我无法在多模块应用程序的总体覆盖范围内强制执行此操作。如有任何建议,将不胜感激 Parent- -Module1 -Module2 -test 我试图在测试模块的pom文件中设置jacoco check的执行目标,但clean安装成功,没有任何覆盖错误 父pom.xml `

我有下面的multi-maven模块应用程序,并按照下面的步骤在tests模块中生成一个整合的jacoco单元测试覆盖率报告。然而,我们有一个政策来强制执行80%的最低总覆盖率,否则构建失败。这在单模块应用中非常有效。但是,我无法在多模块应用程序的总体覆盖范围内强制执行此操作。如有任何建议,将不胜感激

Parent-
-Module1
-Module2
-test
我试图在测试模块的pom文件中设置jacoco check的执行目标,但clean安装成功,没有任何覆盖错误

父pom.xml `

。。。。。。
模块1
模2
测验
org.springframework.boot
spring启动程序父级
2.0.1.1发布
UTF-8
UTF-8
1.8
芬奇利,释放
3.0.0-M1
0.8.3
org.springframework.boot
弹簧靴起动器
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧起动试验
测验
org.jacoco
jacocomaven插件
0.7.9
准备单元测试
配制剂
配制剂
配制剂
预集成测试
它是一种药剂
`
测试pom.xml

`

4.0.0
com.myapp.parent
我的服务
0.0.1-快照
myapp测试
${basedir}//
${basedir}/./target/aggregate.exec
0.84
模块1
myapp-module1
1
模块1
myapp-module2
1
org.apache.maven.plugins
maven surefire插件
${argLine}-Xms256m-Xmx2048m
1.
随机的
org.jacoco
jacocomaven插件
配制剂
汇报
准备包装
汇报
杰科科支票
检查
包裹
线
覆盖层
${代码覆盖率.行覆盖率.min}
报告汇总
验证
报告汇总
合并结果
验证
合并
${code.coverage.project.folder}
**/target/jacoco.exec
${code.coverage.total.data.folder}/aggregate.exec
`


如果聚合覆盖率不是80%,我预计mvn clean install会抛出错误。

您只是在测试模块上强制覆盖

您应该移动此配置:

  <execution>
    <id>jacoco-check</id>
    <goals>
        <goal>check</goal>
    </goals>
    <configuration>
        <rules>
            <rule>
                <element>PACKAGE</element>
                <limits>
                    <limit>
                        <counter>LINE</counter>
                        <value>COVEREDRATIO</value>
                        <minimum>${code-coverage.line-covered-ratio.min}</minimum>
                    </limit>
                </limits>
            </rule>
        </rules>
    </configuration>
  </execution>

杰科科支票
检查
包裹
线
覆盖层
${代码覆盖率.行覆盖率.min}
进入你父母的pom。 这样将检查所有模块

<modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.myapp.parent</groupId>
    <artifactId>myservice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>myapp-tests</artifactId>
  <properties>
        <code.coverage.project.folder>${basedir}/../</code.coverage.project.folder>
        <code.coverage.overall.data.folder>${basedir}/../target/aggregate.exec</code.coverage.overall.data.folder>
        <code-coverage.line-covered-ratio.min>0.84</code-coverage.line-covered-ratio.min>
  </properties>

    <dependencies>
        <dependency>
           <groupId>module1</groupId>
           <artifactId>myapp-module1</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
           <groupId>module1</groupId>
           <artifactId>myapp-module2</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <!-- Jacoco prepare-agent builds some command-line params without -->
                    <!-- which jacoco will not instrument. Hence it is important to add -->
                    <!-- those command-line params here (${argLine} holds those params) -->
                    <argLine>${argLine} -Xms256m -Xmx2048m</argLine>
                    <forkCount>1</forkCount>
                    <runOrder>random</runOrder>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <executions>
                <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>jacoco-check</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <rule>
                                    <element>PACKAGE</element>
                                    <limits>
                                        <limit>
                                            <counter>LINE</counter>
                                            <value>COVEREDRATIO</value>
                                            <minimum>${code-coverage.line-covered-ratio.min}</minimum>
                                        </limit>
                                    </limits>
                                </rule>
                            </rules>
                        </configuration>
                    </execution>
                    <execution>
                        <id>report-aggregate</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>report-aggregate</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>merge-results</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>merge</goal>
                        </goals>
                        <configuration>
                            <fileSets>
                                <fileSet>
                                    <directory>${code.coverage.project.folder}</directory>
                                    <includes>
                                        <include>**/target/jacoco.exec</include>
                                    </includes>
                                </fileSet>
                            </fileSets>
                            <destFile>${code.coverage.overall.data.folder}/aggregate.exec</destFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
  <execution>
    <id>jacoco-check</id>
    <goals>
        <goal>check</goal>
    </goals>
    <configuration>
        <rules>
            <rule>
                <element>PACKAGE</element>
                <limits>
                    <limit>
                        <counter>LINE</counter>
                        <value>COVEREDRATIO</value>
                        <minimum>${code-coverage.line-covered-ratio.min}</minimum>
                    </limit>
                </limits>
            </rule>
        </rules>
    </configuration>
  </execution>