一、maven 的依赖
1.依赖的传递性
A依赖B,B依赖C,那么A依赖C
好处:可以传递,不必在每个模块工程中都重复声明,在"最下面"的工程中依赖一次即可
注意:非compile范围的依赖不能传递,所以在各个工程模块中,如果有需要就得重复声明依赖
2.依赖的排除
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
3.依赖的原则
解决模块之间jar包冲突问题
- 路径最短者优先
- 路径相同时先声明者优先
4.统一管理版本号
1) 使用properties标签内使用自定义标签统一声明版本号
<properties>
<junit-version>4.12</junit-version>
</properties>
2) 在需要统一版本的位置,使用${自定义标签名}引用声明的版本号
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
二、maven 的继承
目的:统一管理各个模块工程中依赖的版本
例如:将junit依赖版本统一提取到父工程中,在子工程中声明junit依赖时不指定版本,以父工程中统一设定为准,同时也便于修改
步骤:
- 创建一个maven工程作为父工程,注意:打包的方式pom
- 在子工程中声明父工程的引用
- 在子工程的坐标中与父工程坐标中重复的内容删除
- 在父工程中统一junit的依赖
- 在子工程中删除junit依赖的版本号部分
注意: 配置继承后,执行安装命令时要先安装父工程
父工程pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- 配置依赖的管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
子工程pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 子工程中声明父工程 -->
<parent>
<groupId>org.example</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 以当前文件为基准的父工程pom.xml文件的相对路径 -->
<relativePath>../parent/pom.xml</relativePath>
</parent>
<artifactId>child</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
三、maven 的聚合
作用: 一键安装各个模块工程
配置方式: 在一个"总的聚合工程"中配置参与聚合的模块
使用方式: 在聚合工程的pom.xml上 maven install
父工程pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- 配置聚合 -->
<modules>
<!-- 指定各个子工程相对路径 -->
<module>../child</module>
</modules>
<!-- 配置依赖的管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
四、依赖分析
父模块,dependencyManagement 只定义版本号,不作为依赖查找;父模块的 modules 可分析依赖关系
例如:
父pom位置:parent/pom.xml
子pom位置:parent/child1/pom.xml、parent/child2/pom.xml、parent/child3/pom.xml、parent/child4/pom.xml
parent 模块:
<groupId>org.example</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>child1</module>
<module>child2</module>
<module>child3</module>
<module>child4</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>child2</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
</dependencyManagement>
child1 模块:
<parent>
<groupId>org.example</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>child1</artifactId>
<version>2.0</version>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>child2</artifactId>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>child3</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
child2 模块:
<parent>
<groupId>org.example</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>child2</artifactId>
<version>2.0</version>
child3 模块:
<parent>
<groupId>org.example</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>child3</artifactId>
<version>2.0</version>
child4 模块:
<parent>
<groupId>org.example</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>child4</artifactId>
<version>2.0</version>
1.编译全模块
zxm@zxm-pc:~/IdeaProjects/parent$ mvn -T 1C clean package -DskipTests
[INFO] parent 1.0 ......................................... SUCCESS [ 0.067 s]
[INFO] child2 2.0 ......................................... SUCCESS [ 0.340 s]
[INFO] child3 2.0 ......................................... SUCCESS [ 0.340 s]
[INFO] child1 2.0 ......................................... SUCCESS [ 0.013 s]
[INFO] child4 2.0 ......................................... SUCCESS [ 0.340 s]
2.编译 child1,并编译其依赖
zxm@zxm-pc:~/IdeaProjects/parent$ mvn -T 1C clean package -DskipTests -pl child1 -am
[INFO] parent 1.0 ......................................... SUCCESS [ 0.078 s]
[INFO] child2 2.0 ......................................... SUCCESS [ 0.334 s]
[INFO] child3 2.0 ......................................... SUCCESS [ 0.334 s]
[INFO] child1 2.0 ......................................... SUCCESS [ 0.012 s]
3.只编译 child1
zxm@zxm-pc:~/IdeaProjects/parent$ mvn -T 1C clean package -DskipTests -pl child1
[INFO] BUILD FAILURE
[ERROR] Failed to execute goal on project child1: Could not resolve dependencies for project org.example:child1:jar:2.0: The following artifacts could not be resolved: org.example:child2:jar:2.0, org.example:child3:jar:2.0: Could not find artifact org.example:child2:jar:2.0 in aliyun (https://maven.aliyun.com/repository/public) -> [Help 1]
或
zxm@zxm-pc:~/IdeaProjects/parent/child1$ mvn -T 1C clean package -DskipTests
[ERROR] Failed to execute goal on project child1: Could not resolve dependencies for project org.example:child1:jar:2.0: The following artifacts could not be resolved: org.example:child2:jar:2.0, org.example:child3:jar:2.0: org.example:child2:jar:2.0 was not found in https://maven.aliyun.com/repository/public during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of aliyun has elapsed or updates are forced -> [Help 1]