Skip to content

Instantly share code, notes, and snippets.

@timander
Last active August 28, 2021 23:15
Show Gist options
  • Save timander/89e09e384ae3f41a908c to your computer and use it in GitHub Desktop.
Save timander/89e09e384ae3f41a908c to your computer and use it in GitHub Desktop.
Maven JaCoCo Coverage Profile
<profiles>
<profile>
<id>normal</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<surefireArgLine></surefireArgLine> <!--create blank property for surefire when not running under coverage profile-->
<failsafeArgLine></failsafeArgLine> <!--create blank property for failsafe when not running under coverage profile-->
</properties>
</profile>
<profile>
<id>coverage</id>
<properties>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.jacoco.reportPath>${project.build.directory}/jacoco.exec</sonar.jacoco.reportPath>
<sonar.jacoco.itReportPath>${project.build.directory}/jacoco-it.exec</sonar.jacoco.itReportPath>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.1.201405082137</version>
<executions>
<!--
Prepares the property pointing to the JaCoCo runtime agent which
is passed as VM argument when Maven the Surefire plugin is executed.
-->
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<destFile>${sonar.jacoco.reportPath}</destFile>
<!--
Sets the name of the property containing the settings
for JaCoCo runtime agent.
-->
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<!--
Ensures that the code coverage report for unit tests is created after
unit tests have been run.
-->
<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>${sonar.jacoco.reportPath}</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${project.reporting.outputDirectory}/jacoco</outputDirectory>
</configuration>
</execution>
<!-- The Executions required by unit tests are omitted. -->
<!--
Prepares the property pointing to the JaCoCo runtime agent which
is passed as VM argument when failsafe plugin is executed.
-->
<execution>
<id>pre-integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<destFile>${sonar.jacoco.itReportPath}</destFile>
<!--
Sets the name of the property containing the settings
for JaCoCo runtime agent.
-->
<propertyName>failsafeArgLine</propertyName>
</configuration>
</execution>
<!--
Ensures that the code coverage report for integration tests after
integration tests have been run.
-->
<execution>
<id>post-integration-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>${sonar.jacoco.itReportPath}</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
@Ramblurr
Copy link

Ramblurr commented Dec 7, 2016

Thanks for sharing this config. I'm activating the profile with -Pcoverage yet jacoco never seems to be used, surefire execs without the jacoco agent. Is there something else that needs to be done to activate this?

@iluxame
Copy link

iluxame commented Jun 14, 2017

you need to add ${surefireArgLine} to surfire plugin argLine like:
@{surefireArgLine} -Xms512m -Xmx2048m

@kamalcph
Copy link

kamalcph commented Apr 2, 2018

You also need to add ${failsafeArgLine} in failsafe plugin like:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.18.1</version>
    <executions>
        <!-- Ensures that both integration-test and verify goals of the Failsafe Maven plugin are executed. -->
        <execution>
            <id>integration-tests</id>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
             </goals>
             <configuration>
                 <!-- Sets the VM argument line used when integration tests are run. -->
                 <argLine>${failsafeArgLine}</argLine>
             </configuration>
        </execution>
    </executions>
</plugin>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment