Maven简介(六)——Dependency

简介: Dependency介绍7.1     依赖的传递性当项目A依赖于B,而B又依赖于C的时候,自然的A会依赖于C,这样Maven在建立项目A的时候,会自动加载对C的依赖。

Dependency介绍

7.1     依赖的传递性

当项目A依赖于B,而B又依赖于C的时候,自然的A会依赖于C,这样Maven在建立项目A的时候,会自动加载对C的依赖。

依赖传递对版本的选择

假设A依赖于B和C,然后B依赖于D,D又依赖于E1.0,C直接依赖于E2.0,那么这个时候A依赖的是E1.0还是E2.0,还是这两个都依赖呢?两个都依赖是肯定不行的,因为它们可能会有冲突的地方。这个时候就涉及到Maven中依赖传递对版本的选择问题。依赖传递在选择版本的时候首先是根据深度选择的。当一个项目同时经过不同的路径依赖于同一个组件时,会选择其深度最短的对应组件进行依赖。举例来说,假设A->B->C->D1.0,A->E->D2.0,那么这个时候A就会选择对D相对路径短的组件来进行依赖,也就是D2.0。那么当深度一样的时候Maven会如何选择呢?即A->B->D1.0和A->C->D2.0,这个时候Maven会如何选择A所依赖的D的版本呢?这种情况Maven会根据申明的依赖顺序来进行选择,先申明的会被作为依赖包。向前面这种情况,如果先申明对B的依赖,则A依赖的就是D1.0,如果先申明对C的依赖,则A依赖的就是D2.0。

使用exclusion排除依赖

假设有这样一种依赖关系,A->B->C,这个时候由于某些原因,我们不需要对C的依赖,但是我们又必须要对B的依赖,这个时候该怎么办呢?针对这种情况,Maven给我们提供了一个exclusion功能,我们可以在添加A对B的依赖时申明不需要引进B对C的依赖。具体做法如下:

Xml代码   收藏代码
  1. <dependencies>  
  2.        <dependency>  
  3.               <groupId>groupB</groupId>  
  4.               <artifactId>artifactB</artifactId>  
  5.               <version>1.0</version>  
  6.               <exclusions>  
  7.                      <exclusion>  
  8.                             <groupId>groupC</groupId>  
  9.                             <artifactId>artifactC</artifactId>  
  10.                      </exclusion>  
  11.               </exclusions>  
  12.        </dependency>  
  13.        ...  
  14. </dependencies>  

 

可选的依赖项

可选的依赖项表示可有可无,不一定需要的,它只是做一个标记。为了便于大家理解,我们先看这样一种情况,假设项目B依赖于项目C,这个时候我们把B对C的依赖利用optional标记为可选的,它意味着B中只有部分地方用到了C,并不是必须要的,当你依赖于B,但是又不需要使用到B的C功能时,可以不依赖于C。这样当A->B->C时,在建立项目A的时候将不会加入对C的依赖,因为C对B是可选的,我们不一定会用到C。但是在建立项目B的时候,Maven就会加入对C的依赖。也就是说这种标记为optional的依赖项对项目本身而言是没有什么影响的,它影响的是以该项目作为依赖项的其他项目,如这里的项目A。这种可选的依赖项有一个好处就是它会默认的作为exclusion项排除。

Xml代码   收藏代码
  1. <project>  
  2.        <groupId>groupB</groupId>  
  3.        <artifactId>artifactB</artifactId>  
  4.        <version>1.0</version>  
  5.        <dependencies>  
  6.               <dependency>  
  7.                      <groupId>groupC</groupId>  
  8.                      <artifactId>artifactC</artifactId>  
  9.                      <version>1.0</version>  
  10.                      <optional>true</optional>  
  11.               </dependency>  
  12.               ...  
  13.        </dependencies>  
  14. </project>  
Xml代码   收藏代码
  1.    
  2. <project>  
  3.        <groupId>groupA</groupId>  
  4.        <artifactId>artifactA</artifactId>  
  5.        <version>1.0</version>  
  6.        <dependencies>  
  7.               <dependency>  
  8.                      <groupId>groupB</groupId>  
  9.                      <artifactId>artifactB</artifactId>  
  10.                      <version>1.0</version>  
  11.               </dependency>  
  12.               ...  
  13.        </dependencies>  
  14. </project>  

 

