Maven – JaCoCo not generating jacoco.exec until after skipping JaCoCo execution

jacocomaven

I'm having trouble generating AHP reports via JaCoCo in one of my modules. When the build starts, I see JaCoCo correctly setting argLine with:

[INFO] jacoco.agent.argLine set to -javaagent:<...>/.m2/repository/org/jacoco/org.jacoco.agent/0.7.2.201409121644/org.jacoco.agent-0.7.2.201409121644-runtime.jar=destfile=<...>/target/jacoco.exec

However, the .exec hasn't been created yet by the time maven tries to run JaCoCo:

[INFO] Skipping JaCoCo execution due to missing execution data file:<...>/target/jacoco.exec

The jacoco.exec does eventually get created, after maven has skipped JaCoCo execution. Therefore, I can still generate AHP reports, if I re-run the build without cleaning.

I've seen in various other questions that I need to be careful using Maven Surefire with JaCoCo. However, I don't explicitly use argLine in my Surefire plugins, or in any plugin for that matter. I'm starting to wonder if one of the other plugins is hijacking the argLine parameter automatically like JaCoCo does.

Here is a list of all plugins used:

  • jacoco-maven-plugin
  • vertx-maven-plugin
  • maven-resources-plugin
  • maven-dependency-plugin
  • maven-surefire-plugin
  • maven-failsafe-plugin
  • maven-surefire-report-plugin
  • maven-assembly-plugin

I do see one suspicious message in the build output:

[INFO] --- maven-compiler-plugin:3.0:compile (default-compile) @ <module> ---
[INFO] Changes detected - recompiling the module!

I'm not sure if that's relevant, but it appears twice before the Skipping message, and doesn't appear in a module where JaCoCo works properly.

Any ideas?

*edit – Here's the jacoco config

    <plugins>
        <...>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco.version}</version>
            <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>
    <pluginManagement>
        <plugins>
            <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.jacoco</groupId>
                                    <artifactId>
                                        jacoco-maven-plugin
                                    </artifactId>
                                    <versionRange>
                                        [0.7.2.201409121644,)
                                    </versionRange>
                                    <goals>
                                        <goal>prepare-agent</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore></ignore>
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>

I'm not sure exactly what that plugin management part is doing, but commenting it out doesn't fix anything. I've also tried putting the JaCoCo plugin config above the surefire/failsafe config in case order mattered for plugins sharing the same goals, but that didn't help either.

*edit 2 – Looks like the problem was surefire's includes. Commenting them out somehow fixes JaCoCo's .exec generation, and JaCoCo works properly.

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven.surefire.plugin.version}</version>
            <configuration>
                <!-- <includes>
                    <include>**/unit/**/*Test*.java</include>
                </includes> -->
            </configuration>
        </plugin>

Anyone know why?

Best Answer

Just an addition to the answers already given. It could happen that in your maven-surefire-plugin configuration you already use the argLine configuration to override something like the memory used. If you do so the argline set by jacoco-maven-plugin will not be used failing to generate the jacoco report. In this case assign a property name to the jacoco-maven-plugin config and then reference it in your maven-surefire-plugin argLine parameter.

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.9</version>
            <executions>
                <!-- prepare agent for measuring unit tests -->
                <execution>
                    <id>prepare-unit-tests</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <append>true</append>
                        <destFile>${sonar.jacoco.reportPath}</destFile>
                        <!-- Sets the VM argument line used when unit tests are run. -->
                        <propertyName>surefireArgLine</propertyName>
                    </configuration>
                </execution>
            </executions>
        </plugin>

[...]

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <printSummary>false</printSummary>
                <redirectTestOutputToFile>true</redirectTestOutputToFile>
                <forkCount>3</forkCount>
                <reuseForks>true</reuseForks>
                <argLine>${surefireArgLine} -Xmx1024m -noverify</argLine>                  
            </configuration>
        </plugin>
Related Topic