带有 maven 的 Jacoco 0.8.8 在 JDK17 和 Sonarqube 9.9.2.77730 中代码覆盖率为零

Jacoco 0.8.8 with maven having zero code coverage with JDK17 and Sonarqube 9.9.2.77730

提问人:Piyush Chaudhari 提问时间:11/13/2023 更新时间:11/13/2023 访问量:72

问:

我正在将我的Spring Boot微服务从Java11升级到Java17,使用maven成功构建和运行服务。我已经添加了用于单元测试用例覆盖率的 jacoco-maven-plugin 插件,但是当我尝试使用 sonarqube 时,我看到代码覆盖率为零。同样的更改在 Java 11 中也正常工作。我的配置如下。

Maven:3.6.1
Java:17.0.7
maven-surefire-插件:2.22.2 jacoco-maven-插件:0.8.8
sonarqube:9.9.2.77730

我正在使用以下命令进行测试用例和覆盖。

mvn clean test


mvn clean verify sonar:sonar \
  -Dsonar.projectKey=test-service \
  -Dsonar.host.url=http://localhost:9000 \
  -Dsonar.login=sqa_57657e1f59a8f2b720badb3b7b2043e004e3832f

Maven 属性如下:

<maven.surefire.version>2.22.2</maven.surefire.version>
<sonar.jacoco.reportPath>target/coverage-reports/jacoco-unit.exec</sonar.jacoco.reportPath>
<sonar.test.reportPath>coverage-reports/</sonar.test.reportPath>
<sonar.jacoco.coverage.output.directory>target/site/jacoco-ut</sonar.jacoco.coverage.output.directory>
<sonar.junit.reportsPath>target/surefire-reports</sonar.junit.reportsPath>
<sonar.projectName>test-service</sonar.projectName>
<sonar.skip>false</sonar.skip>

测试配置文件如下:

<profiles>
    <profile>
        <id>sonar-build</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
            <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${maven.surefire.version}</version>
                    <configuration>
                        <!-- Jacoco prepare-agent builds some command-line params without -->
                        <!-- which jacoco will not instrument. Hence it is important to add -->
                        <!-- those command-line params here (${argLine} holds those params) -->
                        <argLine>${surefireArgLine} -Xms256m -Xmx2048m</argLine>
                        <argLine>
                            --add-opens java.base/java.time=ALL-UNNAMED
                            --add-opens java.base/java.time.format=ALL-UNNAMED
                            --add-opens java.base/java.util=ALL-UNNAMED
                            --add-opens java.base/java.util.stream=ALL-UNNAMED
                            --add-opens java.base/java.lang=ALL-UNNAMED
                        </argLine>
                        <forkCount>1</forkCount>
                        <runOrder>random</runOrder>
                        <destFile>${sonar.test.reportPath}</destFile>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>0.8.8</version>
                    <configuration>
                        <destFile>${sonar.jacoco.reportPath}</destFile>
                        <dataFile>${sonar.jacoco.reportPath}</dataFile>
                        <append>true</append>
                    </configuration>
                    <executions>
                        <execution>
                            <id>jacoco-initialize</id>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                            <configuration>
                                <propertyName>surefireArgLine</propertyName>
                            </configuration>
                        </execution>
                        <execution>
                            <id>jacoco-report</id>
                            <goals>
                                <goal>report</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${sonar.jacoco.coverage.output.directory}</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
sonarqube java-17 jacoco-maven-plugin

评论

1赞 Holger 11/13/2023
在我的系统上,Jacoco 在没有这些选项的情况下运行良好。我什至无法想象为什么 Jacoco 需要这样的选择。--add-opens
0赞 David M. Karr 11/14/2023
首先确定问题出在 Jacoco 还是 SonarQube 上。您应该在“target”中有一个目录,用于写入 Jacoco 输出文件。如果 Jacoco 运行,应该有一个“index.html”文件。在浏览器中打开该文件。检查结果。如果它们看起来太低(或为零),那么问题出在您的 Jacoco 配置中。如果它们没问题,那么 SonarQube 不会获取 Jacoco 数据。

答: 暂无答案