user1226320
user1226320

Reputation: 453

JaCoCo report generation using maven

I have captured the coverage details in a jacoco.exec file. It is 6Mb in size. I need to generate a coverage report using maven . I tried the below

  <dependencies>
    <dependency>
      <groupId>org.jacoco</groupId>
      <artifactId>org.jacoco.ant</artifactId>
      <version>0.7.0.201403182114</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.6.3.201306030806</version>
        <executions>
          <!--
            Ensures that the code coverage report for integration tests after
            integration tests have been run.
          -->
          <execution>
            <id>post-integration-test</id>
            <phase>post-integration-test</phase>
            <goals>
              <goal>report</goal>
            </goals>
            <configuration>
              <!-- Sets the path to the file which contains the execution data. -->
              <dataFile>D:/JaCoCo/jacoco.exec</dataFile>
              <!-- Sets the output directory for the code coverage report. -->
              <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>      
    </plugins>
  </build>

It is generating a coverage report of zero percentage.

Before the test the jacoco.exec file was zero bytes in size, and now it is 6MB in size. Is there something I am missing in the pom.xml?

Upvotes: 31

Views: 99502

Answers (3)

subhashis
subhashis

Reputation: 4888

You need to add two plugins

            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.7</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <!-- attached to Maven test phase -->
                    <execution>
                        <id>report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>

Upvotes: 0

Karthik Prasad
Karthik Prasad

Reputation: 10024

Here is the jacoco plug-in configuration.

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.5.10.201208310627</version>
    <configuration>
        <skip>${maven.test.skip}</skip>
        <output>file</output>
        <append>true</append>
    </configuration>
    <executions>
        <execution>
            <id>jacoco-initialize</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>jacoco-site</id>
            <phase>verify</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>
  1. On running maven:test it will generate jacoco.exec file
  2. On running jacoco:report it generates report in html file under target/site/jacoco directory. You can view the report by opening index.html

Upvotes: 41

Chirag
Chirag

Reputation: 349

Run mvn jacoco:report it will generate coverage report. Open target/site/jacoco/index.html file to get pictorial view.

Upvotes: 34

Related Questions