21

I am running Jacoco's Maven plugin. The prepare-agent goal runs fine, but does not generate jacoco.exec file for some reason. Subsequently the report goal complains Skipping JaCoCo execution due to missing execution data file.

Any ideas?

Gili
  • 86,244
  • 97
  • 390
  • 689

2 Answers2

36

Having read https://groups.google.com/forum/#!topic/jacoco/LzmCezW8VKA, it turns out that prepare-agent sets a surefire property called argLine. If you override this property (something that https://issues.apache.org/jira/browse/SUREFIRE-951 encourages you to do) then jacoco never ends up running.

The solution is to replace:

<argLine>-Dfile.encoding=${project.build.sourceEncoding}</argLine>

with

<argLine>-Dfile.encoding=${project.build.sourceEncoding} ${argLine}</argLine>

Meaning, append jacoco's argLine to the new value.

UPDATE: As pointed out by Fodder, if you aren't always running JaCoCo and no other plugin sets ${argLine} then Maven will complain that ${argLine} is undefined. To resolve this, simply define what ${argLine} should look like when JaCoCo is skipped:

<properties>
    <argLine/>
</properties>

In this case use @{argLine} instead of ${argLine} as explained here.

borjab
  • 11,149
  • 6
  • 71
  • 98
Gili
  • 86,244
  • 97
  • 390
  • 689
  • I would add that it's needed only if you change argLine configuration for surefire plugin. Otherwise default configuration is OK. – Sergii Shevchyk Apr 16 '14 at 19:55
  • 2
    If you are using Tycho (for Eclipse plugins and applications), use ${tycho.testArgLine} instead of ${argLine}. – n8n8baby May 22 '14 at 23:11
  • Man, youre a lifesaver. Spent whole day fighting with jacoco - it couldnt generate exec file and surefire has no special arguments. Adding makes everything work. Thanks! – Vladimir Marton Nov 26 '19 at 13:46
3

If you aren't always running JaCoCo when building then @Gili's solution doesn't work, as it can't find argLine. Instead add a property argLine in you POM with the custom values. JaCoCo's prepare agent will append to that property, and Surefire will use the appended argLine.

<properties>
    <argLine>whatever your custom argline variables are</argLine>
</properties>

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <!-- Don't put argLine config here! -->
    </plugin>
</plugins>
Fodder
  • 564
  • 1
  • 10
  • 23
  • Yes, that's true. You need special handling if JaCoCo is disabled but I handled it differently. I've updated my answer accordingly. – Gili Jul 04 '16 at 14:42