javamavenexec-maven-plugin

Maven "process-classes" phase called for each reporting plugin (multiple executions)


I have a Maven artefact that includes data computed at compile-time

This is achieved by using the exec-maven-plugin to call the generating class.

This is correctly being executed when the artefact is built, but when reporting plugins are added, the execution is happening multiple times, slowing it down significantly.

Take the following pom.xml:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.me</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <main.class>test.Runner</main.class>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
    </properties>
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-report-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-report-plugin</artifactId>
            </plugin>
        </plugins>
    </reporting>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>process-classes</phase>
                        <id>${main.class}</id>
                        <goals>
                            <goal>java</goal>
                        </goals>
                        <configuration>
                            <mainClass>${main.class}</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.9.1</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

When clean install site is run, the test.Runner is called three time - and each time a reporting plugin is added, it's run again.


Solution

  • Try changing the reports config to this:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-report-plugin</artifactId>
      <version>${maven.surefire.plugin.version}</version>
      <reportSets>
        <reportSet>
          <reports>
            <report>report-only</report>
          </reports>
        </reportSet>
      </reportSets>
    </plugin>
    

    This tells Maven which reports should be run. Without that reportSet config,

    every reporting goal available in the plugin is rendered once

    per the Maven Site plugin docs. This is important because as @khmarbaise notes, the report mojo

    Invokes the execution of the lifecycle phase test prior to executing itself.