Java 带Maven的JaCoCo-缺少执行数据文件
我们有一个Maven多模块项目,由一个父级(HelloWorld)和不同的子级(HelloWorldServices和HelloWorldPresentation)组成,并使用Jenkins进行构建 运行成功测试后的错误为Java 带Maven的JaCoCo-缺少执行数据文件,java,maven,maven-3,pom.xml,jacoco-maven-plugin,Java,Maven,Maven 3,Pom.xml,Jacoco Maven Plugin,我们有一个Maven多模块项目,由一个父级(HelloWorld)和不同的子级(HelloWorldServices和HelloWorldPresentation)组成,并使用Jenkins进行构建 运行成功测试后的错误为 [INFO] --- jacoco-maven-plugin:0.7.6.201602180812:report (default-cli) @ HelloWorldServices --- [INFO] Skipping JaCoCo execution due to mi
[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>
因为当我省略jacoco目标时,它甚至不会显示信息消息。您不应该在安装阶段之后调用代理,而应该在安装阶段之前调用,因此不要调用:
mvn clean install jacoco:prepare-agent jacoco:report
你应该调用
mvn clean jacoco:prepare-agent install jacoco:report
主要原因是:代理将不参与构建生命周期,测试
阶段将作为安装
阶段的一部分执行,然后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>
org.jacoco
目标已使用${project.build.directory}/jacoco.exec
作为默认值destFile
值,这同样适用于目标的dataFile
值。此更改的主要原因是更灵活和标准的构建,不依赖于artifactId
作为项目名称(默认,但仍然不是必需的),而是使用更通用的${project.build.directory}
属性直接指向目标
最后注意:确保在build/plugins
部分中配置Jacoco插件执行,而不是build/pluginManagement/plugins
部分。pluginManagement
部分用于版本或配置的管理和通用协调,但如果在build/plugins
下不声明相应的插件,则该部分将被忽略
依照
插件管理:是一个可以在插件旁边看到的元素。插件管理包含插件元素的方式与此基本相同,不同之处在于它不是为这个特定的项目构建配置插件信息,而是配置从这个项目构建继承的项目构建但是,这仅配置在子元素的plugins元素中实际引用的插件。孩子们完全有权覆盖pluginManagement
定义
(注意:粗体是我的)我认为“destfile”和“datafile”是区分大小写的,所以试着用“destfile”和“datafile”替换它们,也许会有用:)
- JaCoCo报告是从执行数据文件创建的
- 如果此文件不存在,则JaCoCo报告目标将跳过报告创建
- 因此,必须创建执行数据文件
执行数据文件无法创建的原因如下
-测试不存在。
-忽略所有测试。
-缺少Surefire插件。
-JaCoCo的prepare agent目标未执行,这将设置需要配置为surefire的argLine。
-Surefire插件未配置JaCoCo的代理。
<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>
org.jacoco
jacocomaven插件
0.8.5
**/领域/**
**/列举/**
**/例外情况/**
配制剂
报告
测试
报告
杰科科支票
检查
包裹
指示
覆盖层
65%
我与您分享我的回答可能是其他人为他工作
org.jacoco
jacocomaven插件
0.8.5
配制剂
报告
测试
报告
trymvn clean jacoco:prepare agent install
相反(两个阶段之间的代理)我几天前遇到了相同的问题,在我的例子中,问题是我没有遵循名称约定。测试文件应命名为*test.java。当我遵循命名约定时,问题就消失了。这为我解决了一个非常重要的问题。当只调用mvn install
时,我需要做什么才能使这项工作正常进行?如果我没有显式调用jacoco目标,它们将不会被执行。@dasLort您使用的是哪个版本的Maven?我无法用一个示例多模块项目重现您的问题,我没有收到任何跳过警告,并且正确地获得了jacoco reportsMaven 3.3.9和(Jenkins)Maven集成插件2.12.1。你说你可以mvn安装
,它将通过上面的配置调用jacoco?我发现编辑META-INF/plexus/default-bindings.xml文件可以做到这一点。可悲的是,我认为当使用Jenkins Maven插件时,这个插件会在一个罐子里。此外,我没有发现said-jar(不在maven-core-3.0.jar中)这个答案是如此准确!我花了好几个小时寻找其他答案,但不知怎么的,这些答案并没有提供这类信息。我的问题现在解决了,非常感谢!
<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>
I share with you my response may be someone else work for him
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<!-- <configuration>
<excludes>
<exclude>package you want exclude</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>
<!-- <execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.9</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>-->
</executions>
</plugin>