Java JaCoCo 与 Maven - 缺少执行数据文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36304793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:41:56  来源:igfitidea点击:

JaCoCo with Maven - missing execution data file

javamavenmaven-3pom.xmljacoco-maven-plugin

提问by dasLort

We have a Maven multi module project consisting of a parent (HelloWorld) and different children (HelloWorldServices and HelloWorldPresentation) and use Jenkins to build.

我们有一个由父级(HelloWorld)和不同子级(HelloWorldServices 和 HelloWorldPresentation)组成的 Maven 多模块项目,并使用 Jenkins 构建。

The error after running the successful test is

运行成功测试后的错误是

[INFO] --- jacoco-maven-plugin:0.7.6.201602180812:report (default-cli) @ HelloWorldServices ---
[INFO] Skipping JaCoCo execution due to missing execution data file:/var/lib/jenkins/workspace/HelloWorld/HelloWorldServices/target/jacoco.exec

The lines before it says

它说之前的行

[INFO] --- jacoco-maven-plugin:0.7.6.201602180812:prepare-agent (default-cli) @ HelloWorldServices ---
[INFO] argLine set to -javaagent:/var/lib/jenkins/.m2/repository/org/jacoco/org.jacoco.agent/0.7.6.201602180812/org.jacoco.agent-0.7.6.201602180812-runtime.jar=destfile=/var/lib/jenkins/workspace/HelloWorld/HelloWorldServices/target/jacoco.exec

This is how I defined the parent pom JaCoCo plugin:

这就是我定义父 pom JaCoCo 插件的方式:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.6.201602180812</version>
    <configuration>
        <destfile>${project.artifactId}/target/jacoco.exec</destfile>
        <datafile>${project.artifactId}/target/jacoco.exec</datafile>
    </configuration>

    <executions>
        <execution>
            <id>jacoco-initialize</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>jacoco-site</id>
            <phase>package</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

In no pom did I explicitly mention surefire. I also tried what you find everywhere to put the argLine in the configuration but all with the same result. The JaCoCo .exec file has never been created, no matter what I do. As for the goals, I use

在 pom 中,我没有明确提到surefire。我还尝试了您在任何地方找到的将 argLine 放入配置中的方法,但结果都相同。无论我做什么,都从未创建过 JaCoCo .exec 文件。至于目标,我使用

mvn clean install jacoco:prepare-agent jacoco:report

Since when I omit the jacoco goals, it doesn't even display the INFO message.

因为当我省略 jacoco 目标时,它甚至不显示 INFO 消息。

采纳答案by A_Di-Matteo

You should not invoke the agent after the install phase but before, so instead of invoking:

你不应该在安装阶段之后而是在之前调用代理,所以不要调用:

mvn clean install jacoco:prepare-agent jacoco:report

You should invoke

你应该调用

mvn clean jacoco:prepare-agent install jacoco:report

The main reason is: the agent will not participate to the build lifecycle, the testphase will already be executed as part of the installphase, then Maven will execute the agent as per command line invocation, but it will be too late.

主要原因是:代理不会参与构建生命周期,test阶段已经作为install阶段的一部分执行,然后Maven将根据命令行调用执行代理,但为时已晚。



You should probably also change the plugin configuration above to:

您可能还应该将上面的插件配置更改为:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.6.201602180812</version>
    <executions>
        <execution>
            <id>jacoco-initialize</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>jacoco-site</id>
            <phase>package</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Note: I removed the configuration section, because it was actually pointing to default values. Moreover, XML elements are case sensitives here, so your datafileelement was simply ignored, it should have been dataFileinstead. The same applies to destFile.

注意:我删除了配置部分,因为它实际上是指向默认值。此外,这里的 XML 元素区分大小写,因此您的datafile元素被简单地忽略了,它应该被忽略dataFile。这同样适用于destFile.

