Nicryc
Nicryc

Reputation: 576

Generating a JaCoCo code coverage report with Maven

I don't understand, I try to generate code coverage report with JaCoCo and Maven, the simplest.

I have the following plugin in my pom.xml :

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>${jacoco.version}</version>
    <executions>
      <execution>
        <id>prepare-agent</id>
        <goals>
          <goal>prepare-agent</goal>
        </goals>
      </execution>
      <execution>
        <id>report</id>
        <phase>prepare-package</phase>
        <goals>
          <goal>report</goal>
        </goals>
      </execution>
      <execution>
        <id>post-unit-test</id>
        <phase>test</phase>
        <goals>
          <goal>report</goal>
        </goals>
        <configuration>
          <!-- Sets the path to the file which contains the execution data. -->

          <dataFile>target/jacoco.exec</dataFile>
          <!-- Sets the output directory for the code coverage report. -->
          <outputDirectory>target/my-reports</outputDirectory>
        </configuration>
      </execution>
    </executions>
    <configuration>
      <systemPropertyVariables>
        <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
      </systemPropertyVariables>
  </configuration>
</plugin>

And when I try to do a mvn test, it just doesn't do anything. Not even an error or something. It say BUILD SUCESS for my tests but Maven seems to not see JaCoCo. If I try to execute mvn jacoco:report anyway I have a message : Skipping JaCoCo execution due to missing execution data file.

Upvotes: 5

Views: 24213

Answers (4)

Sanjay Bharwani
Sanjay Bharwani

Reputation: 4839

My answer is based on SpringBoot Version 3.3.8 and JaCoCo Plugin Version 0.8.12

Add below plugin to the pom.xml Below snippet configures the JaCoCo (Java Code Coverage) plugin for maven project. It also excludes the two package from the coverage check. These packages could be model classes, generated classes or anything which should be excluded from coverage.

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.8.12</version>
  <configuration>
   <excludes>
    <exclude>com/example/package1/**/*</exclude>
    <exclude>com/example/package2/*/*</exclude>
   </excludes>
  </configuration>
  <executions>
   <execution>
    <goals>
     <goal>prepare-agent</goal>
    </goals>
   </execution>
   <execution>
    <id>report</id>
    <phase>prepare-package</phase>
    <goals>
     <goal>report</goal>
    </goals>
   </execution>
  </executions>
</plugin>

And this works perfectly fine.

Files will be generated at below locations

target/site/jacoco/jacoco.xml

target/site/jacoco/index.html

Upvotes: 0

Camille
Camille

Reputation: 2541

I finally succeed with help of Baeldung, thanks to them! It was mainly configuration part where I struggled.

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.11</version>
    <executions>
        <execution>
            <phase>verify</phase>
            <goals>
                <goal>report-aggregate</goal>
            </goals>
            <configuration>
                <dataFileIncludes>
                    <dataFileInclude>**/jacoco.exec</dataFileInclude>
                </dataFileIncludes>
                <outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate/</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

For a maven multi-modules project, change

outputDirectory = ${maven.multiModuleProjectDirectory}/target/site/jacoco-aggregate/

Upvotes: 2

dicle
dicle

Reputation: 1162

This should work. Just run from command "mvn clean test"

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.2</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>

https://www.mkyong.com/maven/maven-jacoco-code-coverage-example/

Upvotes: 3

wirnse
wirnse

Reputation: 1146

The following configuration should be enough:

<build>
  <plugins>
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>${jacoco.version}</version>
      <executions>
        <execution>
          <id>prepare-agent</id>
          <goals>
            <goal>prepare-agent</goal>
          </goals>
        </execution>
        <execution>
          <id>report</id>
          <phase>test</phase>
          <goals>
            <goal>report</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

The reports can then be found in target/site/jacoco/

Reasons why it does not work in your case:

  • The Plugin configuration is inside pluginManagement
  • The Plugin is inside a profile

Also check the maven log when you execute mvn test for jacoco-maven-plugin. For more information run mvn -X test

Upvotes: 10

Related Questions