This question is more focused on understanding maven life circle than in solving a real problem.
We have a project with several maven modules. Both Jacoco and Surefire plugins are configured in the parent pom.xml as follows:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.0</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/jacoco.exec</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>default-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<argLine>${surefireArgLine}</argLine>
</configuration>
</plugin>
This configuration works well and the jacoco.exec files are created on the target directory when common goals are executed, for instance:
mvn clean install
or
mvn test
But if we execute the following commands the jacoco.exec files are not created:
mvn clean install -DskipTests
#other actions here...
mvn jacoco:prepare-agent surefire:test
Analyzing the logs with the -X option, surefire plugin indicates that it is going to use the argLine property as it is expected:
<argLine>${surefireArgLine}</argLine>
The goal jococo:prepare-agent generates the value for this variable correctly:
argLine set to -javaagent:s:\\m2_repo\\org\\jacoco\\org.jacoco.agent\\0.8.0\\org.jacoco.agent-0.8.0-runtime.jar=destfile=S:\\sources\\sofia2-s4c\\config\\services\\target\\jacoco.exec
But when surefire:test is executed it does not use the argLine property:
[DEBUG] (s) additionalClasspathElements = []
argLine SHOULD BE HERE!!!!
[DEBUG] (s) basedir = S:\sources\sofia2-s4c\config\services
We have solved this executing:
mvn clean install -DskipTests
#other actions here...
mvn test
Due to mvn test detect that there are no changes in the compiled classes this is efficient. But I would like to understand why the first approach does not work.