Search code examples
spring-bootgradleintellij-ideajava-17

IntelliJ Gradle build failure: Incompatible because component compatible with Java 17 and the consumer needed Java 11


In IntelliJ, when I do a clean build on a new project downloaded from start.spring.io, it gives an error.

Spring Configuration on start.spring.io

I have already installed Java 17 JDK (Amazon Corretto) downloaded directly from IntelliJ

In terminal, ran command: ./gradlew clean build

resulted in the following error:

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'learn-spring-framework'.
> Could not resolve all files for configuration ':classpath'.
   > Could not resolve org.springframework.boot:spring-boot-gradle-plugin:3.0.1.
     Required by:
         project : > org.springframework.boot:org.springframework.boot.gradle.plugin:3.0.1
      > No matching variant of org.springframework.boot:spring-boot-gradle-plugin:3.0.1 was found. The consumer was configured to find a runtime of a library compatible with Java 11, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '7.6' but:
          - Variant 'apiElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.0.1 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares an API of a component compatible with Java 17 and the consumer needed a runtime of a component compatible with Java 11
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.6')
          - Variant 'javadocElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.0.1 declares a runtime of a component, 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 11)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.6')
          - Variant 'mavenOptionalApiElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.0.1 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares an API of a component compatible with Java 17 and the consumer needed a runtime of a component compatible with Java 11
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.6')
          - Variant 'mavenOptionalRuntimeElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.0.1 declares a runtime of a library, 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 11
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.6')
          - Variant 'runtimeElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.0.1 declares a runtime of a library, 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 11
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.6')
          - Variant 'sourcesElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.0.1 declares a runtime of a component, 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 11)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.6')

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s

Thanks in advance for the help

Tried:

  1. Make sure the Project is using Java 17 SDK under File -> Project Structure
  2. Make sure the Gradle JVM is pointing to Java 17 SDK under IntelliJ IDEA -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle
  3. Tried to change build.gradle file for
sourceCompatibility = (11 or 17)
targetCompatibility = (11 or 17)

java {
  toolchain {
    languageVersion = JavaLanguageVersion.of( (11 or 17) )
  }
} 

Weirdly, the project just builds fine if I just click on the "play" button on IntelliJ


Solution

  • Spring Boot 3 requires as minimum version JDK 17.

    If you want to configure InteliJ with gradle using JDK 17 or later the following configurations are needed.

    1. File -> Project Structure -> Project -> Project SDK -> specify a jdk of 17 or later

    enter image description here

    1. You also need the following configuration. File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle -> Gradle JVM and specify a jdk of 17 or later.

    enter image description here

    Keep in mind that the above configurations apply to the embedded version of gradle which InteliJ uses as an embedded tool. Not the one configured to run from Terminal

    In order to use the embedded tool version for gradle terminal which will already be set up with Jdk 17 according to the above configurations, you need to run commands as shown bellow:

    enter image description here

    This should be enough to execute your project.

    Otherwise If you insist on using the terminal to execute gradle commands, then you need to make all the necessary adaptations in environmental variables. To do so head into File -> Settings -> Tools -> Terminal -> Enviroment Variables . For environmental changes to take effect you have to restart InteliJ window. After restart you can confirm with gradle -version command if the correct JVM and gradle version is set up or not. Also you can use the java -version to see in what JDK the terminal points to. Spring Boot Initializer delivers also a portable gradle file which is compatible to run the project. So if you have under environmental properties another gradle installed in your system you can detect with above commands and remove so that you can use only the local one which is shipped.