JaCoCo不忽略课程

时间:2018-08-23 12:37:37

标签: maven unit-testing jacoco cobertura jacoco-maven-plugin

我已经成功将JaCoCo添加到我的项目中,它可以正常工作,但是由于某种原因,它不允许我排除类: src / main / java / com / example / Main.java < / p>

       <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.1</version>
            <executions>
                <execution>
                    <id>prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>check</id>
                    <goals>
                        <goal>check</goal>
                    </goals>
                    <configuration>
                        <skip>false</skip>
                        <haltOnFailure>true</haltOnFailure>
                        <rules>
                            <rule>
                                <element>BUNDLE</element>
                                <excludes>
                                    <exclude>com/example/Main.*</exclude>
                                </excludes>
                                <limits>
                                    <limit>
                                        <counter>LINE</counter>
                                        <value>COVEREDRATIO</value>
                                        <minimum>0.80</minimum>
                                    </limit>
                                    <limit>
                                        <counter>BRANCH</counter>
                                        <value>COVEREDRATIO</value>
                                        <minimum>0.60</minimum>
                                    </limit>
                                </limits>
                            </rule>
                        </rules>
                    </configuration>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

我已经在网站上尝试了其余问题,但是在排除单个课程时,没有一个问题对我有用。执行完上面的代码后,我得到以下输出,其中包含类 Main ,这是我要避免的类:

Output

预先感谢

1 个答案:

答案 0 :(得分:1)

在您的问题中,excludes块在configuration/rules中声明。应该在configuration内部声明。

例如:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.1</version>
    <configuration>
        <excludes>
            <exclude>**/Main.*</exclude>
        </excludes>
    </configuration>
    <executions>
        <execution>
            <id>default-prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
    </executions>
</plugin>

我建议从最简单的插件定义(即我上面发布的内容)开始,然后将其扩展为(1)通过包名称引用该类,并(2)添加您的规则。