jacoco-maven-plugin 中是否需要 prepare-agent 目标?

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

在我的项目中,如果我这样写pom:

...
        <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>
...

在我 运行 mvn install 后,它不会在我的项目中生成报告。

但我将其更改为

<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>

成功了!!

我想知道有什么不同?

我在这里阅读了官方文档:http://www.jacoco.org/jacoco/trunk/doc/prepare-agent-mojo.html

目标 prepare-agent 只是为了设置 属性 jvm agent,而不是启动 jvm agent,为什么需要它?

好吧,你在 prepare:agent 上分享的 link 已经读了很多:

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

因此,如果目标未绑定到插件执行,那么 属性 的默认行为对于包装类型 eclipse-test-plugin 将是 argLinetycho.testArgLine .

在您的情况下,如果我们假设它 argLine,并且您的项目定义了用于测试执行的 VM 参数,您需要确保它们包括此 属性。

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>

详细的 Java Agent 文档,可以帮助您更深入地了解 Jacoco 如何使用自己的代理来提供 允许对所有 class 文件进行内存预处理的机制在 class 独立于应用程序框架加载期间。