Passing Java 11 to SonarQube using Gradle with Java 8

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