maven项目的依赖管理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/by_perseverance/article/details/78493918

依赖管理-传递依赖

1.1 传递依赖

1.1.1  什么是传递依赖

A 依赖BB依赖C,在A中导入B后会自动导入CCA的传递依赖,如果C依赖DD也可能是A的传递依赖

演示:

web中添加struts-springjar,传递依赖了spring

1.1.2 依赖范围对传递依赖的影响(了解)

依赖会有依赖范围,依赖范围对传递依赖也有影响,有ABCA依赖BB依赖CC可能是A的传递依赖,如下图:

最左边一列为直接依赖,理解为A依赖B的范围,最顶层一行为传递依赖,理解为B依赖C的范围,行与列的交叉即为A传递依赖C的范围。

举例:

比如 A compile 依赖,C runtime 依赖,那么根据表格所示A runtime 依赖。

测试

dao依赖junitscoptest

service依赖dao.

查看下图红色框内所示传递依赖范围:

所以maven-first所依赖的junitjar没有加入到maven-web工程。

如果修改maven-first依赖junitscopcompilemaven-first所依赖的junitjar包会加入到maven-web工程中,符合上边表格所示,查看下图红色框内所示:

1.2 依赖版本冲突解决

1.2.1 问题

当一个项目依赖的构件比较多时,它们相互之前存在依赖,当你需要对依赖版本统一管理时如果让maven自动来处理可能并不能如你所愿,如下例子:

同时加入以下依赖,观察依赖:

   <!-- struts2-spring-plugin依赖spirng-beans-3.0.5 -->

   <dependency>

   <groupId>org.apache.struts</groupId>

   <artifactId>struts2-spring-plugin</artifactId>

   <version>2.3.24</version>

   </dependency>

  

      <!-- spring-context依赖spring-beans-4.2.4 -->

   <dependency>

   <groupId>org.springframework</groupId>

   <artifactId>spring-context</artifactId>

   <version>4.2.4.RELEASE</version>

   </dependency>

 

org.apache.struts依赖spirng-beans-3.0.5,spring-context依赖spring-beans-4.2.4,但是发现spirng-beans-3.0.5加入到工程中,而我们希望spring-beans-4.2.4加入工程。

1.2.2 依赖调解原则

maven自动按照下边的原则调解:

ü 1、第一声明者优先原则

pom文件定义依赖,先声明的依赖为准。

测试:

如果将上边struts-spring-pluginsspring-context顺序颠倒,系统将导入spring-beans-4.2.4

分析:

由于spring-context在前边以spring-context依赖的spring-beans-4.2.4为准,所以最终spring-beans-4.2.4添加到了工程中。

ü 2、路径近者优先原则

例如:A依赖 spirng-beans-4.2.4A依赖B依赖 spirng-beans-3.0.5,则spring-beans-4.2.4优先被依赖在A中,因为spring-beans-4.2.4相对spirng-beans-3.0.5A依赖的路径最近。

测试:

在本工程中的pom中加入spirng-beans-4.2.4的依赖,根据路径近者优先原则,系统将导入spirng-beans-4.2.4

<dependency>

   <groupId>org.springframework</groupId>

   <artifactId>spring-beans</artifactId>

   <version>4.2.4.RELEASE</version>

   </dependency>

1.2.3 排除依赖

上边的问题也可以通过排除依赖方法辅助依赖调解,如下:

比如在依赖struts2-spring-plugin的设置中添加排除依赖,排除spring-beans,

下边的配置表示:依赖struts2-spring-plugin,但排除struts2-spring-plugin所依赖的spring-beans。

   <!-- struts2-spring-plugin依赖spirng-beans-3.0.5 -->

   <dependency>

   <groupId>org.apache.struts</groupId>

   <artifactId>struts2-spring-plugin</artifactId>

   <version>2.3.24</version>

   <!-- 排除 spring-beans-->

   <exclusions>

   <exclusion>

   <groupId>org.springframework</groupId>

   <artifactId>spring-beans</artifactId>

   </exclusion>

<exclusion>

   <groupId>org.springframework</groupId>

   <artifactId>spring-context</artifactId>

   </exclusion>

   </exclusions>

   </dependency>

 

1.2.4 锁定版本

面对众多的依赖,有一种方法不用考虑依赖路径、声明优化等因素可以采用直接锁定版本的方法确定依赖构件的版本,版本锁定后则不考虑依赖的声明顺序或依赖的路径,以锁定的版本的为准添加到工程中,此方法在企业开发中常用。

如下的配置是锁定了spring-beansspring-context的版本:

  <dependencyManagement>

   <dependencies>

   <!--这里锁定版本为4.2.4 -->

   <dependency>

   <groupId>org.springframework</groupId>

   <artifactId>spring-beans</artifactId>

   <version>4.2.4.RELEASE</version>

   </dependency>

<dependency>

   <groupId>org.springframework</groupId>

   <artifactId>spring-context</artifactId>

   <version>4.2.4.RELEASE</version>

   </dependency>

   </dependencies>

  </dependencyManagement>

注意:在工程中锁定依赖的版本并不代表在工程中添加了依赖,如果工程需要添加锁定版本的依赖则需要单独添加<dependencies></dependencies>标签,如下:

<dependencies>

   <!--这里是添加依赖 -->

   <dependency>

   <groupId>org.springframework</groupId>

   <artifactId>spring-beans</artifactId>

  </dependency>

<dependency>

   <groupId>org.springframework</groupId>

   <artifactId>spring-context</artifactId>

  </dependency>

   </dependencies>

上边添加的依赖并没有指定版本,原因是已在<dependencyManagement>中锁定了版本,所以在<dependency>下不需要再指定版本。

猜你喜欢

转载自blog.csdn.net/by_perseverance/article/details/78493918