JaCoCo with Maven - missing execution data file

46,616

Solution 1

You should not invoke the agent after the install phase but before, so instead of invoking:

mvn clean install jacoco:prepare-agent jacoco:report

You should invoke

mvn clean jacoco:prepare-agent install jacoco:report

The main reason is: the agent will not participate to the build lifecycle, the test phase will already be executed as part of the install phase, then Maven will execute the agent as per command line invocation, but it will be too late.


You should probably also change the plugin configuration above to:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.6.201602180812</version>
    <executions>
        <execution>
            <id>jacoco-initialize</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>jacoco-site</id>
            <phase>package</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Note: I removed the configuration section, because it was actually pointing to default values. Moreover, XML elements are case sensitives here, so your datafile element was simply ignored, it should have been dataFile instead. The same applies to destFile.

The prepare-agent goal is already using ${project.build.directory}/jacoco.exec as default destFile value, the same applied to the dataFile value for the report goal. The main reason for this change would be a more flexible and standard build, not relying on artifactId as project name (the default, but still not mandatory) and using the more generic ${project.build.directory} property instead to point directly at target.


Final note: make sure to configure the Jacoco Plugin executions within the build/plugins section and not build/pluginManagement/plugins section. The pluginManagement section is meant for governance and common harmonization of versions or configurations, but it will be ignored if the corresponding plugin would not be declared under build/plugins.
As per official Maven POM reference

pluginManagement: is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one. However, this only configures plugins that are actually referenced within the plugins element in the children. The children have every right to override pluginManagement definitions.

(note: bold is mine)

Solution 2

  • JaCoCo reports get created from the execution data file.
  • If this file is not present then JaCoCo report goal skips the report creation.
  • So it is compulsory to create the execution data file.

Reasons due to which execution data file will not get created are the following
- Tests are not present.
- All tests are ignored.
- Surefire plugin is missing.
- JaCoCo's prepare-agent goal is not executed, which sets argLine which is needed to configure to surefire.
- Surefire plugin is not configured with JaCoCo's agent.

Solution 3

I think that "destfile" and "datafile" are case sensitive so try to replace them with "destFile" and "dataFile", maybe it'll work :)

Solution 4

I just dealt with this issue today and found out that this happening when I set the argLine parameter in Surefire plugin (which I needed to do for running JavaFx-related tests) which replaces the default even after you have prepare-agent in your jacoco-maven-plugin goals.

So basically, the solution is to add the original argLine and append your additional arg lines using @{argLine} as noted here:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.0.0-M5</version>
  <configuration>
    <argLine>@{argLine} --enable-native-access ALL-UNNAMED --add-modules jdk.incubator.foreign</argLine>
  </configuration>
</plugin>

It mentioned in that post to assign the jacoco agent argline variable ${surefire.argLine} but i think those steps are not necessary (at least not in my case).

Share:
46,616

Related videos on Youtube

dasLort
Author by

dasLort

Updated on July 09, 2022

Comments

  • dasLort
    dasLort almost 2 years

    We have a Maven multi module project consisting of a parent (HelloWorld) and different children (HelloWorldServices and HelloWorldPresentation) and use Jenkins to build.

    The error after running the successful test is

    [INFO] --- jacoco-maven-plugin:0.7.6.201602180812:report (default-cli) @ HelloWorldServices ---
    [INFO] Skipping JaCoCo execution due to missing execution data file:/var/lib/jenkins/workspace/HelloWorld/HelloWorldServices/target/jacoco.exec
    

    The lines before it says

    [INFO] --- jacoco-maven-plugin:0.7.6.201602180812:prepare-agent (default-cli) @ HelloWorldServices ---
    [INFO] argLine set to -javaagent:/var/lib/jenkins/.m2/repository/org/jacoco/org.jacoco.agent/0.7.6.201602180812/org.jacoco.agent-0.7.6.201602180812-runtime.jar=destfile=/var/lib/jenkins/workspace/HelloWorld/HelloWorldServices/target/jacoco.exec
    

    This is how I defined the parent pom JaCoCo plugin:

    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.7.6.201602180812</version>
        <configuration>
            <destfile>${project.artifactId}/target/jacoco.exec</destfile>
            <datafile>${project.artifactId}/target/jacoco.exec</datafile>
        </configuration>
    
        <executions>
            <execution>
                <id>jacoco-initialize</id>
                <goals>
                    <goal>prepare-agent</goal>
                </goals>
            </execution>
            <execution>
                <id>jacoco-site</id>
                <phase>package</phase>
                <goals>
                    <goal>report</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

    In no pom did I explicitly mention surefire. I also tried what you find everywhere to put the argLine in the configuration but all with the same result. The JaCoCo .exec file has never been created, no matter what I do. As for the goals, I use

    mvn clean install jacoco:prepare-agent jacoco:report
    

    Since when I omit the jacoco goals, it doesn't even display the INFO message.

    • A_Di-Matteo
      A_Di-Matteo about 8 years
      try mvn clean jacoco:prepare-agent install instead (the agent between the two phases)
    • Sajjad Salehi
      Sajjad Salehi over 4 years
      I ran into the same problem a few days ago, in my case, the problem was that I was not following the name convention. The test file should be named as *Test.java. As soon as I followed the naming convention, the problem disappeared.
    • Dhaval D
      Dhaval D almost 3 years
      A very good answer that solved problem for me.
  • dasLort
    dasLort about 8 years
    what do I need to do to make this work when only calling mvn install ? If I don't explicitly call the jacoco goals, they won't be executed.
  • A_Di-Matteo
    A_Di-Matteo about 8 years
    @dasLort which version of Maven are you using? I could not reproduce your issue with a sample multi module project, I didn't get any skip warning and properly got jacoco reports
  • dasLort
    dasLort about 8 years
    Maven 3.3.9 and (Jenkins) Maven Integration Plugin 2.12.1. So you say you can mvn install and it will with above configuration call jacoco?
  • dasLort
    dasLort about 8 years
    I found that editing the META-INF/plexus/default-bindings.xml file could do the trick. Sadly, I think this one would be inside a jar when using the Jenkins Maven plugin. Moreover, I did not find said jar (not in maven-core-3.0.jar)
  • A_Di-Matteo
    A_Di-Matteo about 8 years
    @dasLort indeed you should not change it, which changes made it work?
  • dasLort
    dasLort about 8 years
    It's still not working. I assume you MUST edit that file but not being able to configure this via Jenkins makes it highly unlikely that this is the way to go (since the jar could be overwritten any time). Could be OK if that file was persistent (i.e. no Jenkins plugin) but in my case, it is just a plugin. btw source: maven.apache.org/ref/3.3.9/maven-core/default-bindings.html
  • A_Di-Matteo
    A_Di-Matteo about 8 years
    @dasLort no, you shouldn't change that file, those are default bindings but you can always change them via the POM file
  • dasLort
    dasLort about 8 years
  • payne
    payne over 4 years
    This answer is so spot on! I spent hours looking through other answers that somehow didn't provide this kind of information. My problem is now solved, thank you so much!
  • Amann Malik
    Amann Malik over 2 years
    Thanks for spelling this out. Lost a lot of time to this one unfortunately.