在 IntelliJ 中,当我对从 start.spring.io 下载的新项目进行干净构建时,出现错误。
Spring Configuration on start.spring.io
我已经安装了直接从 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
在此先感谢您的帮助
尝试过:
- 确保项目在
File -> Project Structure
下使用 Java 17 SDK - 确保 Gradle JVM 指向
IntelliJ IDEA -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle
下的 Java 17 SDK - 试图改变
build.gradle
文件
sourceCompatibility = (11 or 17)
targetCompatibility = (11 or 17)
java {
toolchain {
languageVersion = JavaLanguageVersion.of( (11 or 17) )
}
}
奇怪的是,如果我只点击 IntelliJ 上的“播放”按钮,项目就可以正常构建
最佳答案
Spring Boot 3
要求最低版本 JDK 17
。
如果你想使用 JDK 17
或更高版本配置带有 gradle 的 InteliJ,则需要以下配置。
- File -> Project Structure -> Project -> Project SDK -> 指定jdk 17以上
- 您还需要以下配置。 文件 -> 设置 -> 构建、执行、部署 -> 构建工具 -> Gradle -> Gradle JVM 并指定 jdk 为 17 或更高版本。
请记住,上述配置适用于 gradle
的嵌入式版本,InteliJ
将其用作嵌入式工具。 不是配置为从终端
运行的那个
为了使用gradle
终端的嵌入式工具版本,根据上述配置已经设置了Jdk 17
,您需要运行如下所示的命令波纹管:
这应该足以执行您的项目。
否则 如果您坚持使用终端执行gradle命令,那么您需要在环境变量中进行所有必要的适配。为此,请进入 File -> Settings -> Tools -> Terminal -> Enviroment Variables 。要使环境更改生效,您必须重新启动 InteliJ 窗口。重新启动后,您可以使用 gradle -version
命令确认是否设置了正确的 JVM
和 gradle
版本。您也可以使用 java -version
查看终端指向的 JDK。 Spring Boot
Initializer 还提供了一个可移植的 gradle
文件,该文件兼容运行项目。因此,如果您在环境属性下在您的系统中安装了另一个 gradle,您可以使用上述命令检测并删除,以便您只能使用已发货的本地版本。
关于spring-boot - IntelliJ Gradle 构建失败 : Incompatible because component compatible with Java 17 and the consumer needed Java 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75035852/