Using sonarqube-gradle-plugin with JDK 8 matrix in GitHub Actions

TLDR: How do I configure sonarqube-gradle-plugin with GitHub Actions and actions@setup-java set to JDK 8 for project’s compatibility testing/building?


When testing in GitHub actions with java matrix set to 8, 11, 17, 19.
All runs just fine except the java 8 runner which fails with:

...
No matching variant of org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:4.2.1.3168 was found. The consumer was configured to find a library for use during compile-time, compatible with Java  8.
      ...
      >  Incompatible because this component declares a component, compatible with Java 11 and the consumer needed a component, compatible with Java 8

This is understandable since the plugin requires Java 11+, running:

./gradlew test

Will return the error shown above.

Running any ./gradlew command with CI’s job matrix running with JDK 8 will return an error since it can’t resolve sonarqube-gradle-plugin due to incompatibility.

Is there a way to circumvent this? Or did I just set it up wrong? (most likely)

Things I've tried...
  • Moving sonarqube plugin & configuration in root build.gradle.kts - subprojects {}

  • Multiple JDK installation in setup-java action, used JDK 11 for building the project first (hopefully getting the dependencies first), then use JDK 8 to test (./gradlew test)

    - name: Test with JDK 8
      run: |
        JAVA_HOME=${{ env.JAVA_HOME_11_X64 }} ./gradlew build
        JAVA_HOME=${{ env.JAVA_HOME_8_X64 }} ./gradlew test --stacktrace
    

Sample related files:

.github/workflows/test.yml
jobs:
  unit:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        java: [ 8, 11, 17, 19 ]
        os: [ ... ]
    steps:
      - uses: actions/checkout@v3

      - name: Set up JDK ${{ matrix.java }}
        uses: actions/setup-java@v3
        with:
          java-version: ${{ matrix.java }}
          distribution: temurin

      - name: Test with JDK ${{ matrix.java }}
        run: ./gradlew clean test --stacktrace
build-logic/src/main/kotlin/sonarqube-sample.gradle.kts
plugins {
    id("org.sonarqube") // version is defined in version catalog (libs)
   ...
}

sonarqube {
    properties {
        val projectKey = "${rootProject.property("GROUP")}:${project.property("ARTIFACT_ID")}"

        // project metadata
        property("sonar.projectKey", projectKey)
        property("sonar.organization", rootProject.property("sonar.organization").toString())
        property("sonar.host.url", rootProject.property("sonar.host.url").toString())
        property("sonar.projectDescription", project.property("POM_DESCRIPTION").toString())
        property("sonar.projectVersion", project.property("VERSION").toString())

        // project reports
        property(
            "sonar.coverage.jacoco.xmlReportPaths",
            "${project.projectDir}/build/reports/jacoco/test/jacocoTestReport.xml"
        )
        property(
            "sonar.kotlin.detekt.reportPaths",
            "${project.projectDir}/build/reports/detekt/detekt.xml"
        )
    }
}

...
build-logic/build.gradle.kts
plugins {
    `kotlin-dsl`
}

repositories {
    gradlePluginPortal()
    mavenCentral()
}

dependencies {
    ...
    implementation(libs.sonarqube)
}

Hi,

Are you asking specifically how to configure your GH Actions script? I.e. what syntax? Or are you asking for strategy? Because the latter is easier for me. :smile:

Your options are

  • operate exclusively with Java 11, but compile to Java 8. The first search result gives the syntax for that

  • compile with Java 8, then analyze with Java 11, explicitly excluding the compile step. The sonar task depends on compile, so it will invoke it by default if you don’t.

 
HTH,
Ann

1 Like

The first option works (kind of) for me

I went with a strategy similar to: dokka/.github/workflows/tests-thorough.yml at master · Kotlin/dokka · GitHub which configures setup-java to 17, and adjust the JDK toolchain from gradle itself.

Thanks.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.