How to push code coverage from Azure Devops to SonarQube for Maven

ยท

2 min read

Introduction

  • In this blog we are going to discuss, how to generate the code coverage from unit test and publish to sonarqube.

Setup used:-

  • Azure Devops Pipelines

  • Maven Project

  • SonarQube v8.9-community

Step 1: Generate test case in Azure Devops

In azure pipeline there is task called Maven v3, we are using as build for our project.

- task: Maven@3
  displayName: 'Maven Package Build'
  inputs:
    mavenPomFile: $(pomFile)
    mavenOptions: '-Xmx3072m'
    javaHomeOption: 'JDKVersion'
    jdkVersionOption: '1.11'
    jdkArchitectureOption: 'x64'
    mavenAuthenticateFeed: true
    effectivePomSkip: true
    publishJUnitResults: true
    codeCoverageToolOption: JaCoCo
    testResultsFiles: '**/surefire-reports/TEST-*.xml'
    goals: 'package'

As you can see, we simply put publishJUnitResults: true means we are publishing our junit result test cases result on board.

Eg - Below image you can see in Tests tab 2 test cases was passed.

Step 2 - Generate the Code Coverage report using jacoco( as sonarqube support this tool )

For that we can also publish the code coverage separately using PublishCodeCoverageTask

But in my case using into maven build task only ๐Ÿ˜๐Ÿ˜๐Ÿ˜๐Ÿ˜

- task: Maven@3
  displayName: 'Maven Package Build'
  inputs:
    mavenPomFile: $(pomFile)
    mavenOptions: '-Xmx3072m'
    javaHomeOption: 'JDKVersion'
    jdkVersionOption: '1.11'
    jdkArchitectureOption: 'x64'
    mavenAuthenticateFeed: true
    effectivePomSkip: true
    publishJUnitResults: true
    codeCoverageToolOption: JaCoCo
    testResultsFiles: '**/surefire-reports/TEST-*.xml'
    goals: 'package'

In this we simple use codeCoverageToolOption: JaCoCo to publish code coverage.

Once you triggered the pipeline or ci build if you check the maven task while build you see it publish both test and code coverage.

And You can also check on board as well once build got succeed.

You can also see detailed report under Code Coverage tab one ADO.

Step 3 - Pushed this code coverage report to SonarQube

As we used ADO so there is task define for SonarQube

  • SonarQube Prepare

  • SonaruQube Scan

  • SonarQube Publish

- task: SonarQubePrepare@5
  inputs:
    SonarQube: '<SonarQube Service Conn>'
    scannerMode: 'Other'
    extraProperties: |
      # Additional properties that will be passed to the scanner, 
      # Put one key=value per line, example:
      # sonar.exclusions=**/*.bin
      sonar.projectKey=$(sonarKey)
      sonar.coverage.jacoco.xmlReportPaths=<path to jacoco.xml file which is generated by code coverage in maven build task>

- task: Maven@4
  displayName: 'SonarQube'
  inputs:
    mavenPomFile: $(pomFile)
    publishJUnitResults: true
    testResultsFiles: '**/surefire-reports/TEST-*.xml'
    javaHomeOption: 'JDKVersion'
    mavenVersionOption: 'Default'
    mavenAuthenticateFeed: true
    effectivePomSkip: true
    sonarQubeRunAnalysis: true
    sqMavenPluginVersionChoice: 'latest'

- task: SonarQubePublish@5
  inputs:
    pollingTimeoutSec: '300'

To publish just need to add extra properties in SonarPrepare Task sonar.coverage.jacoco.xmlReportPaths= .

Remember to Run the maven build before the SonarQube Scan Task i.e Maven v4

  • SonarPrepare

  • Maven build v3

  • SonarScan Maven v4

  • SonarPublish

Once the run got succeeded it will reflect over SonarQube

That's it for today See you in next blog ๐Ÿ˜Š๐Ÿ˜Š๐Ÿ˜Š๐Ÿ˜Š

ย