前言

项目中 jar 包的加载一般有这么几个阶段:编译、测试、运行,实际项目构建过程中 jar 包应该能够满足不同的阶段有选择性地加载使用的需求,Maven 通过 scope 配置来满足这个需求。

分类

compile(默认)

在编译、测试和运行时都要使用该依赖 jar 包。

provided

编译和测试有效,运行无效。如 servlet-api,在项目运行时,tomcat 等容器已经提供,无需Maven 重复引入。

runtime

测试和运行有效,编译无效。如 jdbc 驱动实现,编译时只需接口,测试或运行时才需要具体的 jdbc 驱动实现。

test

只对测试有效,表明只在测试的时候需要,在编译和运行时将无法使用该类依赖 jar 包,如 junit。

system

被依赖 jar 不会从 maven 仓库中查找,而是从本地系统中获取,需要和 systemPath 元素一起使用,用于指定本地系统中 jar 文件的路径。

import

只在 <dependencyManagement> 中使用,表示从其它的 pom 中导入 dependency 的配置,这样可以简化配置导入的复杂度,同时实现 jar 包版本的统一管理等。下面经常遇到的使用 import 的一个实例:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.1.3.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
 
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

参考

https://maven.apache.org/guid...


阿白
6 声望0 粉丝

引用和评论

0 条评论