Java Code Coverage Reporting in Azure Devops Pipline since Update Pipeline Task

We are experiencing an issue where the code coverage from unit tests in a Java code base is not registered when using Sonar in an Azure pipeline. We are aware that version 1 of the SonarCloud pipeline tasks has recently been deprecated and have tried to update them to version 2. However, in the pipeline for Java code, upgrading to version 2 seems to not be working in combination with the Maven pipeline task (version 4), as code coverage is still not reported in SonarCloud.

What could be the cause?

Hey there.

Probably a great place to start would be to provide the logs from a run with the v1 tasks (where coverage is reported), compared to a run with v2 (where the coverage isn’t reported).

Hi Colin,

Thanks for responding.

When using version 1 of the Prepare Analysis Configuration task for SonarCloud, we see the following in the logs of the Maven task:

2024-06-17T13:23:48.2962445Z ##[section]Starting: produce Jacoco XML and push to SonarCloud
2024-06-17T13:23:48.3010249Z ==============================================================================
2024-06-17T13:23:48.3010395Z Task         : Maven
2024-06-17T13:23:48.3010476Z Description  : Build, test, and deploy with Apache Maven
2024-06-17T13:23:48.3010574Z Version      : 4.235.1
2024-06-17T13:23:48.3010636Z Author       : Microsoft Corporation
2024-06-17T13:23:48.3010730Z Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/build/maven
2024-06-17T13:23:48.3010841Z ==============================================================================
2024-06-17T13:23:49.4014871Z [command]/usr/bin/mvn -version
2024-06-17T13:23:49.6876650Z Apache Maven 3.8.8 (4c87b05d9aedce574290d1acc98575ed5eb6cd39)
2024-06-17T13:23:49.6877192Z Maven home: /usr/share/apache-maven-3.8.8
2024-06-17T13:23:49.6877725Z Java version: 17.0.11, vendor: Eclipse Adoptium, runtime: /usr/lib/jvm/temurin-17-jdk-amd64
2024-06-17T13:23:49.6877952Z Default locale: en, platform encoding: UTF-8
2024-06-17T13:23:49.6878387Z OS name: "linux", version: "5.15.0-1064-azure", arch: "amd64", family: "unix"
2024-06-17T13:23:49.6970767Z 
2024-06-17T13:23:49.9485856Z ##[warning]The settings for the feed or repository already exists in the xml file.
2024-06-17T13:23:49.9642035Z [command]/usr/bin/mvn -f /home/vsts/work/1/s/pom.xml -s /home/vsts/work/_temp/settings.xml jacoco:report org.sonarsource.scanner.maven:sonar-maven-plugin:RELEASE:sonar
2024-06-17T13:23:51.7432686Z [INFO] Scanning for projects...
2024-06-17T13:23:53.3727741Z [INFO] ------------------------------------------------------------------------
2024-06-17T13:23:53.3734539Z [INFO] Reactor Build Order:
2024-06-17T13:23:53.3738974Z [INFO] 
2024-06-17T13:23:53.3775958Z [INFO] FOO Root: Parent                                                  [pom]
2024-06-17T13:23:53.3783165Z [INFO] FOO Dependencies                                                  [pom]
2024-06-17T13:23:53.3787146Z [INFO] Commons                                                            [pom]
. . .
'sonar.coverage.jacoco.xmlReportPaths' is not defined. Using default locations: target/site/jacoco/jacoco.xml,target/site/jacoco-it/jacoco.xml,build/reports/jacoco/test/jacocoTestReport.xml
2024-06-17T13:24:18.9334665Z [INFO] 13:24:18.933 Importing 1 report(s). Turn your logs in debug mode in order to see the exhaustive list.

When using version 2, we see the following:

