是否有可能因为不满足 JaCoCo 代码覆盖要求而失败 "mvn package"?

Is it possible to fail "mvn package" for not meeting JaCoCo code coverage requirements?

我在项目的 POM 中包含了 JaCoCo maven 插件

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.7</version>
    <executions>
        <execution>
            <id>default-prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>default-report</id>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
        <execution>
            <id>default-check</id>
            <goals>
                <goal>check</goal>
            </goals>
            <configuration>
                <rules>
                    <rule>
                        <element>BUNDLE</element>
                        <limits>
                            <limit>
                                <counter>LINE</counter>
                                <value>COVEREDRATIO</value>
                                <minimum>0.70</minimum>
                            </limit>
                        </limits>
                    </rule>
                </rules>
            </configuration>
        </execution>
    </executions>
</plugin>

我注意到当不满足代码覆盖率要求时,只有以下命令会失败:

是否可以配置 JaCoCo 插件使 mvn package 命令也失败?

您可以在 prepare-package 阶段将 check 目标配置为 运行。 prepare-package 阶段 运行 就在 package 目标之前。这样,如果不满足覆盖规则,您的构建将失败。

以下是您的 pom.xml 的变化方式。

<execution>
    <id>default-check</id>
    <goals>
        <goal>check</goal>
    </goals>
    <phase>prepare-package</phase> <!-- Fail just before package phase is executed -->
    <configuration>
        <rules>
            <!-- Rule configuration here -->
        </rules>
    </configuration>
</execution>

检查 documentation for Maven Lifecycle Goals for phases 及其执行顺序。