user1107888 user1107888 - 1 month ago 28
Java Question

JaCoCo Report Format

For my small Java/Maven application , I am using JaCoCo in my POM.xml as under:

<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<configuration>
<destFile>${basedir}/target/coverage-reports/jacoco.exec</destFile>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>


As long as I dont use the
<destFile>
parameter, the default reports in
target/site/jacoco
folder are correctly generated in XML, CSV and HTML format. But when I use the
<destFile>
element in order to change the default folder where the reports are generated, only the
jacoco.exec
file is generated and nothing else. How can I change the report folder as well as get the reports in csv, xml and html formats?

Answer

With the destFile parameter, you changed the location where the prepare-agent goal will write the execution data file. By default, this is ${project.build.directory}/jacoco.exec, meaning (still by default) target/jacoco.exec. However, the report goal expects the path to the execution file to be passed in the dataFile parameter, which, of course, defaults to ${project.build.directory}/jacoco.exec, so that they are in sync. Therefore, if you want to change the path to this execution file, you need to match those two parameters. In order not to duplicate the path, you can use a Maven property to do that:

<properties>
  <jacoco.execution.file>${project.build.directory}/coverage-reports/jacoco.exec</jacoco.execution.file>
</properties>
<build>
  <plugins>
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>0.7.7.201606060606</version>
      <configuration>
        <destFile>${jacoco.execution.file}</destFile>
        <dataFile>${jacoco.execution.file}</dataFile>
      </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>
  </plugins>
</build>

Note that this will not change the output directory of the JaCoCo reports; this just changes the path to the execute file. For that, you can use the outputDirectory parameter:

Output directory for the reports. Note that this parameter is only relevant if the goal is run from the command line or from the default build lifecycle. If the goal is run indirectly as part of a site generation, the output directory configured in the Maven Site Plugin is used instead.

and have the following configuration element added:

<configuration>
  <!-- rest of your JaCoCo configuration -->
  <outputDirectory>${project.build.directory}/coverage-reports/jacoco</outputDirectory>
</configuration>

This will make sure all of the HTML, XML, and CSV reports are generated under target/coverage-reports/jacoco. Take note that this configuration will not be used as part of the Maven Site generation when launching mvn site. During site generation, you'll need to configure the outputDirectory of the maven-site-plugin instead.

Comments