1

I'm trying to run analysis on my code using jacoco plugin in gradle and sonarqube. My tests are written in Groovy. I've automated the process using bitbucket pipelines, so every time I commit code, the tests are run, and the jacoco analysis and sonarqube are run in the end, pushing the report results in SonarCloud. Coverage is shown (as a percentage, with a link to classes) and compared against dev branch (I explicitly specified dev as a long-lived branch in SonarCloud). Also, overall coverage is shown (after merge).

The problem is, when I merge dev into my branch (something else gets merged to dev, so I sync), then, the branch coverage is shown as "-", i.e. empty. I can't seem to find the problem, rather than guessing that the commit (that comes from merging dev into my branch), has 2 parents (the previous commit and another short-lived branch which was merged in dev), and somehow it gets confused. After I commit something, even a silly line of code, then the analysis is shown yet again, correctly.

I'd like to know if anybody has solved this issue, or knows why it happens. Thanks!

In build.gradle I've added:

 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
    }
}

and my bitbucket pipelines file is:

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
gfe
  • 31
  • 8

1 Answers1

2

In build.gradle, it's enough to specify jacoco and jacocoTestReport properties like follows:

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"
    }
}

And then in bitbucket-pipelines.yml, do this:

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
gfe
  • 31
  • 8