You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gradle Catalog在Spring Boot项目中异常:Fat Jar依赖缺失

问题描述

我已通过maven-publish插件将如下libs.version.toml文件发布至公司Nexus仓库:

[versions]
boot-version = '3.1.4'
cloud-version = '2022.0.4'
openapi-version = '2.2.0'

[libraries]
spring-boot-bom = { module = "org.springframework.boot:spring-boot-dependencies", version.ref = "boot-version" }
spring-boot-actuator = { module = 'org.springframework.boot:spring-boot-starter-actuator' }
spring-boot-data-jpa = { module = 'org.springframework.boot:spring-boot-starter-data-jpa' }
spring-boot-data-redis = { module = 'org.springframework.boot:spring-boot-starter-data-redis' }
spring-boot-security = { module = 'org.springframework.boot:spring-boot-starter-security' }
spring-boot-oauth2-resource-server = { module = 'org.springframework.boot:spring-boot-starter-oauth2-resource-server' }
spring-boot-test = { module = 'org.springframework.boot:spring-boot-starter-test' }
spring-boot-validation = { module = 'org.springframework.boot:spring-boot-starter-validation' }
spring-boot-web = { module = 'org.springframework.boot:spring-boot-starter-web' }
spring-boot-web-services = { module = 'org.springframework.boot:spring-boot-starter-web-services' }

spring-cloud-bom = { module = 'org.springframework.cloud:spring-cloud-dependencies', version.ref = 'cloud-version' }
spring-cloud-bootstrap = { module = 'org.springframework.cloud:spring-cloud-starter-bootstrap' }
spring-cloud-config = { module = 'org.springframework.cloud:spring-cloud-starter-config' }
spring-cloud-eureka-client = { module = 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' }
spring-cloud-openfeign = { module = 'org.springframework.cloud:spring-cloud-starter-openfeign' }

spring-kafka = { module = 'org.springframework.kafka:spring-kafka' }

spring-session-data-redis = { module = "org.springframework.session:spring-session-data-redis", version = "3.1.2" }

h2 = { module = 'com.h2database:h2' }
poi-ooxml = { module = 'org.apache.poi:poi-ooxml', version = '5.2.4' }

[bundles]
microservice = ['openapi', 'spring-boot-actuator', 'spring-boot-data-jpa', 'spring-boot-validation', 'spring-boot-web',
    'spring-cloud-config', 'spring-cloud-eureka-client', 'spring-cloud-bootstrap', 'oracle']

[plugins]
spring-boot-plugin = { id = 'org.springframework.boot', version.ref = 'boot-version' }
spring-dependency-management-plugin = { id = 'io.spring.dependency-management', version = '1.1.3' }

随后在Spring Boot微服务项目中使用如下build.gradle配置引用该Catalog:

plugins {
    id 'java'
    alias(libs.plugins.spring.boot.plugin)
    alias(libs.plugins.spring.dependency.management.plugin)
}

group = 'my.project.name'
version = '6.0.0'
sourceCompatibility = JavaVersion.VERSION_17

dependencies {
    implementation platform(libs.spring.cloud.bom)
    implementation libs.bundles.microservice

    compileOnly libs.lombok
    testCompileOnly libs.lombok
    annotationProcessor libs.lombok
    testAnnotationProcessor libs.lombok

    testImplementation libs.spring.boot.test
    testRuntimeOnly libs.h2
}

test {
    useJUnitPlatform()
    jvmArgs = ["-Xss8m"]
}

但构建Spring Boot Fat Jar时出现大量依赖缺失的情况,手动逐个添加这些依赖则无此问题,请问这是什么原因?


问题原因及解决方案

核心原因

  1. Bundle包含未定义的依赖项
    microservice bundle里列了openapi和oracle,但libs.version.toml的[libraries]区块根本没这两个依赖的定义。不过这只是次要问题,更关键的是未引入Spring Boot BOM——Spring Boot Starter能自动携带传递依赖,全靠BOM管理版本和依赖链,没有它的话Gradle只会拉取Starter本身的JAR,不会包含其依赖的其他库。

  2. 缺少Spring Boot BOM的引入
    项目只添加了spring-cloud-bom,但Spring Cloud BOM依赖Spring Boot BOM来管理基础依赖版本。未引入Spring Boot BOM会导致Starter的传递依赖版本混乱,甚至部分依赖不会被Gradle识别为需要打包进Fat Jar。

  3. Lombok依赖未在Catalog中定义
    build.gradle里使用了libs.lombok,但libs.version.toml中没有这个配置,这会导致Lombok相关依赖无法解析,虽不是依赖缺失的主因,但会引发额外构建问题。

修复步骤

1. 补全libs.version.toml的依赖定义

在[libraries]区块添加缺失的依赖(版本根据实际情况调整):

openapi = { module = 'org.springdoc:springdoc-openapi-starter-webmvc-ui', version.ref = 'openapi-version' }
oracle = { module = 'com.oracle.database.jdbc:ojdbc11', version = '21.13.0.0' }
lombok = { module = 'org.projectlombok:lombok', version = '1.18.30' }

确保microservice bundle里的所有条目都在[libraries]中有对应配置。

2. 在build.gradle中引入Spring Boot BOM

修改dependencies块,先引入Spring Boot BOM,再引入Spring Cloud BOM:

dependencies {
    implementation platform(libs.spring.boot.bom)
    implementation platform(libs.spring.cloud.bom)
    implementation libs.bundles.microservice

    // 其余依赖配置保持不变
}

3. 验证依赖解析

执行gradle dependencies命令查看依赖树,确认所有Starter的传递依赖都已被正确引入。如果仍有缺失,检查bundle中的依赖是否正确,或是否还有未定义的依赖项。

额外说明

Spring Boot Starter的自动依赖传递是基于BOM的版本管理实现的,BOM不仅统一了依赖版本,还维护了完整的依赖传递链。如果不引入BOM,Gradle无法识别Starter需要携带的依赖,只会打包Starter本身的JAR,最终导致Fat Jar缺失大量运行必需的库。

内容的提问来源于stack exchange,提问作者Amir Pashazadeh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 20:09:53