尝试在子模块上设置 jacoco-check 代码覆盖率

Trying to set jacoco-check code coverage on submodule

我的应用程序中有一些子模块。有些我希望有代码覆盖规则,有些我希望完全豁免。

我的项目的根 POM 继承自父 POM,其 JaCoCo 配置如下:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>${jacoco-maven-plugin.version}</version>
    <configuration>
        <fileSets>
            <fileSet>
                <directory>domain/target</directory>
                <includes>
                    <include>*.exec</include>
                </includes>
            </fileSet>
            <fileSet>
                <directory>integration/target</directory>
                <includes>
                    <include>*.exec</include>
                </includes>
            </fileSet>
        </fileSets>
        <destFile>target/jacoco.exec</destFile>
    </configuration>
</plugin>

在我希望有 50% 代码覆盖率的子模块 test 中,我已将 JaCoCo 配置为:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>default-prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <excludes>
                    <exclude>**/*</exclude>
                </excludes>
            </configuration>
        </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>0.5</minimum>
                            </limit>
                        </limits>
                    </rule>
                </rules>
            </configuration>
        </execution>
    </executions>
</plugin>

此模块的代码覆盖率绝对为零,但 运行 mvn clean verify 不会产生任何错误。

我想也许我必须在根 POM 中定义它,所以我这样做了:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>default-prepare-agent</id>
            <goals>
                <goal>prepare-agent</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>0.75</minimum>
                            </limit>
                        </limits>
                    </rule>
                </rules>
            </configuration>
        </execution>
    </executions>
</plugin>

mvn clean verify 确实产生错误:

[WARNING] Rule violated for package com.project: lines covered ratio is 0.00, but expected minimum is 0.75

问题是,我希望我的其他子模块的总值为 0.75,但对于此模块,test我希望值为 0.5。我发现 this link 描述了继承和 combine.self="override"。所以我将其添加到 test:

的配置选项中
<execution>
    <id>jacoco-check</id>
    <goals>
        <goal>check</goal>
    </goals>
    <configuration combine.self="override">
        <rules>
            <rule>
                <element>PACKAGE</element>
                <limits>
                    <limit>
                        <counter>LINE</counter>
                        <value>COVEREDRATIO</value>
                        <minimum>0.5</minimum>
                    </limit>
                </limits>
            </rule>
        </rules>
    </configuration>
</execution>

尽管有这个覆盖,我仍然收到错误抱怨它期望 0.75 当我想要 0.5:

[WARNING] Rule violated for package com.project: lines covered ratio is 0.00, but expected minimum is 0.75

如何在子模块中覆盖它?

我的解决方案是在项目根目录的POM中使用<pluginManagement>,然后在每个子模块中手动声明代码覆盖率。

根 POM:

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

我想要 0 代码覆盖率的子模块 POM:

            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>test</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <rule>
                                    <element>BUNDLE</element>
                                    <limits>
                                        <limit>
                                            <counter>LINE</counter>
                                            <value>COVEREDRATIO</value>
                                            <minimum>0</minimum>
                                        </limit>
                                    </limits>
                                </rule>
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>