Maven 2 Cobertura Plugin

June 24th, 2007 by Samuel Santos Leave a reply »

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>
Don't be shellfish...Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInPin on PinterestBuffer this pageEmail this to someone
Advertisement

7 comments

  1. Birger says:

    Thanks, this should definitely have been on the Maven 2 Cobertura plugin site.

  2. Birger says:

    I had to use version 2.0 to make this work (see http://jira.codehaus.org/browse/MCOBERTURA-57).

  3. Ajay Gupta says:

    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.

    • 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.

  4. Morten Lauritsen says:

    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

  5. anjali says:

    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