如何成功更改 Spring Boot 项目中的 Java 版本?

aCa*_*lla 4 java compatibility spring gradle spring-boot

我最近使用 IntelliJ 启动了一个新的 Spring Boot Gradle 项目。在制作项目时,我选择了Java 17作为项目Java版本。我的build.gradle文件有sourceCompatibility = '17',但我的JAVA_HOME环境变量设置为 Java 11 并且我真的不想使用 Java 17。

我尝试更改sourceCompatibility11,然后进入

文件 -> 项目结构 -> 项目设置 -> 项目

将 SDK 也更改为 11。

每当我单击 IntelliJ 中的“运行”按钮时,以及当我尝试在命令行中使用 构建项目时./gradlew clean build,我都会收到一堆错误,主要是抱怨兼容性问题。他们的读法几乎都一样;这是失败构建的错误的开始:

   > Could not resolve org.springframework.boot:spring-boot-gradle-plugin:3.0.1-SNAPSHOT.
     Required by:
         project : > org.springframework.boot:org.springframework.boot.gradle.plugin:3.0.1-SNAPSHOT:20221222.181044-39
      > No matching variant of org.springframework.boot:spring-boot-gradle-plugin:3.0.1-SNAPSHOT:20221222.181044-39 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-SNAPSHOT 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-SNAPSHOT declares a runtime of a component, and its dependencies declared externally:
Run Code Online (Sandbox Code Playgroud)

我不想只是为了解决这个兼容性问题而重新开始我的项目。我不想在 Java 版本之间切换(我了解 jEnv,只是更愿意暂时将计算机上的所有内容设置为 Java 11)。有没有办法修复这个项目而不废弃它并重做它?

小智 13

Ctrl+ Alt+ S> 构建、执行、部署 > Gradle > Gradle JVM -> java 11 photo x


Bas*_*que 8

需要 Java 17

Spring Boot 3 需要 Java 17 或更高版本。

请参阅Spring 博客页面,准备 Spring Boot 3.0。去引用:

Spring Boot 3.0 将需要 Java 17

另请注意,Spring Boot 3 是一项重大更新,其中的更改破坏了向后兼容性。这些更改包括包名称从 更改javax.*jakarta.*


你说:

我真的不想使用 Java 17

我无法想象为什么您会选择 Java 11 而不是 Java 17。

大多数在 Java 11 上运行的 Java 应用程序或库都应该在 Java 17 上运行。我不记得 Java 8 到 9 到 11 之间有任何有问题的更改。

Java 11 和 Java 17 都是长期支持 (LTS)版本。