How to view HTML coverage report using Cobertura Maven plugin?
Asked Answered
W

4

12

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.

Whall answered 18/5, 2011 at 13:46 Comment(3)
Use jacoco-It is much better than cobertura technikes.com/… You also get report in 3 formats namely HTML,CSV and XMLMuseum
@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.Whall
May be it will help other developers. Hope to solve any of your error in future.ThanksMuseum
P
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.

Pauper answered 18/5, 2011 at 13:52 Comment(8)
Good catch, but I still get the same result. I've updated the question.Whall
@FarmBoy - you need to add the cobertura plugin in the <build> section (to execute cobertura) and in the <reporting> section (to create the report. And you may have to add the site goal (mvn cobertura:cobertura site) to actually build the (html) sitePauper
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?Whall
@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)Pauper
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.Whall
@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 artifactPauper
@andreas-d probably, you are talking about mvn dependency:treeBerniecebernier
Need to look at ..\target\site\cobertura\index.htm and NOT ..\target\site\cobertura\cobertura\index.htmPankhurst
C
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.

Clytemnestra answered 21/2, 2013 at 5:44 Comment(3)
Hope this helps someone. I'm not revisiting this old question.Whall
I think <plugin> should be wrapped in a <plugins> element. If so, suggest the answer is amended as it doesn't workBerke
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.Clytemnestra
P
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.

Pauper answered 18/5, 2011 at 13:52 Comment(8)
Good catch, but I still get the same result. I've updated the question.Whall
@FarmBoy - you need to add the cobertura plugin in the <build> section (to execute cobertura) and in the <reporting> section (to create the report. And you may have to add the site goal (mvn cobertura:cobertura site) to actually build the (html) sitePauper
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?Whall
@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)Pauper
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.Whall
@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 artifactPauper
@andreas-d probably, you are talking about mvn dependency:treeBerniecebernier
Need to look at ..\target\site\cobertura\index.htm and NOT ..\target\site\cobertura\cobertura\index.htmPankhurst
F
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.

Farce answered 16/1, 2020 at 18:42 Comment(0)
M
-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

Museum answered 3/6, 2017 at 13:50 Comment(3)
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.Whall
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.ThanksMuseum
This answer actually doesn't address the question about cobertura, it provides a way to do it with another plugin.Shrieve

© 2022 - 2024 — McMap. All rights reserved.