IntelliJ Gradle 构建失败:不兼容,因为组件与 Java 17 兼容并且消费者需要 Java 11

Ton*_*hen 8 intellij-idea gradle spring-boot java-17

在 IntelliJ 中,当我对从 start.spring.io 下载的新项目进行干净构建时,会出现错误。

start.spring.io 上的 Spring 配置

我已经安装了直接从 IntelliJ 下载的 Java 17 JDK (Amazon Corretto)

在终端中,运行命令:./gradlew clean build

导致以下错误:

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

Run Code Online (Sandbox Code Playgroud)

先谢谢您的帮助

尝试过:

  1. 确保项目正在使用 Java 17 SDKFile -> Project Structure
  2. 确保 Gradle JVM 指向 Java 17 SDKIntelliJ IDEA -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle
  3. 尝试更改build.gradle文件
sourceCompatibility = (11 or 17)
targetCompatibility = (11 or 17)

java {
  toolchain {
    languageVersion = JavaLanguageVersion.of( (11 or 17) )
  }
} 
Run Code Online (Sandbox Code Playgroud)

奇怪的是,如果我只需单击 IntelliJ 上的“播放”按钮,该项目就可以正常构建

Pan*_*kos 17

Spring Boot 3需要最低版本JDK 17

如果您想使用 gradleJDK 17或更高版本配置 InteliJ,则需要以下配置。

  1. 文件 -> 项目结构 -> 项目 -> 项目 SDK -> 指定 17 或更高版本的 jdk

在此输入图像描述

  1. 您还需要进行以下配置。文件 -> 设置 -> 构建、执行、部署 -> 构建工具 -> Gradle -> Gradle JVM并指定 17 或更高版本的 jdk。

在此输入图像描述

请记住,上述配置适用于gradle用作InteliJ嵌入式工具的嵌入式版本。不是配置为运行的那个Terminal

为了使用gradle已经根据上述配置设置的终端嵌入式工具版本Jdk 17,您需要运行如下命令:

在此输入图像描述

这应该足以执行您的项目。

否则如果你坚持使用终端执行gradle命令,那么你需要对环境变量进行所有必要的调整。为此,请进入File -> Settings -> Tools -> Terminal -> Enviroment Variables。要使环境更改生效,您必须重新启动 InteliJ 窗口。重启后可以用gradle -version命令确认是否设置了正确的版本JVMgradle您还可以使用java -version查看终端指向的 JDK。Spring Boot初始化程序还提供一个gradle与运行项目兼容的可移植文件。因此,如果您的系统中安装了环境属性下的另一个 gradle,您可以使用上述命令进行检测并删除,以便您只能使用已发布的本地版本。