Generating a JaCoCo code coverage report with Maven
Asked Answered
O

3

5

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.

Omnipresent answered 16/4, 2019 at 21:21 Comment(0)
C
9

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

Cauterize answered 17/4, 2019 at 6:22 Comment(1)
Ah, I also had my jacoco plugin inside pluginManagement. Thanks!Rest
C
3

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/

Calliper answered 17/4, 2019 at 9:4 Comment(0)
G
0

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/

Goggin answered 28/11, 2023 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.