Dependency介绍

上一篇 / 下一篇  2015-06-16 20:55:38 / 个人分类:接口测试

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

依赖传递对版本的选择

A-->B-->D-->E1.0,A-->C-->E2.0。这样会导致依赖冲突。

依赖首先选择深度较短,即E2.0依赖。

A->B->D1.0A->C->D2.0

深度一样时,根据声明顺序确定。先声明B,则用D1.0。

使用exclusion排除依赖

A->B->C,如果A不需要依赖C

  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> 
  15. 依赖项的作用域

    scope的取值有compileruntimetestprovidedsystemimport

    compilescope默认值在所有情况下都是有效的,包括运行、测试和编译时。

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

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

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

    system:当scopesystem时,

    三 、dependencyManagement介绍

    dependencyManagement主要有两个作用,一个是集中管理项目的依赖项,另一个就是控制使用的依赖项的版本。不必指定其对应的版本和作用范围。子类和父类都有指定版本和范围,使用子类的。

    1.   <dependencyManagement>  
    2.               <dependencies>  
    3.                      <dependency>  
    4.                             <groupId>groupC</groupId>  
    5.                             <artifactId>artifactC</artifactId>  
    6.                             <version>1.0</version>  
    7.                      </dependency>  
    8.                      <dependency>  
    9.                             <groupId>groupD</groupId>  
    10.                             <artifactId>artifactD</artifactId>  
    11.                             <version>1.0</version>  
    12.                      </dependency>  
    13.                      <dependency>  
    14.                             <groupId>groupE</groupId>  
    15.                             <artifactId>artifactE</artifactId>  
    16.                             <version>1.0</version>  
    17.                             <type>bar</type>  
    18.                      </dependency>  
    19.               </dependencies>  
    20.        </dependencyManagement>  

    子工程引用

    1.   <dependencies>  
    2.               <dependency>  
    3.                      <groupId>groupC</groupId>  
    4.                      <artifactId>artifactC</artifactId>  
    5.               </dependency>  
    6.               <dependency>  
    7.                      <groupId>groupD</groupId>  
    8.                      <artifactId>artifactD</artifactId>  
    9.               </dependency>  
    10.        </dependencies>  

    七、引入依赖

    子项目可以使用父项目中定义在<dependencyManagement>中的依赖。但是单继承的。

    如果不通过继承来使用,就要用到scope.
    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

TAG:

 

评分:0

我来说两句

Open Toolbar