11

I want to generate and view a coverage report for a Java-Maven project.

I've added the following to pom.xml:

<reporting>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>cobertura-maven-plugin</artifactId>
      <version>2.2</version>
      <configuration>
        <formats>
          <format>html</format>
        </formats>
      </configuration>
    </plugin>
  </plugins>
</reporting>

When I run mvn cobertura:cobertura it builds successfully and reports to the console that Cobertura Report generation was successful but I can't find the report.

If I cd into target/cobertura I find a file called cobertura.ser but I have no idea what to do with it.

Edit: After re-examining the docs, thanks to Andreas_D, I've added the <reporting> tag, but get the same result.

Eric Wilson
  • 57,719
  • 77
  • 200
  • 270
  • Use jacoco-It is much better than cobertura http://technikes.com/how-to-generate-code-coverage-report-in-java-jacoco-graphical-report/ You also get report in 3 formats namely HTML,CSV and XML – saksham agarwal Jun 03 '17 at 13:51
  • @sakshamagarwal Well, I asked this question is 2011, for the record. In 2017, I'm thankful to not be dealing with Java/Maven/XML nonsense. – Eric Wilson Jun 05 '17 at 12:21
  • May be it will help other developers. Hope to solve any of your error in future.Thanks – saksham agarwal Jun 07 '17 at 15:32

4 Answers4

19

This in the Build section:

<build>
    ...
    <plugins>
        ...    
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.5.2</version>
        </plugin>
        ...
    </plugins>
    ...
</build>

And then this in the Reporting section:

<reporting>
    ...
    <plugins>
        ... 
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.5.2</version>
            <configuration>
                <check></check>
                <formats>
                    <format>html</format>
                    <format>xml</format>
                </formats>
            </configuration>
        </plugin>
        ...
    </plugins>
    ...
</reporting>

Execute mvn cobertura:cobertura

Then look for index.html inside the target/site/cobertura/ folder.

acdcjunior
  • 132,397
  • 37
  • 331
  • 304
AStyle1
  • 307
  • 3
  • 5
  • Hope this helps someone. I'm not revisiting this old question. – Eric Wilson Feb 21 '13 at 11:30
  • I think should be wrapped in a element. If so, suggest the answer is amended as it doesn't work – peter.murray.rust Aug 28 '13 at 16:36
  • Yes peter.murray.rust, you're right and I was under the impression that developers won't just copy paste the entire thing but place the necessary code at the right place. Adjusted the answer. – AStyle1 Sep 20 '13 at 06:19
6

Have a look at the plugin's documentation, there's an example. Pretty sure that you have to add a <reporting> element to actually produce the report.

Kariem
  • 4,398
  • 3
  • 44
  • 73
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • Good catch, but I still get the same result. I've updated the question. – Eric Wilson May 18 '11 at 14:03
  • 1
    @FarmBoy - you need to add the cobertura plugin in the `` section (to execute cobertura) and in the `` section (to create the report. And you may have to add the `site` goal (`mvn cobertura:cobertura site`) to actually build the (html) site – Andreas Dolk May 18 '11 at 14:11
  • Hmm, the `site` target creates a dependency problem. It can't find `org.apache.maven.doxia:doxia-core:jar:1.0-alpha-11`. Our repository has `alpha-10`. Why is it looking for this at all? – Eric Wilson May 18 '11 at 15:22
  • @FarmBoy - you'll have to solve the dependency issues. "Execute" the pom on a machine that is connected to the internet, then import the new local repository from that machine to your repository (looks like your disconnected from the web) – Andreas Dolk May 18 '11 at 15:29
  • I'm forced to go through my companies internal repository, and I understand that this is my problem. I'm just baffled why adding the `site` target would create new dependencies. – Eric Wilson May 18 '11 at 15:34
  • @FarmBoy - that's maven :-) If your curious - there's one goal you can call that just shows the dependency tree for a pom (don't know by heart - check the maven site), then you can see *who's* using the artifact – Andreas Dolk May 18 '11 at 15:40
  • @andreas-d probably, you are talking about `mvn dependency:tree` – Slava Semushin May 19 '11 at 03:52
  • Need to look at ..\target\site\cobertura\index.htm and NOT ..\target\site\cobertura\cobertura\index.htm – Smart Coder Jul 31 '15 at 20:07
0

I just tried here in my project, you can do the following also:

a) run mvn cobertura:cobertura
b) it should generate a folder called 'site'
c) you can open the index.html (inside site/sobertura folder) in whatever browser and inspect the results of coverage.

-1

If nothing works - Try this http://technikes.com/how-to-generate-code-coverage-report-in-java-jacoco-graphical-report/ you can get code coverage report in html and xml format

This is amazing as I publish my report in html and xml

saksham agarwal
  • 250
  • 3
  • 14
  • This question is far to old for me to have a chance at reproducing the problem, so I won't be voting on new answers. – Eric Wilson Jun 05 '17 at 12:22
  • 1
    But this answer is not for increase my vote.May be it will help other developers. Hope to solve any of your error in future.Thanks – saksham agarwal Jun 07 '17 at 15:31
  • 2
    This answer actually doesn't address the question about cobertura, it provides a way to do it with another plugin. – rodmunera Jun 09 '17 at 14:06