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.0和A->C->D2.0。
深度一样时,根据声明顺序确定。先声明B,则用D1.0。
使用exclusion排除依赖
A->B->C,如果A不需要依赖C:
- <dependencies>
- <dependency>
- <groupId>groupB</groupId>
- <artifactId>artifactB</artifactId>
- <version>1.0</version>
- <exclusions>
- <exclusion>
- <groupId>groupC</groupId>
- <artifactId>artifactC</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- ...
- </dependencies>
依赖项的作用域
scope的取值有compile、runtime、test、provided、system和import。
compile:scope默认值。在所有情况下都是有效的,包括运行、测试和编译时。
runtime:表示该依赖项只有在运行时才是需要的,在编译的时候不需要。这种类型的依赖项将在运行和test的类路径下可以访问。
test:表示该依赖项只对测试时有用,包括测试代码的编译和运行,对于正常的项目运行是没有影响的。
provided:表示该依赖项将由JDK或者运行容器在运行时提供,也就是说由Maven提供的该依赖项我们只有在编译和测试时才会用到,而在运行时将由JDK或者运行容器提供。
system:当scope为system时,
三 、dependencyManagement介绍
dependencyManagement主要有两个作用,一个是集中管理项目的依赖项,另一个就是控制使用的依赖项的版本。不必指定其对应的版本和作用范围。子类和父类都有指定版本和范围,使用子类的。
- <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>
子工程引用
- <dependencies>
- <dependency>
- <groupId>groupC</groupId>
- <artifactId>artifactC</artifactId>
- </dependency>
- <dependency>
- <groupId>groupD</groupId>
- <artifactId>artifactD</artifactId>
- </dependency>
- </dependencies>
七、引入依赖
子项目可以使用父项目中定义在<dependencyManagement>中的依赖。但是单继承的。如果不通过继承来使用,就要用到scope.- <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
TAG: