`
234390216
  • 浏览: 10193561 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
博客专栏
A5ee55b9-a463-3d09-9c78-0c0cf33198cd
Oracle基础
浏览量:460809
Ad26f909-6440-35a9-b4e9-9aea825bd38e
springMVC介绍
浏览量:1771817
Ce363057-ae4d-3ee1-bb46-e7b51a722a4b
Mybatis简介
浏览量:1395439
Bdeb91ad-cf8a-3fe9-942a-3710073b4000
Spring整合JMS
浏览量:393893
5cbbde67-7cd5-313c-95c2-4185389601e7
Ehcache简介
浏览量:678226
Cc1c0708-ccc2-3d20-ba47-d40e04440682
Cas简介
浏览量:529302
51592fc3-854c-34f4-9eff-cb82d993ab3a
Spring Securi...
浏览量:1178743
23e1c30e-ef8c-3702-aa3c-e83277ffca91
Spring基础知识
浏览量:461913
4af1c81c-eb9d-365f-b759-07685a32156e
Spring Aop介绍
浏览量:150147
2f926891-9e7a-3ce2-a074-3acb2aaf2584
JAXB简介
浏览量:66858
社区版块
存档分类
最新评论

Maven简介(六)——Dependency

阅读更多

7      Dependency介绍

7.1     依赖的传递性

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

依赖传递对版本的选择

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

使用exclusion排除依赖

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

<dependencies>
       <dependency>
              <groupId>groupB</groupId>
              <artifactId>artifactB</artifactId>
              <version>1.0</version>
              <exclusions>
                     <exclusion>
                            <groupId>groupC</groupId>
                            <artifactId>artifactC</artifactId>
                     </exclusion>
              </exclusions>
       </dependency>
       ...
</dependencies>

 

可选的依赖项

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

<project>
       <groupId>groupB</groupId>
       <artifactId>artifactB</artifactId>
       <version>1.0</version>
       <dependencies>
              <dependency>
                     <groupId>groupC</groupId>
                     <artifactId>artifactC</artifactId>
                     <version>1.0</version>
                     <optional>true</optional>
              </dependency>
              ...
       </dependencies>
</project>
 
<project>
       <groupId>groupA</groupId>
       <artifactId>artifactA</artifactId>
       <version>1.0</version>
       <dependencies>
              <dependency>
                     <groupId>groupB</groupId>
                     <artifactId>artifactB</artifactId>
                     <version>1.0</version>
              </dependency>
              ...
       </dependencies>
</project>

 

7.2     依赖项的作用域

在定义项目的依赖项的时候,我们可以通过scope来指定该依赖项的作用范围。scope的取值有compileruntimetestprovidedsystemimport

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

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

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

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

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

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>sun.jdk</groupId>
      <artifactId>tools</artifactId>
      <version>1.5.0</version>
      <scope>system</scope>
      <systemPath>${java.home}/../lib/tools.jar</systemPath>
    </dependency>
  </dependencies>
  ...
</project>

 

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

7.3     dependencyManagement介绍

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

7.3.1集中管理依赖项

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

projectA

<project>
       <groupId>groupA</groupId>
       <artifactId>artifactA</artifactId>
       <version>1.0</version>
       <dependencies>
              <dependency>
                     <groupId>groupC</groupId>
                     <artifactId>artifactC</artifactId>
                     <version>1.0</version>
              </dependency>
              <dependency>
                     <groupId>groupD</groupId>
                     <artifactId>artifactD</artifactId>
                     <version>1.0</version>
              </dependency>
       </dependencies>
       ...
</project>

 

       ProjectB

<project>
       <groupId>groupB</groupId>
       <artifactId>artifactB</artifactId>
       <version>1.0</version>
       <dependencies>
              <dependency>
                     <groupId>groupC</groupId>
                     <artifactId>artifactC</artifactId>
                     <version>1.0</version>
              </dependency>
              <dependency>
                     <groupId>groupE</groupId>
                     <artifactId>artifactE</artifactId>
                     <version>1.0</version>
                     <type>bar</type>
              </dependency>
       </dependencies>
       ...
</project>

 

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

       projectP

<project>
       ...
       <dependencyManagement>
              <dependencies>
                     <dependency>
                            <groupId>groupC</groupId>
                            <artifactId>artifactC</artifactId>
                            <version>1.0</version>
                     </dependency>
                     <dependency>
                            <groupId>groupD</groupId>
                            <artifactId>artifactD</artifactId>
                            <version>1.0</version>
                     </dependency>
                     <dependency>
                            <groupId>groupE</groupId>
                            <artifactId>artifactE</artifactId>
                            <version>1.0</version>
                            <type>bar</type>
                     </dependency>
              </dependencies>
       </dependencyManagement>
       ...
</project>

 

       之后我们就可以这样来定义我们的projectAprojectB

projectA

<project>
       <modelVersion>4.0</modelVersion>
       <groupId>groupA</groupId>
       <artifactId>artifactA</artifactId>
       <version>1.0</version>
       <dependencies>
              <dependency>
                     <groupId>groupC</groupId>
                     <artifactId>artifactC</artifactId>
              </dependency>
              <dependency>
                     <groupId>groupD</groupId>
                     <artifactId>artifactD</artifactId>
              </dependency>
       </dependencies>
       ...
</project>

 

 

       ProjectB

<project>
       <modelVersion>4.0</modelVersion>
       <groupId>groupB</groupId>
       <artifactId>artifactB</artifactId>
       <version>1.0</version>
       <dependencies>
              <dependency>
                     <groupId>groupC</groupId>
                     <artifactId>artifactC</artifactId>
              </dependency>
              <dependency>
                     <groupId>groupE</groupId>
                     <artifactId>artifactE</artifactId>
              <!--因为artifactE的类型不是默认的jar,所以这里需要指定依赖项的类型-->
                     <type>bar</type>
              </dependency>
       </dependencies>
       ...
</project>

 

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

7.3.2控制依赖项使用的版本

看下面这样一种情况

projectA

<project>
       <modelVersion>4.0</modelVersion>
<groupId>groupA</groupId>
       <artifactId>artifactA</artifactId>
       <version>1.0</version>
       <packaging>pom</packaging>
       <dependencyManagement>
              <dependencies>
                     <dependency>
                            <groupId>groupA</groupId>
                            <artifactId>A</artifactId>
                            <version>1.5</version>
                     </dependency>
                     <dependency>
                            <groupId>groupA</groupId>
                            <artifactId>B</artifactId>
                            <version>1.0</version>
                     </dependency>
                     <dependency>
                            <groupId>groupA</groupId>
                            <artifactId>C</artifactId>
                            <version>1.0</version>
                     </dependency>
                     <dependency>
                            <groupId>groupA</groupId>
                            <artifactId>D</artifactId>
                            <version>1.6</version>
                     </dependency>
              </dependencies>
       </dependencyManagement>
       ...
</project>

 

 

       ProjectB

<project>
       <modelVersion>4.0</modelVersion>
       <groupId>groupB</groupId>
       <artifactId>artifactB</artifactId>
       <version>1.0</version>
       <packaging>pom</packaging>
       <dependencyManagement>
              <dependencies>
                     <dependency>
                            <groupId>groupA</groupId>
                            <artifactId>A</artifactId>
                            <version>1.0</version>
                     </dependency>
              </dependencies>
       </dependencyManagement>
       <dependencies>
              <dependency>
                     <groupId>groupA</groupId>
                     <artifactId>B</artifactId>
                     <version>1.8</version>
              </dependency>
              <dependency>
                     <groupId>groupA</groupId>
                     <artifactId>C</artifactId>
                     <scope>runtime</scope>
              </dependency>
       </dependencies>
       ...
</project>

 

       这样,当我们对projectB进行Maven操作的时候,依赖项ABCD将会如下选择:

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

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

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

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

7.4     引入依赖项

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

<project>
       ...
       <groupId>groupA</groupId>
       <artifactId>artifactA</artifactId>
       <version>1.0</version>
       <packaging>pom</packaging>
       <dependencyManagement>
              <dependencies>
                     <dependency>
                            <groupId>test</groupId>
                            <artifactId>A</artifactId>
                            <version>1.0</version>
                     </dependency>
                     <dependency>
                            <groupId>test</groupId>
                            <artifactId>B</artifactId>
                            <version>1.1</version>
                     </dependency>
                     <dependency>
                            <groupId>test</groupId>
                            <artifactId>C</artifactId>
                            <version>1.2</version>
                     </dependency>
                     <dependency>
                            <groupId>test</groupId>
                            <artifactId>D</artifactId>
                            <version>1.3</version>
                     </dependency>
              </dependencies>
       </dependencyManagement>
       ...
</project>

 

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

<project>
       ...
       <parent>
              <groupId>groupA</groupId>
              <artifactId>artifactA</artifactId>
              <version>1.0</version>
       </parent>
       <groupId>groupA</groupId>
       <artifactId>artifactB</artifactId>
       <version>1.0</version>
       <packaging>pom</packaging>
       <dependencies>
              <dependency>
                     <groupId>test</groupId>
                     <artifactId>A</artifactId>
              </dependency>
              <dependency>
                     <groupId>test</groupId>
                     <artifactId>D</artifactId>
                     <scope>runtime</scope>
              </dependency>
       </dependencies>
       ...
</project>

 

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

<project>
       ...
       <groupId>groupA</groupId>
       <artifactId>artifactB</artifactId>
       <version>1.0</version>
       <packaging>pom</packaging>
       <dependencyManagement>
              <dependencies>
                     <dependency>
                            <groupId>groupA</groupId>
                            <artifactId>artifactA</artifactId>
                            <version>1.0</version>
                            <type>pom</type>
                            <scope>import</scope>
                     </dependency>
              </dependencies>
       </dependencyManagement>
       <dependencies>
              <dependency>
                     <groupId>test</groupId>
                     <artifactId>A</artifactId>
              </dependency>
              <dependency>
                     <groupId>test</groupId>
                     <artifactId>D</artifactId>
                     <scope>runtime</scope>
              </dependency>
       </dependencies>
       ...
 
</project>

 

分享到:
评论
1 楼 liu765023051 2016-04-10  
总结的很好,感谢!

相关推荐

Global site tag (gtag.js) - Google Analytics