我们有一个 Maven 多模块项目,由一个父项 (HelloWorld) 和不同的子项 (HelloWorldServices 和 HelloWorldPresentation) 组成,并使用 Jenkins 进行构建。
运行测试成功后的错误是
[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
前面几行
[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
这就是我定义父 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>
我没有在 pom 中明确提到 surefire。我还尝试了您在任何地方都能找到的将 argLine 放入配置中的方法,但结果都相同。 JaCoCo .exec 文件从未创建过,无论我做什么。至于目标,我用
mvn clean install jacoco:prepare-agent jacoco:report
因为当我省略 jacoco 目标时,它甚至不显示 INFO 消息。
最佳答案
您不应在安装阶段之后而是之前调用代理,因此不要调用:
mvn clean install jacoco:prepare-agent jacoco:report
你应该调用
mvn clean jacoco:prepare-agent install jacoco:report
主要原因是:代理不会参与构建生命周期,test
阶段已经作为install
阶段的一部分执行,然后Maven会执行代理根据命令行调用,但为时已晚。
您可能还应该将上面的插件配置更改为:
<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>
注意:我删除了配置部分,因为它实际上指向默认值。此外,XML 元素在这里区分大小写,因此您的 datafile
元素被简单地忽略了,它应该是 dataFile
而不是。这同样适用于 destFile
。
prepare-agent
目标已经使用 ${project.build.directory}/jacoco.exec
作为默认 destFile
值,同样适用于 dataFile
值report
目标。此更改的主要原因是更灵活和标准的构建,不依赖 artifactId
作为项目名称(默认,但仍然不是强制性的)并使用更通用的 ${project. build.directory
属性直接指向 target
。
最后的注意事项:确保在 build/plugins
部分而不是 build/pluginManagement/plugins
部分配置 Jacoco 插件执行。 pluginManagement
部分用于版本或配置的治理和共同协调,但如果相应的插件未在 build/plugins< 下声明,它将被忽略/
.
根据 official Maven POM reference
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 element in the children. The children have every right to override
pluginManagement
definitions.
(注意:粗体是我的)
关于java - JaCoCo 与 Maven - 缺少执行数据文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36304793/