Maven 2 Cobertura Plugin

The Maven 2 Cobertura Plugin web site lacks information to successfully generate Cobertura reports. Worse, some of the usage examples are incorrect and don’t work.

The most common problem when generating Cobertura reports is when the generated report shows 100% test coverage while in reality many of the classes don’t even have tests.

The following example shows how to configure the reports so that it would reflect real test coverage and then check if the specified packages achieved the wanted test coverage:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <check>
                    <haltOnFailure>false</haltOnFailure>
                    <regexes>
                        <regex>
                            <pattern>com.samaxes.business.*</pattern>
                            <branchRate>90</branchRate>
                            <lineRate>90</lineRate>
                        </regex>
                        <regex>
                            <pattern>com.samaxes.persistence.*</pattern>
                            <branchRate>90</branchRate>
                            <lineRate>90</lineRate>
                        </regex>
                    </regexes>
                </check>
                <instrumentation>
                    <includes>
                        <include>com/samaxes/**/*.class</include>
                    </includes>
                </instrumentation>
            </configuration>
            <executions>
                <execution>
                    <id>clean</id>
                    <phase>pre-site</phase>
                    <goals>
                        <goal>clean</goal>
                    </goals>
                </execution>
                <execution>
                    <id>instrument</id>
                    <phase>site</phase>
                    <goals>
                        <goal>instrument</goal>
                        <goal>cobertura</goal>
                        <goal>check</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<reporting>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
        </plugin>
    </plugins>
</reporting>

Published by

Samuel Santos

Java developer, Open Source hacker, Web technologist, JUG Leader.

7 thoughts on “Maven 2 Cobertura Plugin”

  1. I am confused about the snippet above. If you are running the instrumentation goal in Cobertura during the site phase, I am assuming that the test cases have already run by then. So, how would your Cobertura reports show coverage then? Don’t you need to do the instrumentation somewhere between compile and test phases. Please clarify for me.

    1. During the site phase Maven runs the compile and test goals so the code is correctly instrumentated.
      But the code snippet above is already deprecated since the Maven Cobertura plugin has this bug fixed in current versions.

  2. In which version of the plugin is this bug fixed?

    I am experiencing some funny behaviour (0% coverage despite presence of passing tests), I’d like to exclude the possibility that this is it. Am using v. 2.2 of the plugin.

    Cheers,
    Morten

  3. Well i am using Maven 3 .
    Cobertura plugin is not working in my case .

    always shows “No files to instrument.”.
    Doing same like above.

    running command “mvn clean cobertura:instrument” .

    I am suspecting that classes location which i m giving is different than looking for coverage.

    can u pl. expalin from where it picks up classes??

    Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>