Kristel
Kristel

Reputation: 183

Include Jacoco repots in Maven site

I've integrated the jacoco-maven-plugin in my project, based on this excellent guide: http://www.petrikainulainen.net/programming/maven/creating-code-coverage-reports-for-unit-and-integration-tests-with-the-jacoco-maven-plugin/

The Jacoco plugin runs fine. However, the maven-site-plugin does not include the Jacoco reports in the site. To be more specific: the 'Project Reports' section does not list the Jacoco reports. The Jacoco reports themselves are available in the target/site/jacoco-ut and target/site/jacoco-it directories.

Here's what I did (without success so far).

First, included the jacoco-maven-plugin as a plugin in the build section of my pom.xml, as explained in the guide referenced above. I'm using Jacoco version 0.6.4.201312101107.

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>${jacoco.plugin.version}</version>
  <executions>
    <!-- scissors... -->
    <!-- report goal is bound to the pre-site phase -->
  </executions>
</plugin>

Second, included the jacoco-maven-plugin in the report section of my pom.xml: no success.

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>${jacoco.plugin.version}</version>
</plugin>

Third, tried to add a reportsets section to the jacoco-maven-plugin in the report section: no success.

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>${jacoco.plugin.version}</version>
  <reportSets>
    <reportSet>
      <reports>
        <report>report</report>
      </reports>
    </reportSet>
  </reportSets>
</plugin>

Can anyone help me to make the maven-site-plugin reference the coverage reports generated by Jacoco in the 'Project Reports' section of the site?

Upvotes: 12

Views: 11505

Answers (3)

nineninesevenfour
nineninesevenfour

Reputation: 862

Short Answer

You have been quite close to the solution, but

  • a section report does not exist in pom.xml, you need reporting (maybe just misspelled?)
  • reportSets is not a property of build -> plugins -> plugin either, but of reporting -> plugins -> plugin.

So in order to have the JaCoCo report in the generated Maven site, you will specify the JaCoCo Maven plugin twice:

  1. Under build -> plugins -> plugin to enable the Java agent (no need to add the report goal here).
  2. reporting -> plugins -> plugin to specify the reportSet.

The most simple snippet is:

    <!-- goes directly under tag "project" -->
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco.plugin.version}</version>
                <reportSets>
                    <reportSet>
                        <reports>
                            <report>report</report>
                        </reports>
                    </reportSet>
                </reportSets>
            </plugin>
        </plugins>
    </reporting>

Long Answer

Although it is also possible to manually add a link to the JaCoCo report through customizing the output of maven-site-plugin (e.g. through site.xml), this is not the easiest and not the intended way. JaCoCo's ReportMojo implements MavenMultiPageReport, which allows the maven-site-plugin to detect it as reporting plugin. However just by reading the documentation it is far from obvious how it works.

When specifying the report goal under build -> plugins the maven-site-plugin has no clue the JaCoCo report exists. Only when given under reporting -> plugins, and there it is called report instead of goal, although it is actually the same thing. When specified here, the (reporting) plugin will be started through maven-site-plugin indirectly (and not directly through the Maven build).

However for the report to have the necessary information, the JaCoCo output must exist before reporting, and this is only the case when the goal prepare-agent (or prepare-agent-integration for integration tests) has been added to the jacoco-maven-plugin under build -> plugins. Phew! 😅

JaCoCo has three reports (which can be triggered both ways):

  • report for a single Maven project
  • report-aggregate for a multi-module Maven project
  • report-integration for integration tests

All of them are detected (and also executed in case reportSet is omitted) by the maven-site-plugin automatically:

[INFO] --- site:3.12.1:site (default-site) @ java-playground ---
[INFO] configuring report plugin org.jacoco:jacoco-maven-plugin:0.8.11
[INFO] 3 reports detected for jacoco-maven-plugin:0.8.11: report, report-aggregate, report-integration

Long story short, a complete pom.xml for a simple single Maven project might look like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>java-playground</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.release>17</maven.compiler.release>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.11</version>
                <executions>
                    <execution>
                        <id>prepare</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <!-- no explicit report generation here!
                         (commented for illustration purposes)
                    <execution>
                        <id>report</id>
                        <phase>pre-site</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                    -->
                </executions>
            </plugin>
        </plugins>
    </build>

    <reporting>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.11</version>
                <reportSets>
                    <reportSet>
                        <reports>
                            <report>report</report>
                        </reports>
                    </reportSet>
                </reportSets>
            </plugin>
        </plugins>
    </reporting>

    <!-- whatever you use for your tests -->
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.10.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>3.24.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

The outcome will look like this: enter image description here

See also:

Upvotes: 3

flags
flags

Reputation: 511

What I did was merge the two execution files (jacoco-ut.exec and jacoco-it.exec) into a single file named jacoco.exec and then set that file as the dataFile of the plugin.

