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

如何解决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 任务之前添加了准备分析配置任务。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-