7.2     依赖项的作用域

在定义项目的依赖项的时候,我们可以通过scope来指定该依赖项的作用范围。scope的取值有compile、runtime、test、provided、system和import。

compile:这是依赖项的默认作用范围,即当没有指定依赖项的scope时默认使用compile。compile范围内的依赖项在所有情况下都是有效的,包括运行、测试和编译时。

runtime:表示该依赖项只有在运行时才是需要的,在编译的时候不需要。这种类型的依赖项将在运行和test的类路径下可以访问。

test:表示该依赖项只对测试时有用,包括测试代码的编译和运行,对于正常的项目运行是没有影响的。

provided:表示该依赖项将由JDK或者运行容器在运行时提供,也就是说由Maven提供的该依赖项我们只有在编译和测试时才会用到,而在运行时将由JDK或者运行容器提供。

system:当scope为system时,表示该依赖项是我们自己提供的,不需要Maven到仓库里面去找。指定scope为system需要与另一个属性元素systemPath一起使用,它表示该依赖项在当前系统的位置,使用的是绝对路径。比如官网给出了一个关于使用JDK的tools.jar的例子:

Xml代码   收藏代码
  1. <project>  
  2.   ...  
  3.   <dependencies>  
  4.     <dependency>  
  5.       <groupId>sun.jdk</groupId>  
  6.       <artifactId>tools</artifactId>  
  7.       <version>1.5.0</version>  
  8.       <scope>system</scope>  
  9.       <systemPath>${java.home}/../lib/tools.jar</systemPath>  
  10.     </dependency>  
  11.   </dependencies>  
  12.   ...  
  13. </project>  

 

import:关于import的介绍,可以看后文中对引入依赖项的介绍。

7.3     dependencyManagement介绍

dependencyManagement主要有两个作用,一个是集中管理项目的依赖项,另一个就是控制使用的依赖项的版本。

7.3.1集中管理依赖项

看这样一种情况,我们有以下两个项目:

projectA

Xml代码   收藏代码
  1. <project>  
  2.        <groupId>groupA</groupId>  
  3.        <artifactId>artifactA</artifactId>  
  4.        <version>1.0</version>  
  5.        <dependencies>  
  6.               <dependency>  
  7.                      <groupId>groupC</groupId>  
  8.                      <artifactId>artifactC</artifactId>  
  9.                      <version>1.0</version>  
  10.               </dependency>  
  11.               <dependency>  
  12.                      <groupId>groupD</groupId>  
  13.                      <artifactId>artifactD</artifactId>  
  14.                      <version>1.0</version>  
  15.               </dependency>  
  16.        </dependencies>  
  17.        ...  
  18. </project>  

 

       ProjectB

Xml代码   收藏代码
  1. <project>  
  2.        <groupId>groupB</groupId>  
  3.        <artifactId>artifactB</artifactId>  
  4.        <version>1.0</version>  
  5.        <dependencies>  
  6.               <dependency>  
  7.                      <groupId>groupC</groupId>  
  8.                      <artifactId>artifactC</artifactId>  
  9.                      <version>1.0</version>  
  10.               </dependency>  
  11.               <dependency>  
  12.                      <groupId>groupE</groupId>  
  13.                      <artifactId>artifactE</artifactId>  
  14.                      <version>1.0</version>  
  15.                      <type>bar</type>  
  16.               </dependency>  
  17.        </dependencies>  
  18.        ...  
  19. </project>  

 

       从这两个项目中我们可以看出来,它们都依赖了artifactC,这样我们就可以创建一个新的项目,使用其dependencyManagement来统一管理这些依赖项。我们创建项目P来管理这些依赖项:

       projectP

