Maven依赖管理总结 #CSDN博文精选# #依赖管理# #IT# #第三方集成#

大家好,小C将继续与你们见面,带来精选的CSDN博文~

在这里,你将收获:

  • 将系统化学习理论运用于实践,系统学习IT技术
  • 学习内容涵盖数据库、软件测试、主流框架、领域驱动设计和第三方生态等,离全栈工程师更近一步
  • 精心整理的CSDN技术大咖博文,假期学习实现弯道超车

春节假期过后,已经正式开始工作了~专栏《全栈工程师养成记》也进入了尾声,今天就是小C陪伴你们学习这个专栏的最后一天了!

本文来自CSDN博主@Super-小龙

 

前言

Maven作为当下最流行的项目管理工具,其中核心功能就是依赖管理。本文主要总结Maven依赖管理中依赖范围和依赖冲突的解决。

依赖范围

依赖是maven项目引用的资源架包,依赖范围就是这些资源架包在maven项目中的作用范围,反过来说,maven项目通过依赖范围来控制何时引用资源架包。之前有介绍maven的默认生命周期,(compile,test,package,install,deploy)。maven的依赖范围用scope关键字表示,恰好也是五种,虽然不是和生命周期完整对应的,但是基于生命周期划分的。
在这里插入图片描述
通过下表可以知道依赖范围表示的作用域。

依赖范围对于编译执行环境有效对于测试执行环境有效对于运行时执行环境有效例 子
compilespring-core
test××junit
provided×servlet-api
runtime×JDBC驱动
system×本地的,Maven仓库之外的类库

compile:依赖范围默认值是compile,这是最常用的,如果maven项目中引入一个依赖,没有添加scope依赖范围,那么默认的就是compile,表示maven项目在编译,测试,运行阶段都需要引用该依赖。
test:表示该依赖包只和测试相关,测试代码的编译和运行会引用该依赖。
provided:表示maven项目只在编译和测试时引用该依赖,如果将项目打包运行时,则不会引入该依赖,如servlet-api,这是web项目常用的架包,在项目编译和测试时都需要用到该架包,如果项目需要运行,则需要将项目部署到tomcat或其他web服务器上,但是tomcat中自带了servlet-api,如果maven项目中引入了servlet-api,那么会和tomcat中的servlet-api产生冲突,所以可以使用provided限定servlet-api,让maven项目在打包时不再引入servlet-api.

runtime和system是不常用的。runtime表示在测试和运行阶段需要引入该依赖,在编译阶段不引入该依赖,如JDBC的驱动包,因为JDBC驱动是通过反射机制加载的,所以不参与项目编译过程。system的作用域和provided类似,表示引用仓库之外的依赖,需要通过systemPath指定本地依赖的路径,除了特殊情况基本不使用。

传递依赖冲突的解决

