What is the proper way to use Cobertura with Maven 3.0.2
Asked Answered
Y

3

21

I have been trying unsuccessfully for the last few days to run Cobertura 2.4 with Maven 3.0.2. We have a very large project with many modules (sub-projects). What I found is that documentation is basically non-existent or plain wrong. All tutorials I was able to find don't work with Maven 3.x (they build, but Cobertura either doesn't run or cannot generate the reports).

Has anyone here been able to make it work? Any useful tips/examples? Thanks.

Yeisk answered 3/8, 2011 at 18:29 Comment(0)
M
23

I successfully integrated Cobertura in my projects with adding this:

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <instrumentation>
            <includes>
              <include>foo/bar/**/*.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>
        </goals>
      </execution>
    </executions>
      </plugin>
    </plugins>
  </build>

  <reporting>
    <plugins>
      <plugin>
    <!-- use mvn cobertura:cobertura to generate cobertura reports -->
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>cobertura-maven-plugin</artifactId>
    <version>2.4</version>
    <configuration>
      <formats>
        <format>html</format>
        <format>xml</format>
      </formats>
     </configuration>
      </plugin>
    </plugins>
  </reporting>

If you run mvn cobertura:cobertura the reports will be generated in target\site\cobertura. See also maven cobertura plugin.


Today I analyze projects with SonarQube. It has an easy installation step (if you are not interested in using an enterprise database) and also includes a code coverage analysis (using JaCoCo) among many other metrics.

Methylene answered 3/8, 2011 at 20:3 Comment(12)
Yes Sonar is the way to go. No changes to your project POM requiredHallam
Sonar is awesome, but it's also nice to be able to generate coverage reports without a full Sonar analysis.Formfitting
@Ryan Stewart Because Sonar gives you information about your product you never wanted to know :-)?Methylene
+1 for the lol. My team likes to have coverage reports on every CI build. Sonar increases the build time about 2.5x, so we only run it nightly. Cobertura alone is only a small increase in build time, so we run it per version control change.Formfitting
We're using sonar, but it runs nightly. What I was looking for was general guidelines on how to integrate it for everyday use. So far, the best way as far as I know, is basically do nothing and run mvn cobertura:cobertura to generate it. Your answer so far has been the best.Yeisk
@Yeisk Integration of code coverage tools in the IDE would be the best for everyday use - I tried ecobertura and EclEmma a bit and remember that they worked both well.Methylene
Cobertura Report generation was successful. Thank you very much! To give my two cents I want to say, that you can also use the <exclude> tag within the <instrumentation> tag. It may be easier to exclude the files, which should not be analyzed, than to include the files you want to have analyzed.Vinegarish
Just installing and running Sonar does not generate any Cobertura coverage reports for meTherine
@matt freake Meanwhile SonarQube changed the coverage tool and now uses Jacoco. You should see the results in the project dashboard.Methylene
@Methylene I have a multi module project, Module A and B has Intgrtn Tests and Module C,D and E have unit test cases. How should I use cobertura. I used cobertura-integration-test and cobertura:cobertura but no luck. Do I need to add above plugin in Module A and B pom files as well?Alta
@Kally You should take a look at questions that handle Cobertura on Multi Module projects like this one. I myself have no experience on that, sorry.Methylene
Is there any way to generate Cobertura reports on mvn clean install command instead of mvn cobertura:cobertura, When I changed the executions- phase as test and goal as cobertura, it works but it is running test cases twice, any idea on this?Physicalism
R
7

In maven 3.0.3 (not yet out when you asked the question), simply use maven's site plug-in and configure it such that it uses cobertura, as follows:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-site-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <reportPlugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>cobertura-maven-plugin</artifactId>
                        <version>2.5.1</version>
                        <configuration>
                            <formats>
                                <format>html</format>
                                <format>xml</format>
                            </formats>
                        </configuration>
                    </plugin>
                </reportPlugins>
            </configuration>
        </plugin>
....
Revers answered 20/2, 2012 at 13:30 Comment(1)
how could we initiate it through pluginExecution in maven ? any guess ?Kumkumagai
V
5

You can also integrate the Cobertura plugin in the <reporting> section of your webapp:

  <reporting>
    <outputDirectory>${project.build.directory}/site</outputDirectory>
    <plugins>
      <!-- Maven Project Info Reports Plugin -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-project-info-reports-plugin</artifactId>
        <version>2.7</version>
        <configuration>
          <dependencyLocationsEnabled>false</dependencyLocationsEnabled>
        </configuration>
      </plugin>
      <!-- Cobertura Code Coverage Plugin -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <instrumentation>
            <ignoreTrivial>true</ignoreTrivial>
          </instrumentation>
          <formats>
            <format>html</format>
            <format>xml</format>
          </formats>
        </configuration>
      </plugin>
    </plugins>
  </reporting>

If you run mvn site, then your report will be available in target/site/cobertura/index.html within the target directory of your application.

Vinegarish answered 31/8, 2014 at 12:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.