Xml代码   收藏代码
  1. <project>  
  2.        ...  
  3.        <dependencyManagement>  
  4.               <dependencies>  
  5.                      <dependency>  
  6.                             <groupId>groupC</groupId>  
  7.                             <artifactId>artifactC</artifactId>  
  8.                             <version>1.0</version>  
  9.                      </dependency>  
  10.                      <dependency>  
  11.                             <groupId>groupD</groupId>  
  12.                             <artifactId>artifactD</artifactId>  
  13.                             <version>1.0</version>  
  14.                      </dependency>  
  15.                      <dependency>  
  16.                             <groupId>groupE</groupId>  
  17.                             <artifactId>artifactE</artifactId>  
  18.                             <version>1.0</version>  
  19.                             <type>bar</type>  
  20.                      </dependency>  
  21.               </dependencies>  
  22.        </dependencyManagement>  
  23.        ...  
  24. </project>  

 

       之后我们就可以这样来定义我们的projectA和projectB。

projectA

Xml代码   收藏代码
  1. <project>  
  2.        <modelVersion>4.0</modelVersion>  
  3.        <groupId>groupA</groupId>  
  4.        <artifactId>artifactA</artifactId>  
  5.        <version>1.0</version>  
  6.        <dependencies>  
  7.               <dependency>  
  8.                      <groupId>groupC</groupId>  
  9.                      <artifactId>artifactC</artifactId>  
  10.               </dependency>  
  11.               <dependency>  
  12.                      <groupId>groupD</groupId>  
  13.                      <artifactId>artifactD</artifactId>  
  14.               </dependency>  
  15.        </dependencies>  
  16.        ...  
  17. </project>  

 

 

       ProjectB

Xml代码   收藏代码
  1. <project>  
  2.        <modelVersion>4.0</modelVersion>  
  3.        <groupId>groupB</groupId>  
  4.        <artifactId>artifactB</artifactId>  
  5.        <version>1.0</version>  
  6.        <dependencies>  
  7.               <dependency>  
  8.                      <groupId>groupC</groupId>  
  9.                      <artifactId>artifactC</artifactId>  
  10.               </dependency>  
  11.               <dependency>  
  12.                      <groupId>groupE</groupId>  
  13.                      <artifactId>artifactE</artifactId>  
  14.               <!--因为artifactE的类型不是默认的jar,所以这里需要指定依赖项的类型-->  
  15.                      <type>bar</type>  
  16.               </dependency>  
  17.        </dependencies>  
  18.        ...  
  19. </project>  

 

从上面的定义我们可以看出,我们已经简化了projectA和projectB依赖项的定义,我们可以只在projectA和projectB中申明需要使用的依赖项,而不必指定其对应的版本和作用范围等信息,当指定了这些信息时子项目中的定义会覆盖父项目的dependencyManagement中的定义,否则将使用dependencyManagement中的定义。这样也可以很方便我们对依赖项的版本进行统一管理。

7.3.2控制依赖项使用的版本

看下面这样一种情况

projectA

Xml代码   收藏代码
  1. <project>  
  2.        <modelVersion>4.0</modelVersion>  
  3. <groupId>groupA</groupId>  
  4.        <artifactId>artifactA</artifactId>  
  5.        <version>1.0</version>  
  6.        <packaging>pom</packaging>  
  7.        <dependencyManagement>  
  8.               <dependencies>  
  9.                      <dependency>  
  10.                             <groupId>groupA</groupId>  
  11.                             <artifactId>A</artifactId>  
  12.                             <version>1.5</version>  
  13.                      </dependency>  
  14.                      <dependency>  
  15.                             <groupId>groupA</groupId>  
  16.                             <artifactId>B</artifactId>  
  17.                             <version>1.0</version>  
  18.                      </dependency>  
  19.                      <dependency>  
  20.                             <groupId>groupA</groupId>  
  21.                             <artifactId>C</artifactId>  
  22.                             <version>1.0</version>  
  23.                      </dependency>  
  24.                      <dependency>  
  25.                             <groupId>groupA</groupId>  
  26.                             <artifactId>D</artifactId>  
  27.                             <version>1.6</version>  
  28.                      </dependency>  
  29.               </dependencies>  
  30.        </dependencyManagement>  
  31.        ...  
  32. </project>  

 

 

       ProjectB

