Gradle-04:BOM管理依赖版本

1. 背景

gradle

我们在做多模块开发任务过程中,Java 框架会有很多外部依赖,这些外部依赖还会有版本兼容之间的问题,为了解决每个任务模块所依赖外部 JAR 之间版本,将一组外部依赖 JAR 所兼容或者包含的版本在某个模块中统一管理。

回想下,我们在 Maven 工程中使用 Spring-Bootspring-boot-dependencies 帮我们完成管理了一套所以来的版本,我们在使用过程中只要引入外部依赖时,仅要声明 groupIdartifactId 即可。 不用再担心版本号冲突问题。


<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

20220821194706

详细环境信息参考 [[Gradle-01:0基础入门]]

3. 实现步骤

3.1. 构建Gradle项目

  • IDEA 中点击菜单 File -> New -> Project -> Gradle -> Java ,然后点击 Next

20220821195011

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

20220821195113

  • 等待构建依赖环境

20220821195149

20220821195221

最后将通过 IDEA 构建项目 build.gradle 中的 dependenciestest 节点删除。


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

20220821200324

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 执行发布很快,因为当前任务少

20220821201306

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

20220821201442


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 中发现已经有我们需要的依赖包。

20220821203633

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王老邪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值