首页 > 解决方案 > 与主分支同步时,Sonarcloud 中不显示分支覆盖率

问题描述

我正在尝试使用gradlesonarqube中的jacoco插件对我的代码进行分析。我的测试是用Groovy编写的。我已经使用 bitbucket 管道自动化了这个过程,所以每次我提交代码时,都会运行测试,最后运行 jacoco 分析和 sonarqube,将报告结果推送到 SonarCloud。显示覆盖率(以百分比形式,带有指向类的链接)并与 dev 分支进行比较(我明确地将 dev 指定为 SonarCloud 中的长期分支)。此外,还显示了整体覆盖范围(合并后)。

问题是,当我将 dev 合并到我的分支时(其他东西被合并到 dev,所以我同步),然后,分支覆盖显示为“-”,即为空。我似乎找不到问题,而不是猜测提交(来自将 dev 合并到我的分支中),有 2 个父项(上一个提交和另一个合并到 dev 中的短期分支),并且不知何故变得困惑。在我提交了一些东西之后,即使是一段愚蠢的代码,分析就会再次正确地显示出来。

我想知道是否有人解决了这个问题,或者知道它为什么会发生。谢谢!

在 build.gradle 我添加了:

 plugins {
    id "org.springframework.boot" version "2.0.2.RELEASE"
    id "org.sonarqube" version "2.7.1"
    id "application"
    id "jacoco"
}

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'jacoco'

jacoco {
    toolVersion = "0.8.3"
    reportsDir = file("$buildDir/customJacocoReportDir")
}

jacocoTestReport {
    reports {
        xml.enabled false
        csv.enabled false
        html.destination file("$buildDir/jacocoHtml")
    }
}

sonarqube {
   properties {
        // properties related to credentials
    }
}

我的 bitbucket 管道文件是:

image: java:8

clone:
  depth: full  # SonarCloud scanner needs the full history to assign issues properly

definitions:
  caches:
    sonar: ~/.sonar/cache  # Caching SonarCloud artifacts will speed up your build
  steps:
    - step: &build-test-sonarcloud
        name: Build, test and analyze on SonarCloud
        caches:
          - gradle
          - sonar
        script:
          - ./gradlew clean test
          - ./gradlew jacocoTestReport
          - ./gradlew sonarqube
        artifacts:
          - build/libs/**

pipelines:
  default:
    - step: *build-test-sonarcloud
  pull-requests:
    '**':
      - step: *build-test-sonarcloud

标签: javagradlesonarqubejacocosonarcloud

解决方案


在 build.gradle 中,指定 jacoco 和 jacocoTestReport 属性就足够了,如下所示:

jacoco {
    toolVersion = "0.8.3"
    reportsDir = file("$buildDir/customJacocoReportDir")
}

jacocoTestReport {
    reports {
        xml.enabled true
        html.enabled true
        csv.enabled false
        html.destination file("$buildDir/customJacocoReportDir/test/html")
        xml.destination file("$buildDir/customJacocoReportDir/test/jacocoTestReport.xml")
    }
}

sonarqube {
    properties {
        // define your properties
        property "sonar.jacoco.reportPath", "$buildDir/jacoco/test.exec"
        property "sonar.coverage.jacoco.xmlReportPaths", "$buildDir/customJacocoReportDir/test/jacocoTestReport.xml"
    }
}

然后在 bitbucket-pipelines.yml 中,执行以下操作:

image: java:8

clone:
  depth: full  # SonarCloud scanner needs the full history to assign issues properly

definitions:
  caches:
    sonar: ~/.sonar/cache  # Caching SonarCloud artifacts will speed up your build
  steps:
    - step: &build-test-sonarcloud
        name: Build, test and analyze on SonarCloud
        caches:
          - gradle
          - sonar
        script:
          - ./gradlew clean test
          - ./gradlew jacocoTestReport
          - ./gradlew sonarqube
        artifacts:
          - build/libs/**

pipelines:
  default:
    - step: *build-test-sonarcloud

推荐阅读