Passing Java 11 to SonarQube using Gradle with Java 8

Now: SonarQube 8.0
Was: SonarQube 6.4
Java: All code compiling using Java 8

Error: SonarScanner will require Java 11+ to run starting in SonarQube 8.x

Question: How do I pass Java 11 location to SonarQube on the Gradle command line and not modify JAVA_HOME. i.e. is there a (sonar.) property available for this…?

3 Likes

Hi David,

Did you have any luck with this? I’m running into a similar issue working with a project in Java 7 while Sonar is looking for Java 8. Is it possible to provide the path for Java 8 in the gradle.properties or build.gradle files?

Thanks!
Lee

Also wondering if there was any discovery around this.

I tried to run the build with JDK8 first and separately run the task sonarqube with JDK11. But unfortunately then gradle wants to recompile.
And our source is not compaitble with JDK11 yet (depending on tools.jar).

Does any working solution for Java 8 code exist with gradle yet?

hi @milbrandt , sorry i do not have a solution for you … i just have read the following documentation some hours ago and now am wondering:

the following procedure (changing the JAVA_HOME to jdk11 in between the “clean build” and the “sonarqube” tasks) does not work for you as gradle tries to recompile in the sonarsource task?

image

I soon, too, need to find a way to combine jdk8 and jdk11 with gradle. So if this doesn’t work i will also be bitten … interesting :thinking: :nerd_face:

In our build pipeline I used the Azure DevOps task for Gradle.

  • For build passing the argument jdkDirectory for JDK 8, and
  • for SonarQube analysis the installed OpenJDK 11 (sqAnalysisEnabled: true and sqGradlePluginVersion: '3.1.1')

Haven’t investigated further since February as SonarQube analysis is still running with JDK 8 and using only one Gradle task is always better then 2 :wink:

The approach described in the documentation doesn’t work since running the sonarqube task also executes the JavaCompile tasks.

I came across the same problem in our build pipelines. It appears to be a bug - SonarScanner for Gradle adds dependencies on JavaCompile tasks

1 Like

I found a good solution, so I thought I’d share:

Turns out you can

  • Switch the JDK to Java 11
  • But compile to a Java 8-level
java {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

This allows

  • sonar to work correctly (because it needs Java 11)
  • me to put off upgrading my main app to Java 11 for now

With the newer version of Sonar, I also had to set a few other properties (see this guide):

jacoco {
    // We need to use a newer version of jacoco to be compatible with the Java 11+ JDK that's required for the SonarQube Analysis

    toolVersion = '0.8.8'
}

jacocoTestReport {
    // The newer version of SonarQube uses the xml report instead of the previously used exec report.

    reports {
        xml.enabled true
    }
}

plugins.withType(JacocoPlugin) {
    // This ensures that the jacocoTestReport task runs after the test tasks; the sonar analysis needs the test report.
    tasks['test'].finalizedBy 'jacocoTestReport'
}
1 Like