当前位置: 首页 > ops >正文

Maven​​ 和 ​​Gradle​​ 依赖管理的详细说明及示例,涵盖核心概念、配置方法、常见问题解决和工具对比。

一、Maven 依赖管理

1. 核心概念

  • ​依赖声明​​:在 pom.xml 中通过 <dependency> 标签定义依赖项,包含 groupIdartifactIdversion
  • ​仓库​​:依赖下载的来源,包括中央仓库(Maven Central)、私有仓库(如 Nexus、Artifactory)。
  • ​依赖范围(Scope)​​:控制依赖在不同阶段的可见性,如 compile(默认)、testprovidedruntime
  • ​依赖传递​​:自动引入传递性依赖,可通过 <exclusions> 手动排除。

2. 依赖配置示例

(1) 基本依赖声明
xml

复制

<!-- pom.xml -->
<dependencies><!-- Spring Core 依赖 --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>5.3.20</version><scope>compile</scope> <!-- 默认可省略 --></dependency><!-- JUnit 测试依赖 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency>
</dependencies>
(2) 仓库配置
xml
复制
<repositories><!-- 中央仓库(默认已配置,无需显式声明) --><repository><id>central</id><url>https://repo.maven.apache.org/maven2</url></repository><!-- 私有仓库 --><repository><id>company-nexus</id><url>http://nexus.company.com/repository/maven-public/</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository>
</repositories>
(3) 依赖范围与排除
xml
复制
<!-- 仅编译时可用,不打包到最终产物 -->
<dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>4.0.1</version><scope>provided</scope>
</dependency><!-- 排除传递性依赖 -->
<dependency><groupId>com.example</groupId><artifactId>lib-a</artifactId><version>1.0.0</version><exclusions><exclusion><groupId>org.unwanted</groupId><artifactId>unwanted-lib</artifactId></exclusion></exclusions>
</dependency>

3. 依赖管理(Dependency Management)

在父 POM 中统一管理依赖版本:

 

xml

复制

<dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.7.0</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement>

二、Gradle 依赖管理

1. 核心概念

  • ​依赖配置​​:通过 dependencies 块定义,支持多种配置(如 implementationapitestImplementation)。
  • ​仓库​​:支持 Maven Central、Ivy、自定义仓库(如 GitHub Packages)。
  • ​动态版本​​:支持 1.0.+ 表示最新兼容版本。
  • ​依赖解析策略​​:可自定义版本冲突解决规则。

2. 依赖配置示例

(1) 基本依赖声明(Groovy DSL)
groovy

复制

// build.gradle
plugins {id 'java'
}repositories {mavenCentral()// 私有仓库maven {url 'http://nexus.company.com/repository/maven-public/'}
}dependencies {// 编译时依赖implementation 'org.springframework:spring-core:5.3.20'// 测试依赖testImplementation 'junit:junit:4.13.2'// 排除传递性依赖implementation('com.example:lib-a:1.0.0') {exclude group: 'org.unwanted', module: 'unwanted-lib'}
}
(2) 依赖配置类型
配置名称用途
implementation编译和运行时依赖(默认)
api暴露给其他模块的依赖(类似 Maven compile
testImplementation仅测试代码依赖
runtimeOnly仅运行时需要,编译时不需要
(3) 依赖版本管理
groovy

复制

// 统一管理版本号
ext {springVersion = '5.3.20'
}dependencies {implementation "org.springframework:spring-core:${springVersion}"
}
(4) 动态版本与变更日志
groovy

复制

// 动态版本(慎用,可能导致不稳定)
dependencies {implementation 'com.example:lib-b:1.0.+'
}// 指定版本范围
dependencies {implementation 'com.example:lib-c:[1.0, 2.0)'
}

三、Maven vs Gradle 对比

​特性​​Maven​​Gradle​
​配置语法​XML(冗长、不灵活)Groovy/Kotlin DSL(简洁、灵活)
​依赖管理​静态依赖,无动态版本支持动态版本、自定义解析策略
​构建速度​较慢(无增量构建)快(增量构建、缓存机制)
​扩展性​插件有限,配置复杂支持自定义任务和灵活脚本
​多模块项目​需手动配置父 POM天然支持复合构建

四、常见依赖问题解决

1. 依赖冲突

  • ​Maven​​:使用 mvn dependency:tree 查看依赖树,通过 <exclusions> 排除冲突依赖。
  • ​Gradle​​:运行 gradle dependencies 查看依赖树,使用 exclude 或强制指定版本:
    groovy

    复制

    configurations.all {resolutionStrategy.force 'org.apache.commons:commons-lang3:3.12.0'
    }

2. 依赖下载失败

  • 检查仓库配置(尤其是私有仓库权限)。
  • 清理本地缓存:Maven 使用 mvn dependency:purge-local-repository,Gradle 使用 gradle cleanBuildCache

五、总结

  • ​Maven​​:适合标准化项目,依赖管理简单但灵活性不足。
  • ​Gradle​​:适合复杂项目,依赖管理灵活且性能优越,但学习曲线较陡。

​选择建议​​:

  • 新项目优先选择 Gradle(尤其是 Kotlin DSL)。
  • 遗留项目或严格遵循标准化的团队可使用 Maven。
http://www.xdnf.cn/news/12121.html

相关文章:

  • 最小硬件系统概念及其组成
  • 安卓后台常驻读取NFC
  • 知识蒸馏:从模型输出到深层理解
  • 论文笔记——相干体技术在裂缝预测中的应用研究
  • kafka入门学习
  • 蓝桥杯2118 排列字母
  • 10_聚类
  • llm-d:面向Kubernetes的高性能分布式LLM推理框架
  • react私有样式处理
  • Nuxt.js 入门总结教程
  • 汇编语言综合程序设计:子程序、分支与循环深度解析
  • 灾难恢复演练:数据库备份与恢复的全流程实践
  • [测试_10] Selenium IDE | cssSelector | XPath | 操作测试
  • 9.axios底层原理,和promise的对比(2)
  • BLOB 是用来存“二进制大文件”的字段类型
  • 时间获取函数
  • 制作官网水平导航栏
  • 开源供应链攻击持续发酵,多个软件包仓库惊现恶意组件
  • 捍卫低空安全!-中科固源发现无人机MavLink协议远程内存泄漏漏洞
  • 设计模式(代理设计模式)
  • 墨者学院-密码学实训隐写术第二题
  • 【C++快读快写】
  • nt!CcMapData函数分析之Loop to touch each page触发nt!MmAccessFault函数----NTFS源代码分析之四
  • 中国西部逐日1 km全天候地表温度数据集(TRIMS LST-TP;2000-2024)
  • npm run dev 报错:Error: error:0308010C:digital envelope routines::unsupported
  • UDP:简洁高效的报文结构解析与关键注意事项
  • std::conditional_t一个用法
  • [10-2]MPU6050简介 江协科技学习笔记(22个知识点)
  • MVCC机制:Undo Log版本链与ReadView机制
  • Python Excel 文件处理:openpyxl 与 pandas 库完全指南