maven学习笔记(三)maven的依赖、传递依赖和依赖冲突

有三个maven项目,分别的A项目、B项目、C项目,都把A项目、B项目、C项目都打成一个jar包,并且把jar包安装到本地仓库中。

如下:


下面是A项目的pom文件,其他B项目和C项目的是一样的,只是他们的项目信息构件中的artifactId标签是B和C而已。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <!-- 项目的构件信息 -->
  <groupId>com.qw.hongxing</groupId>
  <artifactId>A</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>A</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <!-- 管理项目jar包的列表。 -->
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

B项目引入了A项目的jar包,那么B项目和A项目的关系就是,B项目依赖A项目。

C项目引入了B项目的jar包,那么C项目依赖B项目,C项目传递依赖A项目。

B项目可以使用A项目的所有jar包,C项目可以使用A项目、B项目的所有jar包。

1、B项目如何引用A项目的jar包?如下:

<dependencies>
    <dependency><!--这里是junit的构件信息 -->
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <!-- 引入的A项目,这里是A项目的构件信息 -->
    <dependency>
    	<groupId>com.qw.hongxing</groupId>
	<artifactId>A</artifactId>
	<version>0.0.1-SNAPSHOT</version>
    </dependency>
 </dependencies>

2、C项目只想引用B项目的jar包,不想引用A项目的jar包,怎么办?

使用排除依赖的标签,就可以不适用A项目的jar包了。

<!-- 引入的B项目 -->
    <dependency>
    	        <groupId>com.qw.hongxing</groupId>
	  	<artifactId>B</artifactId>
	  	<version>0.0.1-SNAPSHOT</version>
	  	<exclusions>
	  		<exclusion><!-- 排除A项目的jar包 -->
	  			<groupId>com.qw.hongxing</groupId>
	  			<artifactId>A</artifactId>
	  		</exclusion>
	  	</exclusions>
    </dependency>


依赖冲突:

问题:如果A项目、B项目有相同构件的jar包,版本不同,比如,A项目有spring-context 3.0,B项目有spring-context 4.0,而C项目到底依赖的是那个版本的构件呢?

1、短路优先:解析路径短的版本

比如:

C 依赖 B 依赖 A ,并且 C 也依赖 D

A项目中有一个log4j 2.10,D项目中有一个log4j 2.0

那么,C依赖的log4j 版本就是2.0

2、先声明,下优先,路径一样的时候,则谁先声明,就先解析谁。

比如:C依赖B,并且C也依赖D

B项目中有一个log4j 2.10,D项目中有一个log4j 2.0

那么,就要看谁先声明,就引用谁了。

如下,C先声明B项目,那么C依赖的log4j版本就是2.10,如下就是C项目的pom文件的jar包列表:

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <!-- 引入的B项目 -->
    <dependency>
    	<groupId>com.qw.hongxing</groupId>
	  	<artifactId>B</artifactId>
	  	<version>0.0.1-SNAPSHOT</version>
    </dependency>
    
    <!-- 引入D项目 -->
    <dependency>
    	<groupId>com.qw.hongxing</groupId>
		<artifactId>D</artifactId>
		<version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值