Azure DevOps 构建后,SonarCloud 中未显示代码覆盖率

问题描述

我们有一个多模块 Maven 项目,并使用 JaCoCo 进行代码覆盖率分析。我准备了 pom 文件来运行测试用例并获取代码覆盖率。

下面是我的 pom.xml添加了 JaCoCo 和其他插件,并在 Azure Devops 构建中传递了命令 clean install sonar:sonar,在 Maven 任务中尝试了 clean test sonar:sonar,参数为 -Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco-utpom.xml 中定义。

我在日志中看到以下详细信息。

Maven 日志:

Maven logs

分析完成日志:

Analysis completion logs

测试运行:

Test runs

生成 Jacoco 报告:

Generating Jacoco report

我查看了各种博客和帖子,尝试了很多方法,但在 Sonar 中看不到代码覆盖率。

    <plugins>
        <!--Jacoco Maven Plugin -->
        <plugin>
           <groupId>org.jacoco</groupId>
           <artifactId>jacoco-maven-plugin</artifactId>
           <version>0.8.7-SNAPSHOT</version>
           <executions>
        <!--
        Prepares the property pointing to the JaCoCo runtime agent which
        is passed as VM argument when Maven the Surefire plugin is executed.
        -->
            <execution>
                <id>pre-unit-test</id>
               <goals>
                    <goal>prepare-agent</goal>
              </goals>
              <configuration>
            <!-- Sets the path to the file which contains the execution data. -->
                <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
            <!--
                Sets the name of the property containing the settings
                for JaCoCo runtime agent.
            -->
                <propertyName>surefireArgLine</propertyName>
            </configuration>
          </execution>
    <!--
        Ensures that the code coverage report for unit tests is created after
        unit tests have been run.
    -->
           <execution>
               <id>post-unit-test</id>
                <phase>test</phase>
                <goals>
                  <goal>report</goal>
                  </goals>
              <configuration>
            <!-- Sets the path to the file which contains the execution data. -->
                   <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
            <!-- Sets the output directory for the code coverage report. -->
                   <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
              </configuration>
            </execution>
      </executions>
  </plugin>
         
         <!-- Maven ant plugin -->
        <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-antrun-plugin</artifactId>
           <version>3.0.0</version>
        </plugin>
        
        <!-- Maven Surefire Plugin -->
        <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-surefire-plugin</artifactId>
             <version>3.0.0-M5</version>
             <configuration>
    <!-- Sets the VM argument line used when unit tests are run. -->
               <argLine>${surefireArgLine}</argLine>
    <!-- Skips unit tests if the value of skip.unit.tests property is true -->
               <skipTests>${skip.unit.tests}</skipTests>
    <!-- Excludes integration tests when unit tests are run. -->
                <excludes>
               <exclude>**/IT*.java</exclude>
                </excludes>
               </configuration>
        </plugin>
        
        <!-- Sonar Maven Plugin -->
        <plugin>
           <groupId>org.sonarsource.scanner.maven</groupId>
           <artifactId>sonar-maven-plugin</artifactId>
           <version>3.7.0.1746</version>
        </plugin>

分享我的 Maven 任务 YAML 配置

  • 任务:Maven@3

    输入:

    mavenPomFile: 'pom.xml'

    目标:'清理验证声纳:声纳'

    选项:'-Dsonar.projectKey=** -Dsonar.organization=**

    -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=**

    -Dsonar.java.binaries=$(build.sourcesdirectory)/**/src/test/java/

    -Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco-aggregate/jacoco.xml

    -Dsonar.exclusions=/jaxb/**/*'

    publishJUnitResults: 假

    codeCoveragetoolOption: 'JaCoCo'

    codeCoverageClassFilesDirectories: 'core/target/classes,/core/target/test-classes'

    codeCoverageFailIfEmpty: 真

    javaHomeOption: '路径'

    jdk目录:'/opt/jdk-11.0.2'

    mavenVersionoption: '路径'

    mavendirectory: '/usr/local/apache-maven/apache-maven-3.6.3/'

    mavenSetM2Home:假

    Maven 选项:'-Xmx3072m'

    mavenAuthenticateFeed: 假

    effectivePomSkip: 假

    sonarQubeRunAnalysis:真

    isJacocoCoverageReportXML: true

    sqMavenPluginVersionChoice: 'pom'

经过一些更改后,我可以看到 Jacoco 报告被拾取,但无法看到代码覆盖率

传感器 JaCoCo XML 报告导入器 [jacoco]

[INFO] 正在导入 1 份报告。将您的日志设置为调试模式以查看详尽列表。

更新日志

主要: [echo] 生成 JaCoCo 报告

[report] 加载执行数据文件 /home/AzureUser/adoagent/_work/2/s/CCReport43F6D5EF/jacoco.exec

[report] 使用 338 个类编写包“Jacoco 报告”

[INFO] 已执行的任务

[信息]-------------------------------------------- ----------------------------- [信息] 构建成功

[信息]-------------------------------------------- ----------------------------- [信息] 总时间:4.743 秒

解决方法

请检查您是否已按照以下步骤配置 Azure Pipeline:

  1. 在您的组织中的 Azure DevOps 上安装扩展 SonarCloud
  2. 添加新的 SonarCloud 服务端点。
  3. 在构建步骤之前添加“准备分析配置”任务。
  4. 在构建步骤之后添加“运行代码分析”任务。
  5. 添加“发布质量门控结果”任务。

enter image description here

如果问题仍然存在,为了让我们进一步调查此问题,请分享一个示例来展示您的 Azure Pipeline 的详细配置。您可以将管道定义导出为 YAML 文件,不要忘记在 YAML 文件中屏蔽或隐藏您的私人信息。

[更新]

我注意到有关 Maven task 的文档中关于“运行 SonarQube 或 SonarCloud 分析”(sqAnalysisEnabled) 的描述:

此选项已从 Maven 任务的版本 1 更改为使用 SonarQubeSonarCloud 市场扩展。启用此选项以在执行 Goals 字段中的目标后运行 SonarQube or SonarCloud analysisinstallpackage 目标应该首先运行。您还必须在此 Maven 任务之前将一个准备分析配置任务从扩展之一添加到构建管道。 enter image description here

因此,正如我上面提到的,请确保您在管道中的 Maven 任务之前添加了准备分析配置任务。