maven依赖小结

1.依赖元素

[html]  view plain  copy
 
  1. <dependency>  
  2.     <groupId>org.springframework</groupId>  
  3.     <artifactId>spring-core</artifactId>  
  4.     <version>${springframework.version}</version>  
  5.     <type>jar</type>  
  6.     <scope>compile</scope>  
  7. </dependency>  

groupId,必选,实际隶属项目

artifactId,必选,其中的模块

version必选,版本号

type可选,依赖类型,默认jar。 还可以是pom,那就是依赖pom依赖的所有包,还能使war,那就是war包的合并

scope可选,依赖范围,默认compile

optional可选,标记依赖是否可选,默认false

exclusion可选,排除传递依赖性,默认空

2.依赖范围

maven项目又三种classpath(编译,测试,运行)

scope用来表示与classpath的关系,总共有五种

compile:编译,测试,运行

test:测试

provided:编译,测试

runtime:运行

system:编译,测试,同provided,但必须指定systemPath,慎用

3.传递性依赖

顾名思义,你懂的,但是传递的范围会发生改变,这个由maven自身处理,只要理解下即可

第一列为第一依赖,第二列为第二依赖,单元格为传递范围

 

  compile test provided runtime
compile compile _ _ runtime
test test _ _ test
provided provided _ provided provided
runtime runtime _ _ runtime

4.依赖调解

传递路径长度取最短原则,传递路径长度相等时,采取最先申明原则

5.可选依赖

尽量少用,可选依赖不会被传递,需要显式申明

6.排除依赖

发现依赖包里有些包不稳定,可以排除依赖,显式的申明文档的包

[html]  view plain  copy
 
  1. <dependency>  
  2.     <groupId>javax.mail</groupId>  
  3.     <artifactId>mail</artifactId>  
  4.     <version>1.4.1</version>  
  5.     <exclusions>  
  6.         <exclusion>  
  7.             <groupId>javax.activation</groupId>  
  8.             <artifactId>activation</artifactId>  
  9.         </exclusion>  
  10.     </exclusions>  
  11. </dependency>  
  12. <dependency>  
  13.     <groupId>javax.activation</groupId>  
  14.     <artifactId>activation</artifactId>  
  15.     <version>1.1</version>  
  16. </dependency>  

7.分类依赖

当同一个模块,所依赖的几个模块版本都相同时,可以使用maven里的属性做分类依赖,依赖版本升级时改一处即可

[html]  view plain  copy
 
  1. <properties>  
  2.     <springframework.version>2.5.6</springframework.version>  
  3. </properties>  
  4. <dependencies>  
  5.     <dependency>  
  6.         <groupId>org.springframework</groupId>  
  7.         <artifactId>spring-core</artifactId>  
  8.         <version>${springframework.version}</version>  
  9.         <type>jar</type>  
  10.         <scope>compile</scope>  
  11.     </dependency>  
  12.     <dependency>  
  13.         <groupId>org.springframework</groupId>  
  14.         <artifactId>spring-beans</artifactId>  
  15.         <version>${springframework.version}</version>  
  16.         <type>pom</type>  
  17.         <scope>compile</scope>  
  18.     </dependency>  
  19.     <dependency>  
  20.         <groupId>org.springframework</groupId>  
  21.         <artifactId>spring-context</artifactId>  
  22.         <version>${springframework.version}</version>  
  23.         <type>jar</type>  
  24.         <scope>compile</scope>  
  25.     </dependency>  
  26.     <dependency>  
  27.         <groupId>org.springframework</groupId>  
  28.         <artifactId>spring-context-support</artifactId>  
  29.         <version>${springframework.version}</version>  
  30.         <type>jar</type>  
  31.         <scope>compile</scope>  
  32.     </dependency>  
  33. </dependencies>  

猜你喜欢

转载自xiaoxiaoher.iteye.com/blog/2421999