使用Gradle生成BOM管理依赖版本

写在前边

自Gradle 5开始添加了Maven中 BOM 的支持。然而Gradle 6 以前,想使用BOM,需要使用 Maven 定义并发布 pom.xml 才可以。

好在 Gradle 6 开始,官方提供了一个名为 java-platform 插件,无需复杂配置,即可生成 BOM 并发布,本文就带大家写一个 demo 简单应用此插件。

什么是 BOM?

BOM (The Bill of Materials in Maven) 是Maven中的概念,表现形式是一个 pom.xml 文件,使用 BOM 可以定义一系列依赖及版本。其它项目可以在使用 BOM 时,无需定义已在 BOM 中定义依赖的版本号,即可获取版本号,可理解为统一依赖版本的用途。

当传递的版本号不合要求,可自行定义版本号,覆盖 BOM 中的定义

BOM 由下图结构组成,以下经过部分简化:
在这里插入图片描述
可以看到 打包方式是 pom,包含了 dependencyManagement 块,在 dependencyManagement 中又包含了一系列依赖及具体版本。

使用 Gradle 6 生成 BOM

这里用 IDEA 生成一个空的Gradle项目,结构如下:
在这里插入图片描述

1. 定义 build.gradle

plugins {
    id 'java-platform' // 引入 java-platform插件
    id 'maven-publish' // 发布插件,可用来发布 BOM 或 jar到本地与远程仓库
}

group 'online.hellxz'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

// 默认为防止用户错误地引入依赖,而不是引入依赖约束,如果引入依赖会报错失败。
// 通过这个配置可以让Gradle允许引入依赖,当然这是可选的
javaPlatform {
    allowDependencies()
}

dependencies {
    //在constraints块中定义依赖版本约束
    constraints{
        api group: 'org.springframework.boot', name: 'spring-boot-starter', version: '2.4.2'
    }
}

publishing{
    publications{
        //以下maven并非关键字,可自定义
        maven(MavenPublication) {
            //通过java-platform生成BOM
            from components.javaPlatform
        }
    }
}

2. 定义setting.gradle

只有一行 rootProject.name = 'bom-demo1'

3. 生成 BOM 到本地仓库

在这里插入图片描述
或 执行命令:

gradle -q publishToMavenLocal

4. 查看本地仓库生成的 BOM

在这里插入图片描述

其他项目使用 BOM

build.gradle如下:

plugins {
    id 'java'
}

group 'online.hellxz'
version '1.0-SNAPSHOT'

repositories {
    mavenLocal() // 引本地仓库
}

dependencies {
    // 依赖上文本地仓库中的BOM
    implementation platform('online.hellxz:bom-demo1:1.0-SNAPSHOT')
    // 不带依赖版本引包
    implementation 'org.springframework.boot:spring-boot-starter'
}

结果如图:
在这里插入图片描述

参考

  • https://dzone.com/articles/the-bill-of-materials-in-maven
  • https://dzone.com/articles/gradle-goodness-use-bill-of-materials-bom-as-depen
  • https://docs.gradle.org/6.8.2/userguide/java_platform_plugin.html
Learn how to use Gradle's powerful dependency management through extensive code samples, and discover how to define, customize, and deploy dependencies About This Book Be in total control of your dependencies Deploy your artifacts to repositories with Gradle Learn through code snippets and real-life examples Who This Book Is For If you work on Java projects, use Gradle as a build automation tool, and you use dependencies in your project, this is the book for you. Additionally, if you want to deploy your project artifacts as dependencies for other developers using Gradle, you've found the right book. In Detail Gradle is the next generation in build automation. It allows you to define dependencies for your project in a clear way and also customize how they are resolved to suit your needs. It offers fine-grained control over how to publish your artifacts to Maven and Ivy repositories. Gradle Dependency Management defines dependencies for your Java-based project and customizes how they are resolved. You will learn how to configure the publication of artifacts to different repositories. Packed with plenty of code samples, you will understand how to define the repositories that contain dependencies. Following this, you will learn how to customize the dependency resolution process in Gradle. Table of Contents Chapter 1. Defining Dependencies Chapter 2. Working with Repositories Chapter 3. Resolving Dependencies Chapter 4. Publishing Artifacts Chapter 5. Publishing to a Maven Repository Chapter 6. Publishing to Bintray Chapter 7. Publishing to an Ivy Repository
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

东北小狐狸-Hellxz

请作者喝杯咖啡

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

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

打赏作者

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

抵扣说明:

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

余额充值