Spring Boot 3.x 升级。无法解析 org.springframework.boot:spring-boot-gradle-plugin:3.0.1

Rom*_*n T 72 intellij-idea gradle spring-boot java-17

我最近想将我的 kotlin 项目从 spring boot 2.7.x 升级到 3.0.1。我使用 Java 17 temurin,gradle 7.6。在 IntelliJ 中,我在通过 gradle 导入项目时收到以下错误消息:

A problem occurred configuring root project 'demo'.
> 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')
Run Code Online (Sandbox Code Playgroud)

我还尝试从 start.spring.io 下载类似的项目框架。使用以下 build.gradle.kts:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "3.0.1"
    id("io.spring.dependency-management") version "1.1.0"
    kotlin("jvm") version "1.7.22"
    kotlin("plugin.spring") version "1.7.22"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "17"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}
Run Code Online (Sandbox Code Playgroud)

但导入项目时的错误消息仍然相同。有任何想法吗?

小智 191

我发现您正在使用 IntelliJ IDEA 来实现此目的。这对我有用:转到设置 --> 构建、执行、部署 --> 构建工具 --> Gradle。单击“Gradle 项目”下的 gradle 项目。为项目选择您的 Gradle JVM...在我的例子中是 openjdk-19。现在应该可以了。

  • 一种“不同的方式来做到这一点”。(如果这个答案小锤子不起作用,则用更大的锤子)如果(或确保)您的 JAVA_HOME 指向“正确”的 JDK(原始问题的 JDK17)。关闭 IntelliJ(所有实例)。删除“.idea”文件夹。在 IntelliJ 中重新加载项目。这将“重新设计”.idea 文件夹以指向 JDK17。(请注意,这个答案基本上是更改 .idea 文件夹中存储的内容中的一项)。再次强调,首先尝试这个答案。但这个评论是作为一个更大的锤子提供的。 (5认同)
  • 在ChatGPT时代,这就是这个问题的最佳答案。 (4认同)

小智 25

Spring Boot3.0需要 Java 17。阅读此博客了解更多信息。

build.bradle文件应包含:

  1. 在“ plugins”部分下:org.springframework.boot带版本。3.x.x;
  2. 在“ Java”部分下:“ sourceCompatibility”作为版本“ 17”;
  3. File-> Settings-> Build, Execution, Deployment-> Gradle-> Gradle JVM: 版本。17

或者

  1. 在“ plugins”部分下:org.springframework.boot带版本。2.x.x;
  2. 在“ Java”部分下:“ sourceCompatibility”作为版本“ 11”;
  3. File-> Settings-> Build, Execution, Deployment-> Gradle-> Gradle JVM: 版本。11