Gradle多模块项目构建报错:无法解析spring-boot-starter依赖
Got it, let's work through this issue step by step. The error you're seeing happens because Gradle has no idea where to download your dependencies from—even if you've set up common config in the root build.gradle, you probably haven't properly shared the repository configuration with your submodules.
Here's how to fix it:
1. Add repository configuration to the root build.gradle (shared across all modules)
Wrap your repositories and common plugins in an allprojects or subprojects block so every module inherits this config.
For example, update your root build.gradle like this:
// Apply shared plugins and config to all projects (root + submodules) allprojects { apply plugin: 'java' apply plugin: 'maven' apply plugin: 'eclipse' // This tells Gradle where to fetch dependencies repositories { mavenCentral() // Default Maven central repo // Optional: Add other repos if needed (like mirrors or private repos) // maven { url 'https://maven.aliyun.com/repository/public/' } } // Optional: Add other shared config (Java version, encoding, etc.) sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 }
- Use
allprojectsif you want the root project to also use these settings. - Use
subprojectsinstead if you only want submodules to inherit the config (leave root project settings separate).
2. Clean up your submodule build.gradle files
Your submodules only need their dependencies block—no need to repeat plugins or repositories since they inherit from the root. Make sure your dependencies have valid version numbers too (your error shows a missing version after spring-boot-starter:):
dependencies { // Add a valid version, or use Spring Boot's dependency management (see below) implementation 'org.springframework.boot:spring-boot-starter:3.2.0' // Add other module-specific dependencies here }
Bonus: Better dependency management for Spring Boot multi-module projects
If you're using Spring Boot, you can simplify version management by using the Spring Boot BOM (Bill of Materials). Update your root build.gradle to:
plugins { // Apply Spring Boot plugin without activating it for the root project id 'org.springframework.boot' version '3.2.0' apply false id 'io.spring.dependency-management' version '1.1.4' apply false } allprojects { apply plugin: 'java' apply plugin: 'maven' apply plugin: 'eclipse' repositories { mavenCentral() } } subprojects { // Use Spring's dependency management plugin to inherit version numbers apply plugin: 'io.spring.dependency-management' dependencyManagement { imports { mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES } } }
Now your submodules can omit version numbers for Spring Boot dependencies:
dependencies { implementation 'org.springframework.boot:spring-boot-starter' }
Why this works
Gradle doesn't automatically share repository config across modules unless you explicitly define it in a shared block like allprojects or subprojects. By moving your repository setup to the root config and wrapping it correctly, every submodule will know where to pull dependencies from.
内容的提问来源于stack exchange,提问作者Mike3355

