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

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

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

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

2.依赖范围

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

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

compile:编译,测试,运行

test:测试

provided:编译,测试

runtime:运行

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

3.传递性依赖

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

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

compiletestprovidedruntime
compilecompile__runtime
testtest__test
providedprovided_providedprovided
runtimeruntime__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>

8.优化依赖

可概括为三个命令

mvn dependency:list

表示依赖列表,maven eclipse插件已经实现,有图形化显示,在pom.xml的dependencies页


mvn dependency:tree

表示依赖列表,maven eclipse插件已经实现,有图形化显示,在pom.xml的dependency hierarchy页

mvn dependency:analyze

查找出在编译和测试中未使用但显示声明的依赖

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值