IntelliJ中Spring Boot Gradle构建报错:Could not resolve artifacts
解决Spring Boot + Gradle构建失败问题
问题现象
导入Spring Boot项目到IntelliJ IDEA后构建始终失败,执行./gradlew build --warning-mode=all时出现依赖解析错误。
报错详情
./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
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!"); } }
已尝试操作
- 切换Java 11、21、22版本,问题未解决
- 调整build.gradle,添加buildscript块、修改版本等(sourceCompatibility呈灰色)
解决方案
1. 统一Java版本
Spring Boot 3.x最低要求Java 17,修改build.gradle中的sourceCompatibility为17:
sourceCompatibility = '17'
同时确保IntelliJ IDEA的Project Structure和Settings中配置的Java SDK为17。
2. 清理重复插件配置
删除buildscript块,因为plugins块已引入Spring Boot插件,重复配置会导致冲突。
3. 修正后的build.gradle
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 = '17' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' testImplementation('org.springframework.boot:spring-boot-starter-test') } test { useJUnitPlatform() }
4. 同步并重新构建
点击IntelliJ IDEA中的"Sync Project with Gradle Files"按钮,执行./gradlew build重新构建项目。
关键原因
- Spring Boot 3.x系列仅支持Java 17及以上版本,原配置的sourceCompatibility=1.6导致插件依赖解析不兼容。
- buildscript块与plugins块重复引入Spring Boot插件,引发配置冲突。
内容的提问来源于stack exchange,提问作者alex
相关产品推荐
相关产品推荐