First, let's define these properties:

<jacoco.it.execution.data.file>${project.build.directory}/coverage-reports/jacoco-it.exec</jacoco.it.execution.data.file>
<jacoco.ut.execution.data.file>${project.build.directory}/coverage-reports/jacoco-ut.exec</jacoco.ut.execution.data.file>
<jacoco.execution.data.file>${project.build.directory}/coverage-reports/jacoco.exec</jacoco.execution.data.file>

Then, along with the configuration to generate the reports, include this snippet to create the merged file:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>${jacoco-plugin.version}</version>
    <executions>
        ...
        <execution>
            <id>merge</id>
            <phase>verify</phase>
            <goals>
                <goal>merge</goal>
            </goals>
            <configuration>
                <fileSets>
                    <fileSet>
                        <directory>${project.build.directory}/coverage-reports</directory>
                        <includes>
                            <include>*.exec</include>
                        </includes>
                    </fileSet>
                </fileSets>
                <destFile>${jacoco.execution.data.file}</destFile>
            </configuration>
        </execution>
        <execution>
            <id>report-merge</id>
            <phase>verify</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <dataFile>${jacoco.execution.data.file}</dataFile>
            </configuration>
        </execution>
    </executions>
</plugin>

And, finally, configure the plugin in the reporting section to generate the report from that merged file:

<reporting>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <configuration>
                <dataFile>${jacoco.execution.data.file}</dataFile>
            </configuration>
            <reportSets>
                <reportSet>
                    <reports>
                        <report>report</report>
                    </reports>
                </reportSet>
            </reportSets>
        </plugin>
        ...
    </plugins>
</reporting>

Upvotes: 1

GMelo
GMelo

Reputation: 231

I've found jacoco to be very tricky, the below config worked for me, i put it together from bits and pieces from blogs in the properties

<jacoco.reportPath>${main.basedir}/target/jacoco.exec</jacoco.reportPath>
    <jacoco.itReportPath>${main.basedir}/target/jacoco-it.exec</jacoco.itReportPath>

inside the build

<plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.6.4.201312101107</version>
                <executions>
                    <execution>
                        <id>pre-unit-test</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <destFile>${sonar.jacoco.reportPath}</destFile>
                            <propertyName>utCoverageAgent</propertyName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>post-unit-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <dataFile>${sonar.jacoco.reportPath}</dataFile>
                        </configuration>
                    </execution>
                    <!-- prepare agent for measuring integration tests -->
                    <execution>
                        <id>agent</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <destFile>${sonar.jacoco.itReportPath}</destFile>
                            <propertyName>itCoverageAgent</propertyName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>jacoco-site</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <dataFile>${sonar.jacoco.itReportPath}</dataFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

       <!-- Reporting plugins -->
        <plugin>
            <artifactId>maven-site-plugin</artifactId>
            <version>${plugin.site.version}</version>
            <configuration>
                <attach>true</attach>
                <reportPlugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-changelog-plugin</artifactId>
                        <version>2.2</version>
                        <configuration>
                            <type>range</type>
                            <range>1</range>
                            <!--<displayFileDetailUrl>${project.scm.url}/tree/master/%FILE%</displayFileDetailUrl>-->
                            <headingDateFormat>MM-dd-yyyy</headingDateFormat>
                            <outputEncoding>${project.reporting.outputEncoding}</outputEncoding>

                        </configuration>
                        <reports>
                            <report>changelog</report>
                        </reports>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-project-info-reports-plugin</artifactId>
                        <version>2.7</version>
                        <configuration>
                            <dependencyDetailsEnabled>false</dependencyDetailsEnabled>
                            <dependencyLocationsEnabled>false</dependencyLocationsEnabled>
                        </configuration>
                        <!-- simpler configuration without reportSets available for usual cases -->
                        <!-- distribution-management, index, dependencies, help, issue-tracking, plugins, cim,
                        license, dependency-management, mailing-list, project-team, dependency-convergence,
                        scm, plugin-management, modules, summary -->
                        <reports>
                            <report>index</report>
                            <report>dependencies</report>
                            <report>issue-tracking</report>
                            <report>scm</report>
                            <report>summary</report>
                        </reports>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jxr-plugin</artifactId>
                        <version>2.4</version>
                        <configuration>
                            <aggregate>true</aggregate>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <version>2.9.1</version>
                            <reports>
                                <report>javadoc</report>
                                <report>aggregate</report>
                            </reports>
                        <configuration>
                            <failOnError>false</failOnError>
                        </configuration>
                    </plugin>

                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <version>0.6.4.201312101107</version>
                    </plugin>

                </reportPlugins>
            </configuration>
            <executions>
                <execution>
                    <id>attach-descriptor</id>
                    <goals>
                        <goal>attach-descriptor</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Hope that helps !

Upvotes: 4

Related Questions