先了解一下传递依赖以及什么情况下会产生冲突。
传递依赖,如果项目A需要引入依赖包B,依赖包B又引入了依赖包C,那么B是A的直接依赖,C是A的传递依赖。如果项目A还需要引入依赖包D,依赖包D也引用了依赖包C,当依赖包B引用的C和依赖包D引用的C版本不同时,则发生了依赖冲突。
通过实例说明:一下是Spring的架构图(4.X版本的官方参考文档,5.X版本的没找到,地址:https://docs.spring.io/spring/docs/4.3.21.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#overview-modules)
在这里插入图片描述

现在项目中使用到AOP和Messaging两个模块,在pom.xml文件中引入两个模块的架包坐标信息。

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.3.7.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-messaging</artifactId>
			<version>4.1.7.RELEASE</version>
		</dependency>

由下面的依赖层次结构图可知这两个模块都依赖于spring-core和spring-beans核心架包,这里只看spring-beans架包。
在这里插入图片描述
由上图可知,项目(A)依赖spring-aop(B)和spring-messaging(D)两个架包,而spring-aop(B)又依赖于spring-beans ( C),spring-messaging(D)也依赖于spring-banes( C),但是两个依赖的C版本不一样,spring-messaging引入的spring-beans:4.1.7.RELEASE后面括号中提示因为和4.3.7版本冲突而忽略了,也就是说spring-beans:4.1.7并没有被项目(A)引用。
在这里插入图片描述
查看此时项目中引用的maven依赖,spring-beans的版本是4.3.7.
以上情况是因为maven具有自调节的maven传递依赖的冲突解决。
总结一下maven传递依赖冲突解决方案:

1.Maven自调节原则

1.1 第一声明优先原则

在pom.xml文件中引入两个模块 AOP 和 Messaging 的架包坐标信息,先声明的是AOP模块,当AOP和Messaging的依赖包发生冲突时,项目只会引入AOP模块底下的冲突依赖包,Messaging的冲突依赖包则会被排除。
将AOP和Messaging的声明信息调换一下位置:

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-messaging</artifactId>
			<version>4.1.7.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.3.7.RELEASE</version>
		</dependency>

在pom.xml文件中声明Messaging模块,或者先加入spring-messaging架包,那么项目中会引入spring-messaging下的冲突架包。
在这里插入图片描述

1.2 路径近者优先原则

这个就比较好理解了,就是直接依赖优先于传递依赖,当然会存在多层依赖,当多层依赖发生冲突时,maven的依赖管理会优先引入依赖层级最少的冲突依赖。如:
在这里插入图片描述
项目直接引入spring-aop(4.3.7版本),项目引入的spring-messaging架包的依赖 spring-context,spring-context又依赖于spring-aop(4.1.7版本),那么项目肯定会优先引入AOP的4.3.7版本,因为这个依赖路径最短。

因为Maven具有自调节依赖冲突解决原则,所以项目中不会发生依赖冲突的错误,但是如果想要自定义选择依赖包的版本,可以用以下方法。

2.排除依赖

排除依赖顾名思义就是将不需要的依赖排除,这也是依赖冲突的一种解决方案。

如,项目A依赖于B,B依赖于C,项目A依赖于D,D依赖于C,如果在项目A的pom.xml中先定义B,根据maven自调节的第一声明优先原则,那么D依赖的C就会被默认排除。此时,如果使用关键字exclusion在B中排除C,那么C自然不会再发生冲突了,因为B依赖的C被排除了,项目A会引入D依赖的C。

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-messaging</artifactId>
			<version>4.1.7.RELEASE</version>
			<exclusions>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring-beans</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.3.7.RELEASE</version>
		</dependency>

在这里插入图片描述
spring-messaging中使用exclusion关键字排除spring-beans依赖包,可以看到依赖层级中,spring-messaging依赖中没有spring-beans,所以项目中引入的是spring-aop底下的spring-beans(4.3.7版本)。这就是排除依赖的使用。

3.版本锁定(重点)

所谓版本锁定就是指定依赖包的版本,这种依赖冲突的解决方案是目前实际项目中使用的最多的。
版本锁定使用< dependencyManagement >节点

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-core</artifactId>
				<version>4.3.2.RELEASE</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-aop</artifactId>
				<version>4.3.2.RELEASE</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context</artifactId>
				<version>4.3.2.RELEASE</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-messaging</artifactId>
				<version>4.3.2.RELEASE</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-beans</artifactId>
				<version>4.3.2.RELEASE</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-expression</artifactId>
				<version>4.3.2.RELEASE</version>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-messaging</artifactId>
			<version>4.1.7.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.3.7.RELEASE</version>	
		</dependency>
	</dependencies>

需要说明的是< dependencyManagement >节点仅仅起指定依赖包版本的作用。依赖包的引入仍然是由以下内容确定,如果pom.xml引入依赖包的节点设置了< version >,那么项目中则会引入该版本,如果在< dependencyManagement >节点指定了依赖包的版本,引入依赖包的节点可以不用设置版本。

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-messaging</artifactId>
			<version>4.1.7.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.3.7.RELEASE</version>	
		</dependency>
	</dependencies>

如下图所示 :spring-messaging和spring-aop版本分别是4.1.7和4.3.7,而其他的版本都指定为4.3.2版本。如果将依赖包的坐标信息中的< version >去掉,那么spring-messaging和spring-aop的版本也会变为< dependencyManagement >设定的4.3.2版本。
在这里插入图片描述
为了项目版本依赖管理的统一和方便升级,版本信息通常使用OGNL表达式表示,如下:

	<properties>
		<spring-version>4.3.2.RELEASE</spring-version>
	</properties>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-core</artifactId>
				<version>${spring-version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-aop</artifactId>
				<version>${spring-version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context</artifactId>
				<version>${spring-version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-messaging</artifactId>
				<version>${spring-version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-beans</artifactId>
				<version>${spring-version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-expression</artifactId>
				<version>${spring-version}</version>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-messaging</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
		</dependency>
	</dependencies>

spring-version是一个自定义的常量名,可以设定值为依赖包的版本,然后在
< dependencyManagement >中引用该值,如果项目需要升级依赖包的版本,只需要改变该常量值即可。

传递依赖范围

如A依赖于B,B依赖于C,A传递依赖于C,依赖范围关系如下:
在这里插入图片描述
之后会总结Maven项目拆分的相关知识,再说明对于传递依赖范围的理解。

 

关注高校俱乐部,更多精彩内容等着你~

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值