2024-06-24T15:12:49.1588058Z ##[section]Starting: produce Jacoco XML and push to SonarCloud
2024-06-24T15:12:49.1598562Z ==============================================================================
2024-06-24T15:12:49.1598680Z Task         : Maven
2024-06-24T15:12:49.1598747Z Description  : Build, test, and deploy with Apache Maven
2024-06-24T15:12:49.1598832Z Version      : 4.235.1
2024-06-24T15:12:49.1598889Z Author       : Microsoft Corporation
2024-06-24T15:12:49.1598963Z Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/build/maven
2024-06-24T15:12:49.1599059Z ==============================================================================
2024-06-24T15:12:50.0141906Z [command]/usr/bin/mvn -version
2024-06-24T15:12:50.1716570Z Apache Maven 3.8.8 (4c87b05d9aedce574290d1acc98575ed5eb6cd39)
2024-06-24T15:12:50.1717445Z Maven home: /usr/share/apache-maven-3.8.8
2024-06-24T15:12:50.1718222Z Java version: 17.0.11, vendor: Eclipse Adoptium, runtime: /usr/lib/jvm/temurin-17-jdk-amd64
2024-06-24T15:12:50.1718809Z Default locale: en, platform encoding: UTF-8
2024-06-24T15:12:50.1719519Z OS name: "linux", version: "5.15.0-1064-azure", arch: "amd64", family: "unix"
2024-06-24T15:12:50.1854524Z 
2024-06-24T15:12:50.4020347Z ##[warning]The settings for the feed or repository already exists in the xml file.
2024-06-24T15:12:50.4113620Z [command]/usr/bin/mvn -f /home/vsts/work/1/s/pom.xml -s /home/vsts/work/_temp/settings.xml jacoco:report org.sonarsource.scanner.maven:sonar-maven-plugin:RELEASE:sonar
2024-06-24T15:12:51.3881415Z [INFO] Scanning for projects...
2024-06-24T15:12:52.9432761Z [INFO] ------------------------------------------------------------------------
2024-06-24T15:12:52.9433240Z [INFO] Reactor Build Order:
2024-06-24T15:12:52.9433417Z [INFO] 
2024-06-24T15:12:52.9467698Z [INFO] INDI Root: Parent                                                  [pom]
2024-06-24T15:12:52.9468034Z [INFO] INDI Dependencies                                                  [pom]
2024-06-24T15:12:52.9473374Z [INFO] Commons                                                            [pom]
. . .
2024-06-24T15:13:14.5504346Z [INFO] 15:13:14.550 'sonar.coverage.jacoco.xmlReportPaths' is not defined. Using default locations: target/site/jacoco/jacoco.xml,target/site/jacoco-it/jacoco.xml,build/reports/jacoco/test/jacocoTestReport.xml
2024-06-24T15:13:14.5509343Z [INFO] 15:13:14.550 No report imported, no coverage information will be imported by JaCoCo XML Report Importer

We don’t have any logs from when v1 still worked.

Well, you can always switch back to v1 now to see if the behavior goes back to what you expected.

If not, then the problem is more likely from somewhere else in your build. Particularly, you’ll want to make sure your JaCoCo report is being generated at the expected location!

[INFO] --- jacoco:0.8.11:report (report) @ sonarscanner-maven-basic ---
[DEBUG] Loading mojo org.jacoco:jacoco-maven-plugin:0.8.11:report from plugin realm ClassRealm[plugin>org.jacoco:jacoco-maven-plugin:0.8.11, parent: jdk.internal.loader.ClassLoaders$AppClassLoader@1cf4f579]
[DEBUG] Configuring mojo execution 'org.jacoco:jacoco-maven-plugin:0.8.11:report:report' with basic configurator -->
[DEBUG]   (f) dataFile = /Users/colin/Source/sonar-scanning-examples/sonar-scanner-maven/maven-basic/target/jacoco.exec
[DEBUG]   (f) formats = [HTML, XML, CSV]
[DEBUG]   (f) outputDirectory = /Users/colin/Source/sonar-scanning-examples/sonar-scanner-maven/maven-basic/target/site/jacoco
[DEBUG]   (f) outputEncoding = UTF-8
[DEBUG]   (f) project = MavenProject: org.sonarqube:sonarscanner-maven-basic:1.0-SNAPSHOT @ /Users/colin/Source/sonar-scanning-examples/sonar-scanner-maven/maven-basic/pom.xml
[DEBUG]   (f) skip = false
[DEBUG]   (f) sourceEncoding = UTF-8
[DEBUG]   (f) title = Example of basic Maven project
[DEBUG] -- end configuration --
[INFO] Loading execution data file /Users/colin/Source/sonar-scanning-examples/sonar-scanner-maven/maven-basic/target/jacoco.exec
[INFO] Analyzed bundle 'Example of basic Maven project' with 2 classes

Nothing should have changed between these versions of the tasks with regards to importing the JaCoCo report. And remember – SonarCloud only reads in the JaCoCo report where it’s told to look for it (or at the default locations mentioned in the logs). It isn’t involved in generating it.