cobertura-maven-plugin excludes configuration

蹲街弑〆低调 提交于 2019-12-04 02:26:04

After a lot try and fail I got it working.

  1. Edit the pom.
  2. mvn clean test-compile
  3. mvn cobertura:cobertura
  4. Reopen the page from Firefox. (make sure the page is not cached)

I got it working with:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>cobertura-maven-plugin</artifactId>
    <version>2.6</version>
        <configuration>
            <instrumentation>
                <excludes>
                    <exclude>aaa/**/*.class</exclude>
                    <exclude>com/test/bbb/**/*.class</exclude>
                </excludes>
            </instrumentation>
        </configuration>
 </plugin>

Change 'aaa' with the beginning of the package name to be excluded. Change 'bbb' with your package name that you want to exclude from the report.

I hope it helps, Marc Andreu

You should use the <ignore> tag.

<configuration>
  <instrumentation>
    <ignores>
      <ignore>com.example.boringcode.*</ignore>
    </ignores>
  </instrumentation>
</configuration>

<exclude> used within <instrumentation> simply excludes the package from what your instrumenting. Which in this case, is nothing because you're not doing anything.

Please see the Mojo Maven Cobertura Plugin docs here.

Is it a typo?

<exclude>test/co/**/*.class</exclude>.

The co should be com.

BTW, <ignores> instructs Cobertura to ignore any calls to any method that matches the ignore regular expression. It will NOT skip over these classes during instrumention. To exclude classes from being instrumented, <excludes> should be used.

You should not append the .class as the following example

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>cobertura-maven-plugin</artifactId>
    <version>2.5.2</version>
    <configuration>
        <formats>
            <format>html</format>
            <format>xml</format>
        </formats>
        <instrumentation>
            <excludes>
                <exclude>test/co/**/*</exclude>
            </excludes>
        </instrumentation>
    </configuration>
</plugin>

I hope this may help.

I just lost two hours of my life getting an exclusion for Cobertura to be excluded, but finally succeeded.

The solution I found is that the plugin configuration with instrumentation & exclusion for the cobertura-maven-plugin MUST be in the build/plugins or build/pluginManagement chapter of the pom, while there also must be a reference to the cobertura-maven-plugin in the reporting chapter.

The pitfall here is that you initially start with defining the plugin at the reporting chapter, otherwise no report is generated. But the instrumentation itself doesn't pick up any configuration from that part of the pom. You need to define that within the build chapter.

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <instrumentation>
                        <excludes>
                            <exclude>my/exclusion/package/**</exclude>
                        </excludes>
                    </instrumentation>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

<reporting>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
        </plugin>
    </plugins>
</reporting>

In addition to BertKoor's answer, I'd like to point out that if you're executing mvn cobertura:cobertura or mvn cobertura:cobertura-integration-test directly, your report will still include coverage on all instrumented classes found in your target directory, as reported here!

If this is the case, make sure you do mvn **clean** cobertura:cobertura in order to clean up the target dir from a previous build first, and then instrument and run your tests.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!