1. 背景

我们在做多模块开发任务过程中,Java 框架会有很多外部依赖,这些外部依赖还会有版本兼容之间的问题,为了解决每个任务模块所依赖外部 JAR 之间版本,将一组外部依赖 JAR 所兼容或者包含的版本在某个模块中统一管理。
回想下,我们在 Maven 工程中使用 Spring-Boot , spring-boot-dependencies 帮我们完成管理了一套所以来的版本,我们在使用过程中只要引入外部依赖时,仅要声明 groupId 和 artifactId 即可。 不用再担心版本号冲突问题。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
因为项目构建技术在发展,我们新项目全部改用 Gradle 技术,所以只演示 Gradle 构建 BOM 的过程。
Gradle 在早先的时候并不具备原生 BOM,只能通过其他非常不合理的手段或者方式引用 Maven BOM,在 6.0 版本, Gradle 官方提供一款名为 java-platform 插件来实现类似 BOM 的方式,较此前的实现方式,更加简洁。
2. 环境准备
- IDEA 2021+
- Gradle 7.0

详细环境信息参考 [[Gradle-01:0基础入门]]
3. 实现步骤
3.1. 构建Gradle项目
- IDEA 中点击菜单
File->New->Project->Gradle->Java,然后点击Next

- 填写项目信息,然后点击 Finish

- 等待构建依赖环境


最后将通过 IDEA 构建项目 build.gradle 中的 dependencies 和 test 节点删除。
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}
test {
useJUnitPlatform()
}
3.2. 编写build.gradle
3.2.1. plugins
java-platform:Gradle官方提供BOM插件maven-publish: 构建将项目发布的插件
plugins {
// 引入 java-platform 插件
id 'java-platform'
// 发布插件,可用来发布 BOM 或 jar到本地与远程仓库
id 'maven-publish'
}
配置完成上述插件完后,此时编译项目,会在右侧 Gradle 任务模块下多个任务分类,名为 publishing 。

3.2.2. javaPlatform
该配置为防止用户错误地引入依赖,而不是引入依赖约束,如果引入依赖会报错失败。通过这个配置可以让 Gradle 允许引入依赖。
这是一个可选项。
javaPlatform {
allowDependencies()
}
3.2.3. dependencies定义依赖
演示需要,只定义两个外部三方依赖。
dependencies {
constraints {
api 'io.github.rothschil:common-utils:1.2.6.RELEASE'
api 'io.github.rothschil:persistence-mybatis:1.2.6.RELEASE'
}
}
3.2.4. publishing发布
这里只是将项目发布到 Maven 本地仓库。
-
执行
Gradle任务publishToMavenLocal -
IDEA执行发布很快,因为当前任务少

Maven本地仓库就有我们刚才发布的模块

publishing {
publications {
myMvnLocalRepertory(MavenPublication) {
from components.javaPlatform
}
}
}
3.3. publishing发布中央仓库
3.3.1. plugins
plugins {
id 'java-platform'
id 'maven-publish'
id 'signing'
id 'jacoco'
}
3.3.2. publishing
需要在中央仓库中额外申请,申请过程不做赘述,自行百度。
oss_name=${OSS申请}
oss_password=${OSS申请过程中密码}
publishing {
publications {
toSonaType(MavenPublication) {
groupId "$project.group"
artifactId "$project.name"
version "$project.version"
from components.javaPlatform
pom {
name = "rothschil-common"
description = "Pippin, was a Hobbit of the Shire, and one of Frodo Baggins' youngest, but closest friends. He was a member of the Fellowship of the Ring and later became the thirty-second Thain of the Shire"
url = "https://github.com/rothschil/rothschil-common"
// 添加你的 git 仓库 信息
scm {
connection= "scm:git:https://github.com/rothschil/rothschil-common.git"
developerConnection= "scm:git:https://github.com/rothschil/rothschil-common.git"
url= "https://github.com/rothschil/rothschil-common"
}
licenses {
license {
name ="The Apache License, Version 2.0"
url ="http://www.apache.org/licenses/LICENSE-2.0.txt"
}
}
developers {
// 添加开发者描述
developer {
id ="xx"
name ="xxx"
email ="xx"
}
}
}
}
}
repositories {
maven {
name 'snapshot'
url 'https://s01.oss.sonatype.org/content/repositories/snapshots/'
credentials {
username(oss_name)
password(oss_password)
}
}
maven {
name 'release'
url 'https://s01.oss.sonatype.org/content/repositories/releases/'
credentials {
username(oss_name)
password(oss_password)
}
}
}
}
signing {
sign publishing.publications.toSonaType
}
4. 应用
- repositories: 我是将
BOM发布在本地Maven仓库,引用mavenLocal本地库 - implementation: platform(“io.github.rothschil:common-bom:1.0.0-SNAPSHOT”) 配置我发布的
BOM依赖,记得带版本号,此时定义再用io.github.rothschil:persistence-mybatis已经不需要带版本号。
plugins {
id 'java'
}
group 'io.github.rothschil'
version '1.0-SNAPSHOT'
repositories {
mavenLocal()
}
dependencies {
implementation platform("io.github.rothschil:common-bom:1.0.0-SNAPSHOT")
implementation("io.github.rothschil:persistence-mybatis")
}
在 External Libraries 中发现已经有我们需要的依赖包。

Gradle 6.0新特性:构建Java项目BOM解决依赖版本管理
本文介绍了如何在Gradle 7.0环境中利用java-platform插件实现BOM(Bill of Materials),统一管理项目模块间的外部依赖,避免版本冲突,并演示了从本地仓库发布到Sonatype中央仓库的全过程。
1576

被折叠的 条评论
为什么被折叠?



