JaCoCo 在跳过 JaCoCo 执行后才生成 jacoco.exec

2024-03-02

我在我的模块之一中通过 JaCoCo 生成 AHP 报告时遇到问题。当构建开始时,我看到 JaCoCo 正确设置 argLine 为:

[INFO] jacoco.agent.argLine set to -javaagent:<...>/.m2/repository/org/jacoco/org.jacoco.agent/0.7.2.201409121644/org.jacoco.agent-0.7.2.201409121644-runtime.jar=destfile=<...>/target/jacoco.exec

然而,当 Maven 尝试运行 JaCoCo 时,.exec 尚未创建:

[INFO] Skipping JaCoCo execution due to missing execution data file:<...>/target/jacoco.exec

在 maven 跳过 JaCoCo 执行之后,jacoco.exec 最终会被创建。因此,如果我重新运行构建而不进行清理,我仍然可以生成 AHP 报告。

我在其他各种问题中看到我需要小心使用 Maven Surefire 和 JaCoCo。但是,我没有在 Surefire 插件或任何与此相关的插件中明确使用 argLine。我开始怀疑是否其他插件之一会像 JaCoCo 一样自动劫持 argLine 参数。

以下是所有使用的插件的列表:

  • jacoco-maven-插件
  • vertx-maven-插件
  • maven-资源-插件
  • maven 依赖插件
  • maven-surefire-插件
  • maven-failsafe-插件
  • maven-surefire-报告插件
  • maven 组件插件

我确实在构建输出中看到一条可疑消息:

[INFO] --- maven-compiler-plugin:3.0:compile (default-compile) @ <module> ---
[INFO] Changes detected - recompiling the module!

我不确定这是否相关,但它在“跳过”消息之前出现两次,并且不会出现在 JaCoCo 正常工作的模块中。

有任何想法吗?

*编辑-这是 jacoco 配置

    <plugins>
        <...>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <pluginManagement>
        <plugins>
            <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.jacoco</groupId>
                                    <artifactId>
                                        jacoco-maven-plugin
                                    </artifactId>
                                    <versionRange>
                                        [0.7.2.201409121644,)
                                    </versionRange>
                                    <goals>
                                        <goal>prepare-agent</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore></ignore>
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>

我不确定插件管理部分到底在做什么,但是将其注释掉并不能解决任何问题。我还尝试将 JaCoCo 插件配置放在surefire/failsafe 配置之上,以防顺序对共享相同目标的插件很重要,但这也没有帮助。

*编辑2 - 看起来问题是surefire 的包含。注释掉它们可以以某种方式修复 JaCoCo 的 .exec 生成,并且 JaCoCo 可以正常工作。

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven.surefire.plugin.version}</version>
            <configuration>
                <!-- <includes>
                    <include>**/unit/**/*Test*.java</include>
                </includes> -->
            </configuration>
        </plugin>

有人知道为什么吗?


只是对已经给出的答案的补充。 可能会发生这样的情况:在您的 maven-surefire-plugin 配置中,您已经使用 argLine 配置来覆盖诸如所用内存之类的内容。如果这样做,将不会使用 jacoco-maven-plugin 设置的 argline,从而无法生成 jacoco 报告。 在这种情况下,为 jacoco-maven-plugin 配置分配一个属性名称,然后在 maven-surefire-plugin argLine 参数中引用它。

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.9</version>
            <executions>
                <!-- prepare agent for measuring unit tests -->
                <execution>
                    <id>prepare-unit-tests</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <append>true</append>
                        <destFile>${sonar.jacoco.reportPath}</destFile>
                        <!-- Sets the VM argument line used when unit tests are run. -->
                        <propertyName>surefireArgLine</propertyName>
                    </configuration>
                </execution>
            </executions>
        </plugin>

[...]

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <printSummary>false</printSummary>
                <redirectTestOutputToFile>true</redirectTestOutputToFile>
                <forkCount>3</forkCount>
                <reuseForks>true</reuseForks>
                <argLine>${surefireArgLine} -Xmx1024m -noverify</argLine>                  
            </configuration>
        </plugin>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JaCoCo 在跳过 JaCoCo 执行后才生成 jacoco.exec 的相关文章

随机推荐