The prepare-agentgoal is already using ${project.build.directory}/jacoco.execas default destFilevalue, the same applied to the dataFilevalue for the reportgoal. The main reason for this change would be a more flexible and standard build, not relying on artifactIdas project name (the default, but still not mandatory) and using the more generic ${project.build.directory}property instead to point directly at target.

prepare-agent目标已经使用${project.build.directory}/jacoco.exec默认destFile值,适用于相同dataFile的价值report目标。这种变化的主要原因是更灵活和标准的构建,不依赖于artifactId项目名称(默认,但仍然不是强制性的),而是使用更通用的${project.build.directory}属性直接指向target.



Final note: make sure to configure the Jacoco Plugin executions within the build/pluginssection and not build/pluginManagement/pluginssection. The pluginManagementsection is meant for governance and common harmonization of versions or configurations, but it will be ignoredif the corresponding plugin would not be declared under build/plugins.
As per official Maven POM reference

最后注意:确保在build/plugins部分而不是build/pluginManagement/plugins部分中配置 Jacoco 插件执行。该pluginManagement部分是为治理和版本或配置的共同协调,但它会被忽略,如果相应的插件就不会在声明build/plugins
根据官方 Maven POM 参考

pluginManagement: is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one. However, this only configures plugins that are actually referenced within the plugins elementin the children. The children have every right to override pluginManagementdefinitions.

pluginManagement: 是一个与插件一起看到的元素。插件管理以大致相同的方式包含插件元素,除了不是为此特定项目构建配置插件信息,而是旨在配置从该项目构建继承的项目构建。但是,这仅配置在子项的 plugins 元素实际引用的插件。孩子们完全有权推翻pluginManagement定义。

(note: bold is mine)

(注:粗体是我的)

回答by Bogdan Halmaghi

I think that "destfile" and "datafile" are case sensitive so try to replace them with "destFile" and "dataFile", maybe it'll work :)

我认为“destfile”和“datafile”区分大小写,所以尝试用“destFile”和“dataFile”替换它们,也许它会起作用:)

回答by Shivkumar Kawtikwar

  • JaCoCo reports get created from the execution data file.
  • If this file is not present then JaCoCo report goal skips the report creation.
  • So it is compulsory to create the execution data file.
  • JaCoCo 报告是从执行数据文件中创建的。
  • 如果此文件不存在,则 JaCoCo 报告目标将跳过报告创建。
  • 因此必须创建执行数据文件。

Reasons due to which execution data file will not get created are the following
- Tests are not present.
- All tests are ignored.
- Surefire plugin is missing.
- JaCoCo's prepare-agent goal is not executed, which sets argLine which is needed to configure to surefire.
- Surefire plugin is not configured with JaCoCo's agent.

不会创建执行数据文件的原因如下
- 不存在测试。
- 所有测试都被忽略。
- 缺少 Surefire 插件。
- JaCoCo 的prepare-agent 目标没有执行,这将需要配置的argLine 设置为surefire。
- Surefire 插件没有配置 JaCoCo 的代理。

回答by m.nguyencntt

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.5</version>
    <configuration>
        <target>
            <propertyfile file="lombok.config">
                <entry key="config.stopBubbling" value="true" />
                <entry key="lombok.addLombokGeneratedAnnotation" value="true" />
            </propertyfile>
        </target>
        <excludes>
            <exclude>**/domain/**</exclude>
            <exclude>**/enumeration/**</exclude>
            <exclude>**/exception/**</exclude>
        </excludes>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <!-- attached to Maven test phase -->
        <execution>
            <id>report</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
        <!-- Add this checking -->
        <execution>
            <id>jacoco-check</id>
            <goals>
                <goal>check</goal>
            </goals>
            <configuration>
                <rules>
                    <rule>
                        <element>PACKAGE</element>
                        <limits>
                            <limit>
                                <counter>INSTRUCTION</counter>
                                <value>COVEREDRATIO</value>
                                <minimum>65%</minimum>
                            </limit>
                        </limits>
                    </rule>
                </rules>
            </configuration>
        </execution>
    </executions>
</plugin>