Question

We've got a multiproject we're trying to run Cobertura test coverage reports on as part of our mvn site build. I can get Cobertura to run on the child projects, but it erroneously reports 0% coverage, even though the reports still highlight the lines of code that were hit by the unit tests.

We are using mvn 2.0.8. I have tried running mvn clean site, mvn clean site:stage and mvn clean package site. I know the tests are running, they show up in the surefire reports (both the txt/xml and site reports). Am I missing something in the configuration? Does Cobertura not work right with multiprojects?

This is in the parent .pom:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <inherited>true</inherited>
                <executions>
                    <execution>
                        <id>clean</id>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
<reporting>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <inherited>true</inherited>
        </plugin>
    </plugins>
</reporting>

I've tried running it with and without the following in the child .poms:

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

I get this in the output of the build:

...
[INFO] [cobertura:instrument]
[INFO] Cobertura 1.9 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
Instrumenting 3 files to C:\workspaces\sandbox\CommonJsf\target\generated-classes\cobertura
Cobertura: Saved information on 3 classes.
Instrument time: 186ms

[INFO] Instrumentation was successful.
...
[INFO] Generating "Cobertura Test Coverage" report.
[INFO] Cobertura 1.9 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
Cobertura: Loaded information on 3 classes.
Report time: 481ms

[INFO] Cobertura Report generation was successful.

And the report looks like this: cobertura report

Was it helpful?

Solution

I suspect that you're missing an execution of cobertura plugin during the compile phase so that the code only gets instrumented by the reporting plugins, in the site lifecycle, after the tests were run. So the test runs aren't picked up because they run on non-instrumented code. Analyze your build logs more carefully - if I'm right, you'll notice that surefire tests are executed before cobertura:instrument.

My configuration is similar to yours, but in addition to specifying the clean exectution in pluginManagement (like you), I specify the cobertura plugin explicitly in build plugins section:

  <build>
  ...
    <plugins>
    ...
      <plugin>
        <inherited>true</inherited>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>${cobertura.plugin.version}</version>
      </plugin>
    </plugins>
  </build>

My configuration sorta works, and all Cobertura stuff is in the global organization-wide pom, which all projects use as a parent.

This way projects don't specify anything Cobertura-related in their pom.xml's, but they still generate coverage reports.

OTHER TIPS

I haven't been succesful at getting Cobertura to combine reporting from multi-projects. This has been a problem in general with multi-project reporting.

We have been evaluating sonar as a solution for our metrics reporting. It seems to do a great job of providing summary metrics across projects, including multi-proijects.

The solution implemented by me is somewhat manual, but works. It consists of several steps of one is a step to combine the several .ser files that are generated by Cobertura. This can be done by using the cobertura-merge commandline tool inside a maven task.

According to the output you show is that the files are not actually instrumented, it tells that only 3 files are instrumented.

@Marco is right, it is not possible to achieve this normally through maven only as the maven cobertura plugin is missing a merge goal.

You can achieve it through a mix of maven and ant goals : http://thomassundberg.wordpress.com/2012/02/18/test-coverage-in-a-multi-module-maven-project/

Nevertheless, in the case you have one single project undertest, there is no need to merge. You can, in the test project, copy the .ser file and the instrumented classes from the project under test :

//in test project
<plugin> 
<groupId>com.github.goldin</groupId>
<artifactId>copy-maven-plugin</artifactId>
<version>0.2.5</version>
<executions>
    <execution>
    <id>copy-cobertura-data-from-project-under-test</id>
    <phase>compile</phase>
    <goals>
        <goal>copy</goal>
    </goals>
    <configuration>
    <resources>
        <resource>
                        <directory>${project.basedir}/../<project-under-test>/target/cobertura</directory>
                            <targetPath>${project.basedir}/target/cobertura</targetPath>
                <includes>                  
                              <include>*.ser</include>
                </includes>
           </resource>
           <resource>
                    <directory>${project.basedir}/../<project-under-test>/target/generated-classes/cobertura/</directory>
                    <targetPath>${project.basedir}/target/generated-classes/cobertura</targetPath>
                    <preservePath>true</preservePath>
           </resource>
        </resources>
            </configuration>
        </execution>
</executions>
</plugin>

//in parent project
<build>
<plugins>
    <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>cobertura-maven-plugin</artifactId>
    <configuration>
        <format>xml</format>
        <aggregate>true</aggregate>
    </configuration>
    <executions>
        <execution>
                    <goals>
                <goal>clean</goal>
            </goals>
        </execution>
    </executions>
</plugin>
    </plugins>
</build>
<reporting>
<plugins>
    <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>cobertura-maven-plugin</artifactId>
    <version>${cobertura.version}</version>
        </plugin>
</plugins>
 </reporting>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top