Gradle项目中 dependencyManagement 的作用

在Gradle项目中,dependencyManagement 块用于集中管理项目的依赖版本,确保所有子模块使用相同的依赖版本。这有助于避免版本冲突和依赖混乱。
dependencyManagement 的作用

集中管理依赖版本:
在多模块项目中,可以集中管理所有子模块的依赖版本。
避免每个子模块单独指定依赖版本,从而减少版本冲突。

简化子模块依赖声明:
子模块只需要声明依赖名称,而不必指定版本号。
示例代码
假设您有一个多模块的Gradle项目,最外层的 build.gradle 文件中包含 dependencyManagement 块:

plugins {
    id 'org.springframework.boot' version '2.7.4'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
}

dependencyManagement {
    dependencies {
        imports {
            mavenBom 'org.springframework.cloud:spring-cloud-dependencies:2021.0.1'
        }

        dependency 'org.springframework.boot:spring-boot-starter-web'
        dependency 'org.springframework.boot:spring-boot-starter-data-jpa'
        dependency 'org.springframework.boot:spring-boot-starter-test'
    }
}


imports 块:imports 块用于导入外部的 BOM(Bill of Materials)。
例如,mavenBom 'org.springframework.cloud:spring-cloud-dependencies:2021.0.1' 导入 Spring Cloud 的依赖管理 BOM。


dependency 块:dependency 块用于指定具体的依赖项及其版本。
例如,dependency 'org.springframework.boot:spring-boot-starter-web' 指定了 spring-boot-starter-web 依赖。

假设您的项目结构如下:

my-project/
├── build.gradle
├── settings.gradle
└── submodules/
    ├── module1/
    │   └── build.gradle
    └── module2/
        └── build.gradle

build.gradle 文件(最外层)

plugins {
    id 'org.springframework.boot' version '2.7.4'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
}

dependencyManagement {
    dependencies {
        imports {
            mavenBom 'org.springframework.cloud:spring-cloud-dependencies:2021.0.1'
        }

        dependency 'org.springframework.boot:spring-boot-starter-web'
        dependency 'org.springframework.boot:spring-boot-starter-data-jpa'
        dependency 'org.springframework.boot:spring-boot-starter-test'
    }
}

module1/build.gradle 文件

plugins {
    id 'org.springframework.boot' version '2.7.4'
    id 'java'
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    runtimeOnly 'mysql:mysql-connector-java'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}


dependencyManagement:集中管理依赖版本,避免版本冲突。
imports 块:导入外部的 BOM,简化依赖管理。
dependency 块:指定具体的依赖项及其版本。
这样可以确保所有子模块使用相同的依赖版本,提高项目的可维护性和稳定性。

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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值