Gradle SonarCloud Integration with multiple JDK dependencies

May 12, 2024

SonarCloud requires JDK 17 however the project you want to scan may be using an older version of Java. Source

This reviews one approach to manage these dependencies.

SonarQube/SonarCloud deprecates JDK 11 runtime support

One of the JavaFX projects I was working on runs on JDK 11 and we wanted to do a SonarCloud scan on the codebase. When it came time to look into this, SonarQube was deprecating support for running on a JDK 11 runtime so it became a bit of a challenge to build the project using JDK 11 and then scan with SonarQube using JDK 17, the new minimum runtime version.

Using Gradle and SonarQube

SonarCloud provides a simple out of the box code samples when setting up a project to be scanned and results viewable on sonarcloud.io

gradle.build can be updated as such:

plugins {
  id "org.sonarqube" version "versionNumber" // Replace with latest scanner version number
}

sonar {
  properties {
    property "sonar.projectKey", "myProjectKey"
    property "sonar.organization", "myOrganization"
    property "sonar.host.url", "https://sonarcloud.io"
  }
}

Then, running the scan is simply:

gradle build sonar

Source

The challenge with only running this command where SonarQube requires JDK 17 and the project runs on JDK 11 is that the runtime could only be either one. To overcome this, we need a way to declare gradle sonar to run using JDK 17 and gradle build to run using another JDK version.

Using different JDK versions with Gradle

I eventually came across this Stackoverflow post that indicated you can declare the JDK you want to use with Gradle.

gradle -Dorg.gradle.java.home=/PATH_TO_JDK

Therefore we can run build and sonar sequentically and declare with JDK to use for each task.

gradle build -Dorg.gradle.java.home=/PATH_TO_JDK_11 
&& 
gradle sonar -Dorg.gradle.java.home=/PATH_TO_JDK_17

Doing this, it is possible to run the SonarQube scanner requiring JDK 17 on a project built on JDK 11.