为什么在单个测试为 运行 时不生成 jacoco.exec - 但在所有测试为 运行 时生成?

Why no jacoco.exec produced when a single test is run - but it is produced when all tests are run?

我正在 运行使用 jacoco 代理进行万无一失的测试。当我 运行 mvn verify 生成一个 jacoco.exec 文件时。

当我 运行 mvn clean verify -Dtest=com.org.MyTest -DfailIfNoTests=false 时,没有生成 jacoco.exec 文件。

这是我的 surefire 配置。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <configuration>
    </configuration>
    <executions>
        <execution>
            <phase>test</phase>
            <id>testconfig</id>
            <configuration>
                <argLine>${test.jvm.options} ${jacoco.agent.argLine}</argLine>
                <skip>false</skip>
            </configuration>
            <goals><goal>test</goal></goals>
        </execution>
    </executions>
</plugin>

这是我的 jacoco 配置

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.5.201505241946</version>
    <configuration>
        <properties>
            <property>
                <name>listener</name>
                <value>org.sonar.java.jacoco.JUnitListener</value>
            </property>
        </properties>
    </configuration>
    <executions>
        <execution>
            <id>unit_agent</id>
            <phase>initialize</phase>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <propertyName>jacoco.agent.argLine</propertyName>
            </configuration>
        </execution>                           
    </executions>
</plugin>

我的问题是:为什么在单个测试为 运行 时没有生成 jacoco.exec - 但在所有测试为 运行 时生成?

mvn clean verify -Dtest=com.org.MyTest -DfailIfNoTests=false 的执行日志显示类似(我使用的是 Apache Maven 3.3.9):

[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ example ---
[INFO] Surefire report directory: /private/tmp/jacoco-example/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.org.MyTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec - in com.org.MyTest

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (testconfig) @ example ---
[INFO] Skipping execution of surefire because it has already been run for this configuration

注意 maven-surefire-plugin 执行了两次 - 一次使用 id default-test 和另一次使用 id testconfig 的执行实际上被跳过了,而只有使用 id testconfig 的配置${jacoco.agent.argLine}.

maven-surefire-plugin 的定义更改于

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <configuration>
        <argLine>${jacoco.agent.argLine}</argLine>
    </configuration>
</plugin>

问题已解决。