10

In My project, if I write pom like this:

...
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.9</version>
            <executions> 
                <execution>  
                  <id>post-test</id>  
                  <phase>test</phase>  
                  <goals>  
                        <goal>report</goal>  
                  </goals>  
                </execution>  
           </executions>  

        </plugin>
...

it will not generate report in my project after i run mvn install.

but i change it to

<plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.9</version>
            <executions> 
                <execution>  
                    <id>pre-test</id>  
                    <goals>  
                      <goal>prepare-agent</goal>  
                    </goals>  
                  </execution> 
                <execution>  
                  <id>post-test</id>  
                  <phase>test</phase>  
                  <goals>  
                        <goal>report</goal>  
                  </goals>  
                </execution>  
           </executions>  

        </plugin>

it worked!!

I want to know what was the different?

I read the offical document here: http://www.jacoco.org/jacoco/trunk/doc/prepare-agent-mojo.html

the goal prepare-agent is just for set property for jvm agent, not start jvm agent, why it is necessary?

jianfeng
  • 2,440
  • 4
  • 21
  • 28

1 Answers1

7

Well the link shared by you over prepare:agent already reads much of it:

Prepares a property pointing to the JaCoCo runtime agent that can be
passed as a VM argument to the application under test.

So if the goal is not bound to the plugin execution, then the default behavior for the property would be either argLine or tycho.testArgLine for packaging type eclipse-test-plugin.

In your case, if we assume its argLine, and your project defines the VM arguments for test execution, you need to make sure that they include this property.

One of the ways to do this in case of maven-surefire-plugin - is to use syntax for late property evaluation:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
      <argLine>@{argLine} -your -extra -arguments</argLine>
    </configuration>
</plugin>

The detailed Java Agent doc, can help you understand deeper how Jacoco uses its own agent to provide a mechanism that allows in-memory pre-processing of all class files during class loading independent of the application framework.

Naman
  • 27,789
  • 26
  • 218
  • 353