Maven依赖管理
Maven依赖管理
- 在pom.xml中编写
<dependencies>
标签 - 在
<dependencies>
标签中使用<dependency>
引入坐标 - 定义坐标的groupId, artifactId, version
- 点击刷新按钮,做坐标生效
alt+insert导入本地库中的jar包
设置自动刷新
依赖范围
通过设置坐标的依赖范围(scope),可以设置对应jar包的作用范围:编译环境、测试环境、运行环境
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.2</version>
<scope>test</scope>
</dependency>
<scope>
默认值:compile
依赖传递
- 依赖具有传递性
- 直接依赖:在当前项目中通过依赖配置建立的依赖关系
- 间接依赖:被资源的资源如果依赖其他资源,当前项目间接依赖其他资源
- 依赖传递冲突问题
- 路径优先:当依赖中出现相同的资源时,层级越深,优先级越低,层级越浅,优先级越高
- 声明优先:当资源在相同层级被依赖时,配置顺序靠前的覆盖配置顺序靠后的。
- 特殊优先:当同级配置了相同的资源的不同版本,后配置的覆盖先配置的。
可选依赖与排除依赖
-
可选依赖指对外隐藏当前所依赖的资源——不透明
<dependency> <groupId>com.charley</groupId> <artifactId>maven_03_pojo</artifactId> <version>1.0-SNAPSHOT</version> <!--可选依赖时隐藏当前工程所依赖的资源,隐藏后对应资源将不具有依赖传递性--> <optional>false</optional> </dependency>
-
排除依赖指主动断开依赖的资源,被排除的资源无需要指定版本——不需要
<dependency> <groupId>com.charley</groupId> <artifactId>maven_03_pojo</artifactId> <version>1.0-SNAPSHOT</version> <!--排除依赖是隐藏当前资源对应的依赖关系--> <exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency>
-
排除依赖资源仅指定GA即可,无需指定V。