Xml代码   收藏代码
  1. <project>  
  2.        <modelVersion>4.0</modelVersion>  
  3.        <groupId>groupB</groupId>  
  4.        <artifactId>artifactB</artifactId>  
  5.        <version>1.0</version>  
  6.        <packaging>pom</packaging>  
  7.        <dependencyManagement>  
  8.               <dependencies>  
  9.                      <dependency>  
  10.                             <groupId>groupA</groupId>  
  11.                             <artifactId>A</artifactId>  
  12.                             <version>1.0</version>  
  13.                      </dependency>  
  14.               </dependencies>  
  15.        </dependencyManagement>  
  16.        <dependencies>  
  17.               <dependency>  
  18.                      <groupId>groupA</groupId>  
  19.                      <artifactId>B</artifactId>  
  20.                      <version>1.8</version>  
  21.               </dependency>  
  22.               <dependency>  
  23.                      <groupId>groupA</groupId>  
  24.                      <artifactId>C</artifactId>  
  25.                      <scope>runtime</scope>  
  26.               </dependency>  
  27.        </dependencies>  
  28.        ...  
  29. </project>  

 

       这样,当我们对projectB进行Maven操作的时候,依赖项A、B、C和D将会如下选择:

       当A是B或者C的一个依赖项的时候,projectB将使用的是A的1.0版本,因为A是申明在projectB的dependencyManagement中的,此外申明在dependencyManagement中的依赖项将比项目的依赖项的依赖项有更高的优先级,还有就是当前项目的申明将比其父项目的申明具有更高的优先级。

       依赖项B将使用版本1.8,因为依赖项B是直接申明在projectB中的,而且指定了版本为1.8,所以当前项目申明的版本会覆盖父项目的dependencyManagement中指定的版本。

       依赖项C将使用版本1.0,但是其作用范围将会是runtime。因为依赖项C也是直接申明在projectB中的,而且其没有指定自己所使用的版本,所以将使用其父项目的dependencyManagement中指定的版本。此外,其指定了自己所作用的范围是runtime,所以它会覆盖其父项目的dependencyManagement中所指定依赖项的默认的compile作用范围。

       依赖项D将使用版本1.6。当D是B或者C的一个依赖项的时候,projectB将使用D的1.6版本,因为项目D是申明在dependencyManagement中的,而且在dependencyManagement中申明的依赖项将比间接的依赖项具有更高的优先级。

7.4     引入依赖项

我们知道通过继承一个项目我们可以在子项目中很好的申明需要使用的父项目的dependencyManagement定义的依赖项。但是因为每个项目都只能申明唯一的一个父项目,这在某些时候就会限制我们的项目建立。为此Maven为我们提供了一种方法,那就是通过设定依赖项的scope为import。注意这种方式只有在Maven2.0.9以上的版本才能使用。

Xml代码   收藏代码
  1. <project>  
  2.        ...  
  3.        <groupId>groupA</groupId>  
  4.        <artifactId>artifactA</artifactId>  
  5.        <version>1.0</version>  
  6.        <packaging>pom</packaging>  
  7.        <dependencyManagement>  
  8.               <dependencies>  
  9.                      <dependency>  
  10.                             <groupId>test</groupId>  
  11.                             <artifactId>A</artifactId>  
  12.                             <version>1.0</version>  
  13.                      </dependency>  
  14.                      <dependency>  
  15.                             <groupId>test</groupId>  
  16.                             <artifactId>B</artifactId>  
  17.                             <version>1.1</version>  
  18.                      </dependency>  
  19.                      <dependency>  
  20.                             <groupId>test</groupId>  
  21.                             <artifactId>C</artifactId>  
  22.                             <version>1.2</version>  
  23.                      </dependency>  
  24.                      <dependency>  
  25.                             <groupId>test</groupId>  
  26.                             <artifactId>D</artifactId>  
  27.                             <version>1.3</version>  
  28.                      </dependency>  
  29.               </dependencies>  
  30.        </dependencyManagement>  
  31.        ...  
  32. </project>  

 

上面这个项目是用来统一管理项目的依赖项的。那么按照之前的那种继承机制,这个时候我们的项目artifactB是如下这样定义的:

