3

Consider the follow build configuration:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
      <argLine>${surefireArgLine}</argLine>
      <parallel>all</parallel>
      <threadCount>2</threadCount>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <configuration>
      <argLine>${failsafeArgLine}</argLine>
      <parallel>classes</parallel>
      <threadCount>2</threadCount>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <executions>
      <execution>
        <id>pre-unit-test</id>
        <goals>
          <goal>prepare-agent</goal>
        </goals>
        <configuration>
          <destFile>${project.build.directory}/jacoco-ut.exec</destFile>
          <propertyName>surefireArgLine</propertyName>
        </configuration>
      </execution>
      <execution>
        <id>post-unit-test</id>
        <phase>test</phase>
        <goals>
          <goal>report</goal>
        </goals>
        <configuration>
          <dataFile>${project.build.directory}/jacoco-ut.exec</dataFile>
        </configuration>
      </execution>
      <execution>
        <id>pre-integration-test</id>
        <phase>pre-integration-test</phase>
        <goals>
          <goal>prepare-agent</goal>
        </goals>
        <configuration>
          <destFile>${project.build.directory}/jacoco-it.exec</destFile>
          <propertyName>failsafeArgLine</propertyName>
        </configuration>
      </execution>
      <execution>
        <id>post-integration-test</id>
        <phase>post-integration-test</phase>
        <goals>
          <goal>report</goal>
        </goals>
        <configuration>
          <dataFile>${project.build.directory}/jacoco-it.exec</dataFile>
        </configuration>
      </execution>
    </executions>
  </plugin>

And reporting configuration:

  <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <reportSets>
      <reportSet>
        <id>JaCoCo-UnitTest</id>
        <reports>
          <report>report</report>
        </reports>
        <configuration>
          <dataFile>${project.build.directory}/jacoco-ut.exec</dataFile>
        </configuration>
      </reportSet>
      <reportSet>
        <id>JaCoCo-IntegrationTest</id>
        <reports>
          <report>report</report>
        </reports>
        <configuration>
          <dataFile>${project.build.directory}/jacoco-it.exec</dataFile>
        </configuration>
      </reportSet>
    </reportSets>
  </plugin>

The awesome part is mvn clean site works awesome, I get my reports. However, this now means that the JaCoCo agent is starting during the normal lifecycle, ie: mvn clean install verify.

Is there a way to disable the JaCoCo agent during the normal lifecycle but not the site cycle?

Jonathan S. Fisher
  • 8,189
  • 6
  • 46
  • 84

1 Answers1

8

Simply skip the JaCoCo execution using a property:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>${jacoco-maven-plugin.version}</version>
    <executions>
        <execution>
            <id>pre-unit-test</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <!-- Skip this phase if jacoco.skip property is set to true -->
                <skip>${jacoco.skip}</skip>
                <destFile>${project.build.directory}/jacoco-ut.exec</destFile>
                <propertyName>surefireArgLine</propertyName>
            </configuration>
        </execution>

        <execution>
            <id>post-unit-test</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <!-- Skip this phase if jacoco.skip property is set to true -->
                <skip>${jacoco.skip}</skip>
                <dataFile>${project.build.directory}/jacoco-ut.exec</dataFile>
            </configuration>
        </execution>
    </executions>
</plugin>

You can also disable JaCoCo execution when tests are skipped:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${maven-surefire-plugin.version}</version>
    <configuration>
        <argLine>${surefireArgLine}</argLine>
        <!-- Skip tests if skip.unit.tests property is set to true -->
        <skipTests>${skip.unit.tests}</skipTests>
    </configuration>
</plugin>

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>${jacoco-maven-plugin.version}</version>
    <executions>
        <execution>
            <id>pre-unit-test</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <!-- Skip this phase if unit tests are skipped -->
                <skip>${skip.unit.tests}</skip>
                <destFile>${project.build.directory}/jacoco-ut.exec</destFile>
                <propertyName>surefireArgLine</propertyName>
            </configuration>
        </execution>

        <execution>
            <id>post-unit-test</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <!-- Skip this phase if unit tests are skipped -->
                <skip>${skip.unit.tests}</skip>
                <dataFile>${project.build.directory}/jacoco-ut.exec</dataFile>
            </configuration>
        </execution>
    </executions>
</plugin>
Florian Lopes
  • 1,093
  • 1
  • 13
  • 20
  • 1
    I would remove `` override in the Surefire plugin and I would swap out `${jacoco.skip}` with `${skipTests}` to honor the Maven Surefire plugin's default `skipTests` property. See [_Maven Surefire Plugin > Skipping Tests_](https://maven.apache.org/surefire/maven-surefire-plugin/examples/skipping-tests.html) for more information. This way `mvn clean install -DskipTests` will ignore the JaCoCo excution goals. – Mr. Polywhirl May 04 '21 at 17:56