Skipping JaCoCo execution due to missing execution data file

# 情境分析
今天在为项目集成sonar单元测试覆盖率的过程中,发现了许多问题,在这里记录下,为大家提供一下解决思路


# 单测覆盖率执行思路
1. 首先通过maven打包,然后生成测试文件
2. sonar扫描导入maven生成的测试文件,出现单测覆盖率


# 可能出现问题的点
1. 可能没有生成测试文件
2. 生成的测试文件可能为空,没有数据


# 上面两个问题,如何确定呢? (通过查看日志)
1. 使用 Surefire 配置破坏 JaCoCo 代理
在maven install打包的日志中,出现如下日志信息
[INFO] Skipping JaCoCo execution due to missing execution data file.

或者,不会出现如下信息

[INFO] --- jacoco-maven-plugin:0.8.5:prepare-agent (default-prepare-agent) @ sirius ---
[INFO] surefireArgLine set to -javaagent:/builds/situ/situ-java-program/situ-galaxy/sir-server/sir/./.m2/repository/org/jacoco/org.jacoco.agent/0.8.5/org.jacoco.agent-0.8.5-runtime.jar=destfile=/builds/situ/situ-java-program/situ-galaxy/sir-server/sir/target/jacoco.exec,includes=com/situ/sir/**/*,excludes=**/po/**/*:**/config/**/*

2. 虽然单个测试类可以运行也没有错,但是maven install打包的时候,run的数字为0
[INFO] Results:
[INFO] 
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
# pom文件示例配置 (第一种)

<properties>
  <!-- sonar单测扫描 -->
  <jacoco.version>0.8.5</jacoco.version>
  <jacoco.reportPath>${project.basedir}/target/jacoco-ut.exec</jacoco.reportPath>
</properties>

<dependency>
  <groupId>org.jacoco</groupId>
  <artifactId>org.jacoco.agent</artifactId>
  <version>${jacoco.version}</version>
  <classifier>runtime</classifier>
</dependency>

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
            <configuration>
                <systemPropertyVariables>
                    <jacoco-agent.destfile>${jacoco.reportPath}</jacoco-agent.destfile>
                </systemPropertyVariables>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco.version}</version>
            <configuration>
                <includes> <!--覆盖率计算包含的类-->
                    <include>com/cc/situ/**/*</include>
                </includes>
                <excludes>
                    <exclude>**/po/**/*</exclude>
                    <exclude>**/config/**/*</exclude>
                </excludes>
            </configuration>

            <executions>
                <execution>
                    <id>report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal><!--生成覆盖率文件和报告-->
                    </goals>
                    <configuration>
                        <dataFile>${jacoco.reportPath}</dataFile>
                    </configuration>
                </execution>
            </executions>
</plugin>

如果你按照第一种不行,在日志中出现错误情况的第一种

那么可能是Surefire 配置破坏 JaCoCo 代理

请采用第二种方案

# pom文件示例配置 (第二种)

<properties>
  <!-- sonar单测扫描 -->
  <jacoco.version>0.8.5</jacoco.version>
  <jacoco.reportPath>${project.basedir}/target/jacoco-ut.exec</jacoco.reportPath>
</properties>

<dependency>
  <groupId>org.jacoco</groupId>
  <artifactId>org.jacoco.agent</artifactId>
  <version>${jacoco.version}</version>
  <classifier>runtime</classifier>
</dependency>


<plugins>
   <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco.version}</version>
                <configuration>
                    <includes> <!--覆盖率计算包含的类-->
                        <include>com/situation/sirius/**/*</include>
                    </includes>
                    <excludes> <!--排除像Pojo、配置等无单元测试意义的类。如有,在Sonar服务端也需要配合排除(Ignore Code Coverage: https://docs.sonarqube.org/latest/project-administration/narrowing-the-focus/#header-5)-->
                        <exclude>**/po/**/*</exclude>
                        <exclude>**/config/**/*</exclude>
                    </excludes>
                </configuration>
                <executions>
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <propertyName>surefireArgLine</propertyName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal><!--生成覆盖率文件和报告-->
                        </goals>
                        <configuration>
                            <dataFile>${jacoco.reportPath}</dataFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                    <forkMode>once</forkMode>
                    <reuseForks>true</reuseForks>
                    <testFailureIgnore>true</testFailureIgnore>
                    <!--suppress UnresolvedMavenProperty -->
                    <argLine>-Dfile.encoding=UTF-8 ${surefireArgLine}</argLine>
                    <systemPropertyVariables>
                        <jacoco-agent.destfile>${jacoco.reportPath}</jacoco-agent.destfile>
                    </systemPropertyVariables>
                </configuration>
   </plugin>
</plugins>

请注意maven依赖也会影响,是否能够检测出单测覆盖率

# 请使用第一个,不要使用第二个

1. 第一个
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <version>${springboot.version}</version>
</dependency>

2. 第二个
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-test</artifactId>
   <version>${springboot.version}</version>
</dependency>

我在idea中点击右侧的maven标志,并进行run maven bulid时报错:[INFO] --- jacoco:0.8.7:report (report) @ pureedgesim --- [INFO] Skipping JaCoCo execution due to missing execution data file. [INFO] [INFO] --- jar:3.2.0:jar (default-jar) @ pureedgesim --- [WARNING] JAR will be empty - no content was marked for inclusion! [INFO] [INFO] --- source:3.2.1:jar-no-fork (attach-sources) @ pureedgesim --- [INFO] No sources in project. Archive not created. [INFO] [INFO] --- javadoc:3.2.0:jar (attach-javadocs) @ pureedgesim --- [INFO] [INFO] --- install:3.1.2:install (default-install) @ pureedgesim --- [INFO] Installing D:\Users\KEN\K\testtest\PureEdgeSim-master\PureEdgeSim-master\pom.xml to C:\Users\KEN\.m2\repository\com\mechalikh\pureedgesim\5.3.0\pureedgesim-5.3.0.pom [INFO] Installing D:\Users\KEN\K\testtest\PureEdgeSim-master\PureEdgeSim-master\target\pureedgesim-5.3.0.jar to C:\Users\KEN\.m2\repository\com\mechalikh\pureedgesim\5.3.0\pureedgesim-5.3.0.jar [INFO] [INFO] --- deploy:3.1.2:deploy (default-deploy) @ pureedgesim --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.920 s [INFO] Finished at: 2025-03-27T17:35:53+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:3.1.2:deploy (default-deploy) on project pureedgesim: Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::url parameter -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException Process finished with exit code 1
最新发布
03-28
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值