Maven的依赖传递、依赖管理、依赖作用域

发布于:2024-06-24 ⋅ 阅读:(18) ⋅ 点赞:(0)

在Maven项目中通常会引入大量依赖,但依赖管理不当,会造成版本混乱冲突或者目标包臃肿。因此,我们以SpringBoot为例,从三方面探索依赖的使用规则。

1、 依赖传递

依赖是会传递的,依赖的依赖也会连带引入。例如在项目中引入了spring-boot-starter-web依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  	<version>2.7.18</version>
</dependency>

那么业务项目不仅直接引入了spring-boot-starter-web依赖,还间接引入了spring-boot-starter-web的依赖项:

spring-boot-starterspring-boot-starter-jsonspring-boot-starter-tomcatspring-webspring-webmvc

Maven依赖关系如下图所示:

其中,spring-boot-starter-web是直接依赖,spring-boot-starter-web的依赖项:spring-boot-starterspring-boot-starter-jsonspring-boot-starter-tomcatspring-webspring-webmvc为间接依赖。

2、依赖管理

2.1、dependencyManagement

maven中的dependencyManagement节点主要用来统一管理依赖项的版本号。如果依赖中,未指定版本号,则采用dependencyManagement引入的pom文件中的版本号。dependencyManagement只用来指定依赖的版本,不会真正引入依赖。

假如pom文件中引入了如下依赖:  

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>3.3.1</version>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

那么在引入具体依赖时,可以不指定版本号:

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
</dependency>

此时, spring-boot-starter-web会自动引入spring-boot-dependencies中指定的版本号3.3.1

2.2、parent

Maven 借鉴了面向对象中的继承思想,提出了 POM 继承思想。一个项目可通过继承父模块的 POM 来获得对相关依赖的声明。其目的是为了消除子模块 POM 中的重复配置,其中不包含有任何实际代码,因此父模块 POM 的打包类型(packaging)必须是 pom。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.18</version>
</parent>

此时,对于spring-boot-starter-web依赖,会自动引用spring-boot-starter-parent中指定的版本号2.7.18

2.3、dependency

对于普通的依赖,大家用的最多的也便是这种,在声明依赖时直接指定版本号:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
	<version>2.6.15</version>
</dependency>

这种方式最直观,但依赖过多时,不便于统一管理。 

2.4、依赖优先级

如果我们将上述三种方式混合使用,放在同一个项目中,且指定了不同的版本号,会产生什么效果呢?

 先展示结果如下图:

可以看到直接依赖spring-boot-starter-web的版本为2.6.15,但间接依赖spring-******的版本均为2.7.18。所以结论如下:

优先级:dependence指定版本 > parent指定版本 > dependencyManagement指定版本

dependence指定版本仅限于直接依赖的版本,间接依赖仍然采用parent所指定的版本,dependencyManagement指定的版本最次之。

3、 依赖作用域

在Maven中,可以使用scope来指定当前依赖项的作用域,常见的值有:compile、provided、runtime、test、import等。

3.1 compile

compile也是默认的作用域,如果引入依赖时,没有明确指定作用域,则依赖作用域为compile。

compile作用域的依赖,在编译、测试和运行时均有效,并参与项目的打包过程,该依赖会传递给依赖该模块的其他模块。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <version>2.6.15</version>
    <scope>compile</scope>
</dependency>

3.2 provided

provided作用域的依赖,仅在编译和测试时有效,在运行时不可用,且不会参与项目的打包过程,也不会传递给引用项目。

例如,Lombok模块仅用于编译时生成相应的Contructor、Getter、Setter、ToString等方法,但在运行时并不需要这个依赖,因此通常指定为provided,打包后的目标文件中也不包含该依赖:

<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<scope>provided</scope>
</dependency>

3.3 runtime

runtime作用域的依赖,在测试和运行时是可用的,但在编译时是不可用的,也会参与项目的打包过程,其依赖传递给引用该模块的项目。

runtime作用域的模块在编译时不可用,就说明该引用中的类在java代码中也不能直接使用,否则无法编译通过。

例如,mysql数据库驱动在代码中不需要直接调用,代码通常使用mybatis或者java.sql包下的类。因此引入依赖时,通常指定为runtime,在运行时,该驱动包直接注册到程序中:

<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<version>8.0.33</version>
    <scope>runtime</scope>
</dependency>

3.4 test

test作用域的依赖,仅在测试时可用(测试代码的编译和执行),其不会参与项目的打包过程,也不会传递给其他模块。

这些依赖中的类只能在src/test/java中使用,无法用于src/main/java中的代码。

测试相关的均属于此类,比如:spring-boot-starter-test、junit等:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>

3.5 import

每个项目,一般都会继承自一个父项目,这个父项目可能是公司的一个基础项目,也可能是一个框架项目,比如开头提到的springboot:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.18</version>
</parent>

这个父项目中,父项目的父项目是spring-boot-dependencies,spring-boot-dependencies中会使用dependencyManagement标签对依赖项的版本统一管理,子项目中,可以按需引入dependencyManagement中依赖即可,可以省略版本号。

但是Maven最多只能有一个parent项目,如果把所有的dependencyManagement都加入到parent项目中,又会导致parent项目太臃肿,依赖杂乱不好管理。因此,import作用域便可以解决这个问题。import可以通过非继承的方式批量引入另一个依赖项中。

说明:<scope>import</scope>只能用在dependencyManagement下type为pom的dependency中。

3.6 作用域总结

作用域 编译时 测试时 运行时 打包时 传递性
compile
provided × × ×
runtime ×
test × × × ×
import × × ×