Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is prepare-agent goal is necessary in jacoco-maven-plugin?

Tags:

java

maven

jacoco

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?

like image 894
jianfeng Avatar asked Aug 31 '17 03:08

jianfeng


People also ask

What is prepare agent in JaCoCo?

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

Why is JaCoCo not showing coverage for some classes?

Why does the coverage report not show highlighted source code? Make sure the following prerequisites are fulfilled to get source code highlighting in JaCoCo coverage reports: Class files must be compiled with debug information to contain line numbers. Source files must be properly supplied at report generation time.


1 Answers

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.

like image 64
Naman Avatar answered Sep 23 '22 11:09

Naman