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

xytpbqjk  于 2023-01-08  发布在  Java
关注(0)|答案(1)|浏览(1323)

在IntelliJ中,当我对从www.example.com下载的新项目进行干净的构建时start.spring.io,它给出了一个错误。
Spring Configuration on start.spring.io
我已经安装了直接从IntelliJ下载的Java 17 JDK(Amazon Corretto)
在终端中,运行命令:第一个月
导致以下错误:

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

先谢谢你的帮助
尝试:
1.确保项目在File -> Project Structure下使用Java 17 SDK
1.确保Gradle JVM指向IntelliJ IDEA -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle下的Java 17 SDK
1.尝试将build.gradle文件更改为

sourceCompatibility = (11 or 17)
targetCompatibility = (11 or 17)

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

奇怪的是,如果我点击IntelliJ上的“播放”按钮,这个项目就构建得很好

8ljdwjyq

8ljdwjyq1#

Spring Boot 3要求最低版本为JDK 17
如果您想使用JDK 17或更高版本配置InteliJ with gradle,则需要以下配置。
1.文件-〉项目结构-〉项目-〉项目SDK-〉指定jdk为17或更高版本

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

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

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

这应该足够执行您的项目。
否则如果您坚持使用终端执行gradle命令,然后你需要在环境变量中做所有必要的调整。2要做这些,请进入文件-〉设置-〉工具-〉终端-〉环境变量。3要使环境改变生效,你必须重新启动InteliJ窗口。重启后可以使用gradle -version命令确认是否设置了正确的JVMgradle版本,也可以使用java -version查看终端指向哪个JDK。Spring Boot Initializer还提供了一个可移植的gradle文件,该文件兼容于运行项目。因此,如果您在环境属性下有另一个gradle安装在您的系统中,您可以使用上述命令检测并删除,以便您可以只使用本地随附的一个。

相关问题