我只是尝试 spring boot,并尝试将 spring boot 项目导入到 Intellij 上并简单地运行它。尽管尝试了许多不同的解决方案,但每次构建都会失败。这是我收到的错误:
./gradlew build --warning-mode=all
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring root project 'adhdplanner'.
> Could not resolve all artifacts for configuration ':classpath'.
> Could not resolve org.springframework.boot:spring-boot-gradle-plugin:3.2.5.
Required by:
project : > org.springframework.boot:org.springframework.boot.gradle.plugin:3.2.5
> No matching variant of org.springframework.boot:spring-boot-gradle-plugin:3.2.5 was found. The consumer was configured to find a library for use during runtime, compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.7' but:
- Variant 'apiElements' declares a library, packaged as a jar, and its dependencies declared externally:
- Incompatible because this component declares a component for use during compile-time, compatible with Java 17 and the consumer needed a component for use during runtime, compatible with Java 8
- Other compatible attribute:
- Doesn't say anything about org.gradle.plugin.api-version (required '8.7')
- Variant 'javadocElements' declares a component for use during runtime, 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 elements (required them packaged as a jar)
- Doesn't say anything about its target Java version (required compatibility with Java 8)
- Doesn't say anything about org.gradle.plugin.api-version (required '8.7')
- Variant 'mavenOptionalApiElements' declares a library, packaged as a jar, and its dependencies declared externally:
- Incompatible because this component declares a component for use during compile-time, compatible with Java 17 and the consumer needed a component for use during runtime, compatible with Java 8
- Other compatible attribute:
- Doesn't say anything about org.gradle.plugin.api-version (required '8.7')
- Variant 'mavenOptionalRuntimeElements' declares a library for use during runtime, 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 8
- Other compatible attribute:
- Doesn't say anything about org.gradle.plugin.api-version (required '8.7')
- Variant 'runtimeElements' declares a library for use during runtime, 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 8
- Other compatible attribute:
- Doesn't say anything about org.gradle.plugin.api-version (required '8.7')
- Variant 'sourcesElements' declares a component for use during runtime, 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 elements (required them packaged as a jar)
- Doesn't say anything about its target Java version (required compatibility with Java 8)
- Doesn't say anything about org.gradle.plugin.api-version (required '8.7')
* Try:
> Review the variant matching algorithm at https://docs.gradle.org/8.7/userguide/variant_attributes.html#sec:abm_algorithm.
> No matching variant errors are explained in more detail at https://docs.gradle.org/8.7/userguide/variant_model.html#sub:variant-no-match.
> 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.gradle。 sourceCompatibility 是灰色的,我添加了“buildscript”部分,更改了版本等:
buildscript{
ext{
springBootVersion = '3.2.5.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
plugins {
id 'java'
id 'idea'
id 'org.springframework.boot' version '3.2.5'
id 'io.spring.dependency-management' version '1.1.4'
}
group = 'org.example'
version = '1.0-SNAPSHOT'
sourceCompatibility = '1.6'
compileJava.options.fork = true
compileJava.options.forkOptions.executable = true
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
test {
useJUnitPlatform()
}
这是 gradle-wrapper.properties:
#Tue Apr 23 12:49:14 EDT 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
这是我的 Main():
package org.example;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
我不确定我是否提供了足够的信息,请告诉我。
Spring Boot 3.2.5 需要 Java 17 (docs),并且您正在尝试使用 Java 8 运行 Gradle。
我看到您已经安装了 Java 21。确保这是您的项目 SDK(如屏幕截图中的文件 | 项目结构,docs),并检查 Gradle 正在使用它(设置 | 构建、执行、部署 | 构建工具 | Gradle)( 文档)。
编辑:我看到你已经尝试过所有这些。也许您的 JAVA_HOME 环境变量正在覆盖它。
您可以按照配置 Java 或系统环境的方式设置 JAVA_HOME 环境变量,也可以在
gradle.properties
文件中覆盖它。
对于后者,将这样的文件添加到项目的根目录中,并在其中放置以下内容:
org.gradle.java.home=path/to/my/jdk
(显然用正确的路径替换路径(docs))。