Search code examples
javaspring-bootgradle

Gradle toolchain resolver plugin doesn't download jdk17 for a spring boot 3 project


It seems spring boot gradle plugin version 3.x is not working with the toolchain resolver plugin under jdk8. This simple build.gradle:

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.0.3'
    id 'io.spring.dependency-management' version '1.1.0'
}
repositories {
    mavenCentral()
}
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
}
test {
    useJUnitPlatform()
}
sourceCompatibility = 17
targetCompatibility = 17
java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

Along with this simple settings.gradle:

plugins {
    id 'org.gradle.toolchains.foojay-resolver-convention' version '0.4.0' 
}
rootProject.name = 'myAwesomeProject'

Fail with the following error when running a simple gradle build:

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'myAwesomeProject'.
> Could not resolve all files for configuration ':classpath'.
   > Could not resolve org.springframework.boot:spring-boot-gradle-plugin:3.0.3.
     Required by:
         project : > org.springframework.boot:org.springframework.boot.gradle.plugin:3.0.3
      > No matching variant of org.springframework.boot:spring-boot-gradle-plugin:3.0.3 was found. The consumer was configured to find a library for use during runtime, compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.0.1' but:
          - Variant 'apiElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.0.3 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component for use during compile-time, compatible with Java 17 and the consumer needed a component for use during runtime, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.0.1')
          - Variant 'javadocElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.0.3 declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.0.1')
          - Variant 'mavenOptionalApiElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.0.3 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component for use during compile-time, compatible with Java 17 and the consumer needed a component for use during runtime, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.0.1')
          - Variant 'mavenOptionalRuntimeElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.0.3 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.0.1')
          - Variant 'runtimeElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.0.3 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.0.1')
          - Variant 'sourcesElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.0.3 declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.0.1')

If I downgrade the spring boot version to 2.7.9 then jdk17 is downloaded and everything works as expected.

Am I missing something? I know spring boot 3.x requires jdk17, that's why I'm using the toolchain resolver.

There's a plugin called com.github.rmee.jdk-bootstrap that works, I just prefer using the toolchain due to its simplicity (like not having to deal with URLs).

If it's a bug, what plugin project should I open a bug ticket to?


Solution

  • Spring Boot's Gradle plugin requires Java 17. This means that Gradle itself needs to be run using Java 17 as plugin classes are loaded in Gradle's JVM. Toolchains only affect tasks that run in a separate JVM such as Java compilation and running tests.