坐标可以唯一定义一个Maven构件,包括
1.groupId
2.artifactId
3.version
4.packaging
5.classifier
依赖范围
complie
test
provided
runtime
system
import
依赖范围 | 对编译classpath有效 | 对测试classpath有效 | 对运行时classpath有效 | 例子 |
complie | Y | Y | Y | spring-core |
test | - | Y | - | Junit |
provided | Y | Y | - | servlet-api |
runtime | - | Y | Y | jdbc driver |
system | - | Y | Y | local lib |
下表第一列表示第一直接依赖范围,第一行表示第二直接依赖范围,交叉单元格表示传递依赖范围
complie | test | provided | runtime | |
complie | complie | - | - | runtime |
test | test | - | - | test |
provided | provided | - | provided | provided |
runtime | runtime | - | - | runtime |
依赖调解
原则一、最短路径优先
原则二、声明顺序优先
可选依赖
可选依赖不会传递,如需使用,需要单独声明。
可选依赖不推荐使用,属于不好的实践,建议将每个依赖单独做成maven构件,由使用者决定依赖
排除依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>2.5.6</version> <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency>
归类依赖
<dependencies> <dependency> <groupId>org.spring.framework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.spring.framework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.spring.framework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.spring.framework</groupId> <artifactId>spring-mock</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <properties> <spring.version>2.5</spring.version> </properties>
优化依赖 mvn dependency:list mvn dependency:tree mvn dependency:analyze