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

jia*_*eng 4 java maven jacoco

在我的项目中,如果我这样编写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>
...
Run Code Online (Sandbox Code Playgroud)

运行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>
Run Code Online (Sandbox Code Playgroud)

有效!!

我想知道有什么不同?

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

目标prepare-agent仅用于jvm代理的set属性,而不是启动jvm代理,为什么有必要?

Nam*_*man 5

好吧,您共享的链接prepare:agent已经阅读了很多内容:

Prepares a property pointing to the JaCoCo runtime agent that can be
passed as a VM argument to the application under test.
Run Code Online (Sandbox Code Playgroud)

因此,如果目标不限于插件执行,则该属性的默认行为将是argLinetycho.testArgLine包装类型eclipse-test-plugin。

对于您而言,如果我们假设其为argLine,并且您的项目定义了用于执行测试的VM参数,则需要确保它们包含此属性。

在使用maven-surefire-plugin的情况下,执行此操作的方法之一是使用语法进行后期属性评估:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
      <argLine>@{argLine} -your -extra -arguments</argLine>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

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