24

In my project I need to create Cobertura Code Coverage report from Hudson using maven build.
In Hudson I have added the Cobertura Code Coverage plugin.
I need the complete modification steps of pom.xml.

oers
  • 18,436
  • 13
  • 66
  • 75
stackoverflowsk
  • 275
  • 1
  • 5
  • 10
  • Beware of the Java 7 incompatibility! A possible [solution is shown here][1]. [1]: http://stackoverflow.com/questions/7010665/testng-emma-cobertura-coverage-and-jdk-7-result-in-classformaterror-and-verif/9583305#9583305 – user1050755 Mar 18 '13 at 01:40

4 Answers4

33

Did you try to add this to your pom.xml in the reporting section?

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>cobertura-maven-plugin</artifactId>
    <configuration>
       <formats>
           <format>html</format>
           <format>xml</format>
       </formats>
    </configuration>
</plugin>

Complete configuration steps can be found here.

ewernli
  • 38,045
  • 5
  • 92
  • 123
  • I have added configuration under section 'Execute cobertura only from hudson using profiles' available at 'http://wiki.hudson-ci.org/display/HUDSON/Cobertura+Plugin' in my pom.xml Also added the configuraiton under 'reporting' tag for cobertura-maven-plugin But while executing the build from hudson - I am getting the following exception Skipping Cobertura coverage report as build was not UNSTABLE or better ... Finished: FAILURE I have configured the plugin "Publish Cobertura Coverage Report" in Hudson Am I missing any steps during configuration? – stackoverflowsk Jan 07 '10 at 12:38
  • 1
    check your logs, it should show some failure messages. also running mvn site cobertura:cobertura on a local checkout might help. Also as a note, for me, just running "cobertura:cobertura" as a goal was enough, no pom changes were necessary (possibly because an upper level pom already included it...) – rogerdpack Dec 31 '10 at 00:07
24

Hudson needs a you to generate the coverage.xml file. To do this without changing your pom.xml, you can use:

mvn cobertura:cobertura -Dcobertura.report.format=xml
Andrew McKnight
  • 634
  • 1
  • 6
  • 12
  • 1
    On my system the `` section of the plugin is not respected.. However, it works when I specify the above `-Dcobertura.report.format` parameter in the line of goals. Thx amcknight – matthaeus Jul 24 '14 at 15:02
  • 1
    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? – PRATHAP S Sep 07 '17 at 13:12
  • @PRATHAPS, I know it is a very late comment, but did you figure out how to fix this? Both your solution and Johan Norén's answers runs the test cases twice. Did you figure out to run the test cases only once and generate the cobertura report from that single test run? – Amudhan Apr 05 '18 at 09:37
  • @Amudhan what I figured out was it will run twice always. But it doesn't matter as it is not affecting any performance since tests will run only before build. Later started using Jacaco – PRATHAP S Apr 09 '18 at 05:26
  • @PRATHAPS, thanks a lot for your reply! My only concern was, we run the full build before merging the code in to the repo. Developers will (should) do it before merging. If the tests run twice, it will cause waste developers' time. The plan was not just to create the coverage report in build systems, but to check and fail the build if the sufficient coverage is not present. So, I can understand from your reply and my experience that it will always run twice if we want to check. BTW, jacoco doesn't run twice? Can it achieve reporting and checking during a single test run? Thanks in advance! – Amudhan Apr 09 '18 at 06:34
  • 1
    @Amudhan Yes, Jacoco doesn't run tests twice. We can achieve reporting and all other features what Cobertura have – PRATHAP S Apr 09 '18 at 08:32
8

To run Cobertura during package phase, do

 <plugin>  
            <groupId>org.codehaus.mojo</groupId>  
            <artifactId>cobertura-maven-plugin</artifactId>  
            <version>2.5.2</version>  
            <configuration>  
                <formats>  
                    <format>xml</format>  
                </formats>  
            </configuration>  
            <executions>  
                <execution>  
                    <phase>package</phase>  
                    <goals>  
                        <goal>cobertura</goal>  
                    </goals>  
                </execution>  
            </executions>  
   </plugin>         

Heres an example of pom

http://macgyverdev.blogspot.com/2011/04/development-environment-for-google-app.html

And here how to integrate in Hudson http://macgyverdev.blogspot.com/2011/04/hudson-continous-integration-for-google.html

cfeduke
  • 23,100
  • 10
  • 61
  • 65
Johan Norén
  • 627
  • 2
  • 7
  • 14
  • With this configuration, the test cases are run twice. Any idea how to fix it so that the tests are run only once, and the cobertura report is generated using that? – Amudhan Apr 05 '18 at 09:36
1

Cobertura doesn't actually seem to work with hudson.

I have a project where executing the command line: mvn clean package

Builds a coverage report generates an accurate coverage report with an average coverage of about 78% line and 74% branch.

Running the same goals on a Hudson server results in a coverage report showing 0% 0%.

Unfortunately the Jira site for the plugin doesn't seem to allow anyone to post issues so this issue is as yet unreported to the team.

SteveO
  • 11
  • 1