cobertura-maven-plugin excludes configuration

21,592

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

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>
Share:
21,592
Glory to Russia
Author by

Glory to Russia

Updated on December 20, 2020

Comments

  • Glory to Russia
    Glory to Russia over 3 years

    I have a Maven project with a test case DefaultViewTypeToFragmentMapperTest.java in the directory /src/test/java/test/com/mycompany/myproduct/android/viewtype2fragmentmapper/.

    I want this test case to be excluded from unit test coverage calculation. In order to achieve this result, I configured the plugin like this:

    <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/**/*.class</exclude>
                </excludes>
            </instrumentation>
        </configuration>
    </plugin>
    

    But I still see the aforementioned class in the coverage report.

    How can I fix it such that the test case does not appear in the report and its coverage (0 % according to the report) is not taken into account?

  • ninnemannk
    ninnemannk over 11 years
    Okay, let's go back to your exclude statement. What is test/co/**/*.class to you? The test your attempting to exclude is not inside of this package structure.
  • Glory to Russia
    Glory to Russia over 11 years
    No, it's not. The test.co.mycompany.* package name is correct.
  • Matt Byrne
    Matt Byrne almost 11 years
    Well your folder name does not match that package name. You need to put "com" Your post says code is in "/src/test/java/test/com/mycompany/myproduct/android"
  • FrustratedWithFormsDesigner
    FrustratedWithFormsDesigner over 9 years
    The documentation doesn't really explain the difference between exclude or ignore.