Xml代码   收藏代码
  1. <project>  
  2.        ...  
  3.        <parent>  
  4.               <groupId>groupA</groupId>  
  5.               <artifactId>artifactA</artifactId>  
  6.               <version>1.0</version>  
  7.        </parent>  
  8.        <groupId>groupA</groupId>  
  9.        <artifactId>artifactB</artifactId>  
  10.        <version>1.0</version>  
  11.        <packaging>pom</packaging>  
  12.        <dependencies>  
  13.               <dependency>  
  14.                      <groupId>test</groupId>  
  15.                      <artifactId>A</artifactId>  
  16.               </dependency>  
  17.               <dependency>  
  18.                      <groupId>test</groupId>  
  19.                      <artifactId>D</artifactId>  
  20.                      <scope>runtime</scope>  
  21.               </dependency>  
  22.        </dependencies>  
  23.        ...  
  24. </project>  

 

       那么如果按照import这种形式的话,artifactB可以如下定义:

Xml代码   收藏代码
  1. <project>  
  2.        ...  
  3.        <groupId>groupA</groupId>  
  4.        <artifactId>artifactB</artifactId>  
  5.        <version>1.0</version>  
  6.        <packaging>pom</packaging>  
  7.        <dependencyManagement>  
  8.               <dependencies>  
  9.                      <dependency>  
  10.                             <groupId>groupA</groupId>  
  11.                             <artifactId>artifactA</artifactId>  
  12.                             <version>1.0</version>  
  13.                             <type>pom</type>  
  14.                             <scope>import</scope>  
  15.                      </dependency>  
  16.               </dependencies>  
  17.        </dependencyManagement>  
  18.        <dependencies>  
  19.               <dependency>  
  20.                      <groupId>test</groupId>  
  21.                      <artifactId>A</artifactId>  
  22.               </dependency>  
  23.               <dependency>  
  24.                      <groupId>test</groupId>  
  25.                      <artifactId>D</artifactId>  
  26.                      <scope>runtime</scope>  
  27.               </dependency>  
  28.        </dependencies>  
  29.        ...  
  30.    
  31. </project>  
相关文章
|
5月前
|
Java 应用服务中间件 Maven
Maven简介及配置使用
Maven简介及配置使用
417 0
|
4月前
|
存储 Oracle Java
Maven高级-私服简介与安装及私服仓库分类
Maven高级-私服简介与安装及私服仓库分类
61 0
|
3月前
|
SQL Java 数据库连接
Mybatis之Mybatis简介、搭建Mybatis相关步骤(开发环境、maven、核心配置文件、mapper接口、映射文件、junit测试、log4j日志)
【1月更文挑战第2天】 MyBatis最初是Apache的一个开源项目iBatis, 2010年6月这个项目由Apache Software Foundation迁移到了Google Code。随着开发团队转投Google Code旗下,iBatis3.x正式更名为MyBatis。代码于2013年11月迁移到Github iBatis一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。iBatis提供的持久层框架包括SQL Maps和Data Access Objects(DAO)
197 3
Mybatis之Mybatis简介、搭建Mybatis相关步骤(开发环境、maven、核心配置文件、mapper接口、映射文件、junit测试、log4j日志)
|
18天前
|
Java Maven
maven:the pom for XXX is missing, no dependency information available
maven:the pom for XXX is missing, no dependency information available
13 0
|
4月前
|
IDE Java Apache
Maven简介&安装配置及基本使用
Maven简介&安装配置及基本使用
90 0
|
4月前
|
Java 数据管理 Maven
maven的简介及配置使用
maven的简介及配置使用
80 0
|
5月前
|
Java 关系型数据库 Apache
Apache Maven简介&安装及系统坏境配置&eclipse配置Apache Maven---详细介绍
Apache Maven简介&安装及系统坏境配置&eclipse配置Apache Maven---详细介绍
61 0
|
6月前
|
Java 应用服务中间件 测试技术
Maven简介
Maven简介
279 0
|
6月前
|
IDE Java Apache
Maven知识【Maven简介】第一章
Maven知识【Maven简介】第一章
|
8月前
|
Dubbo JavaScript Java
Maven项目中的依赖出现版本冲突,最终发现是对Dependency Scope理解有误
本文记录一下遇到maven依赖版本冲突后的排查过程说明以及问题原因说明
66 0