Maven scope 与 Gradle scope
Maven 与 Gradle 依赖项 scope
Maven 和 Gradle 对依赖项的 scope 有所不同。在 Maven 世界中,一个依赖项有 6 种 scope 而 gradle 的 scope 一直在优化调整优化。
maven scope 属性
依赖范围控制哪些依赖在哪些 classpath 中可用,哪些依赖包含在一个应用中。
-
compile (编译)
compile 是默认的范围;如果没有提供一个范围,那该依赖的范围就是编译范围。编译范围依赖在所有的 classpath 中可用,同时它们也会被打包。 -
provided (已提供)
provided 依赖只有在当 JDK 或者一个容器已提供该依赖之后才使用。例如, 如果你开发了一个 web 应用,你可能在编译 classpath 中需要可用的 Servlet API 来编译一个 servlet,但是你不会想要在打包好的 WAR 中包含这个 Servlet API;这个 Servlet API JAR 由你的应用服务器或者 servlet 容器提供。已提供范围的依赖在编译 classpath (不是运行时)可用。它们不是传递性的,也不会被打包。 -
runtime (运行时)
runtime 依赖在运行和测试系统的时候需要,但在编译的时候不需要。比如,你可能在编译的时候只需要 JDBC API JAR,而只有在运行的时候才需要 JDBC
驱动实现。 -
test (测试)
test 范围依赖 在一般的编译和运行时都不需要,它们只有在测试编译和测试运行阶段可用。 -
system (系统)
system 范围依赖与 provided 类似,但是你必须显式的提供一个对于本地系统中 JAR 文件的路径。这么做是为了允许基于本地对象编译,而这些对象是系统类库的一部分。这样的构建应该是一直可用的,Maven 也不会在仓库中去寻找它。如果你将一个依赖范围设置成系统范围,你必须同时提供一个 systemPath 元素。注意该范围是不推荐使用的(建议尽量去从公共或定制的 Maven 仓库中引用依赖) -
import (导入)
import 仅支持在中的类型依赖项上。它表示要在指定的 POM 部分中用有效的依赖关系列表替换的依赖关系。该 scope 类型的依赖项实际上不会参与限制依赖项的可传递性。
gradle 依赖管理 scope(gradle 6.6.1)
-
compile(已弃用)
编译时间依赖性。被取代 implementation。 -
implementation 延伸 compile
implementation 默认的 scope。implementation 的作用域会让依赖在编译和运行时均包含在内,但是不会暴露在类库使用者的编译时。举例,如果我们的类库包含了 gson,那么其他人使用我们的类库时,编译时不会出现 gson 的依赖。 -
compileOnly
仅编译时依赖项,在运行时不使用。编译时可见 -
compileClasspath 延伸 compile, compileOnly, implementation
编译类路径,在编译源代码时使用。由任务使用 compileJava。 -
annotationProcessor
编译期间使用的注释处理器。 -
runtime(不建议使用)扩展 compile
运行时依赖项。被取代 runtimeOnly。 -
runtimeOnly
仅运行时依赖项,运行时可见 -
runtimeClasspath 延伸 runtimeOnly, runtime, implementation
运行时类路径包含实现的元素以及仅运行时元素。 -
testCompile(不建议使用)扩展 compile
编译测试的其他依赖项。被取代 testImplementation。 -
testImplementation 延伸 testCompile, implementation
仅实现测试的依赖项。 -
testCompileOnly
其他依赖项仅用于编译测试,在运行时不使用。 -
testCompileClasspath 延伸 testCompile, testCompileOnly, testImplementation
测试编译类路径,在编译测试源时使用。由任务使用 compileTestJava。 -
testRuntime(不建议使用)扩展 runtime, testCompile
仅用于运行测试的其他依赖项。被取代 testRuntimeOnly。 -
testRuntimeOnly 延伸 runtimeOnly
运行时仅依赖于运行测试。 -
testRuntimeClasspath 延伸 testRuntimeOnly, testRuntime, testImplementation
用于运行测试的运行时类路径。由任务使用 test。 -
archives
Artifacts (e.g. jars) produced by this project. Used by task uploadArchives. -
default 延伸 runtimeClasspath
项目依赖于此项目的默认配置。包含此项目在运行时所需的工件和依赖项
下图分别显示了 main 和 test 的依赖项配置。您可以使用此图例解释颜色:
-
灰色文本 - 已弃用配置。
-
绿色背景 - 您可以声明对配置的依赖关系。
-
蓝灰色背景 - 该配置供任务使用,而不供您声明依赖项。
-
浅蓝色背景与等宽线文本 — — 一个任务。

图一 java 插件 - main 依赖项配置

图二 Java 插件 - test 依赖项配置
解释
maven、gradle 中依赖的 scope 可以简单梳理成 4 个方面
- 支持自身编译
- 支持自身运行
- 支持子项目编译
- 支持子项目运行
以上 4 个方面可以进行各种组合(maven 项目)
- 1:provided(自身编译要用,运行时不需要,因为其它平台会自动提供,如 servlet)
- 2:runtime+option:true(不参与编译,但是运行时对调用,比如方法内部使用的类就不需要参与编译),子项目可以不需要此功能时。这个组合少有
- 1、2:compile+option:true(自身编译运行都要,子项目可以不需要此功能时。这个组合少有)
- 1、3:没有
- 1、4:没有
- 2、3:runtime+sub-provided(应该没有这个搭配)
- 2、4:runtime+(sub-compile or sub-runtime)(此项目和其子项目都只在运行期用上)
- 1、2、3:compile+sub-provided(应该没有)
- 1、2、4:compile+sub-runtime(子项目编译期不需要,运行时会调用)
- 2、3、4:没有
- 1、2、3、4:compile+sub-compile
以上 4 个方面可以进行各种组合(gradle 项目)
- 1:compileOnly 或 compileClasspath
- 2:runtimeClasspath
- 1、2:没有
- 1、3:compileOnlyApi
- 1、4:没有
- 2、3:没有
- 2、4:没有
- 1、2、3:没有
- 1、2、4:implementation(子项目编译期得不到,运行时可以调用,接口实现类!)
- 2、3、4:没有
- 1、2、3、4:api(接口)
gradle 各个 configuration 的继承关系
下图中相关解释:
compileClasspath:本项目编译期可用
runtimeClasspath:本项目运行期可用
runtimeElements:依赖传递到子模块,且只传递到子模块的 运行期
apiElements:依赖传递到子模块,且只传递到子模块的 编译期
其它:就是以上 4 个配置功能的组合了
以下是测试阶段可用的配置
testCompileClasspath:仅用于测试阶段的编译期
testRuntimeClasspath:仅用于测试阶段的运行期
其它:就是以上 2 个配置功能的组合了
前言
一. scope 的类型总览
-
设置依赖范围
- 可以对引入的资源进行设置,设置其生效的范围。生效范围主要有以下:
-
主代码是否有效
- 测试代码是否有效
- 是否参与打包
- 而上述三种不同范围的组合,便有了下标四个 <scope> 对应的值

二。详解
1.compile(默认选项)
表示为当前依赖参与项目的编译、测试和运行阶段,scope 的默认选项。打包之时,会打到包里去。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
</dependency>
2. test
表示为当前依赖只参与测试阶段,打包之时,不会打到包里
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
3. runtime
表示为当前依赖只参与运行阶段,一般这种类库都是接口与实现相分离的类库,如 mysql 的驱动包
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
只有编译会使用,程序运行时不需要依赖的。
案例一:lombok 只在编译时,把 getter、setter 设置好,不需要运行时使用。
案例二:servlet-api 只在编译时,需要引入,运行时由 tomcat 容器提供。
4.privided
表示为当前依赖在打包过程中,不需要打进去,需要由运行的环境来提供,比如 springboot 打 war 包,使用外部 tomcat 启动,而不是使用内置的 tomcat。区别在于打包阶段进行了 exclude 操作。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
5. system
表示为当前依赖不从 maven 仓库获取,从本地系统获取,结合 systempath 使用,常用于无法从 maven 仓库获取的包。
<dependency>
<groupId>com.supermap</groupId>
<artifactId>data</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>
<systemPath>${project.basedir}/lib/projectJarTest-0.0.1-SNAPSHOT.jar</systemPath>
</systemPath>
</dependency>
6. import
这个是 maven2.0.9 版本后出的属性,import 只能在 dependencyManagement 的中使用,能解决 maven 单继承问题,import 依赖关系实际上并不参与限制依赖关系的传递性。
表示为当前项目依赖为多继承关系,常用于项目中自定义父工程,需要注意的是只能用在 dependencyManagement 里面,且仅用于 type=pom 的 dependency。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
例:
如 spring-boot-dependencies 下的依赖可以直接引入,比如下面这些:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-ldap</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
直接引入,不需要指定版本号:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
但是 spring-cloud-dependencies 下的依赖不能直接引入,比如下面这些:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gateway-dependencies</artifactId>
<version>${spring-cloud-gateway.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-consul-dependencies</artifactId>
<version>${spring-cloud-consul.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-dependencies</artifactId>
<version>${spring-cloud-sleuth.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-vault-dependencies</artifactId>
<version>${spring-cloud-vault.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
不能直接引入,如:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gateway-dependencies</artifactId>
</dependency>
因为它里面的依赖都含有
<type>pom</type>
<scope>import</scope>
而是需要看 spring-cloud-gateway-dependencies 的中的包,才能直接引入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>${project.version}</version>
</dependency>
三.optional
optional 为 true 的 话 作 用 是 让 新 项 目 依 赖 本 项 目 的 时 候 , 不 去 依 赖 此 jar 包,设 置 false 或者不设值采用默认, 新 项 目 就 会 能 引 用 到 此 jar 包 compile,缺省值,适用于所有阶段,会随着项目一起发布。 编译范围依赖在所有的 classpath 中可用,同时它们也会被打包
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<optional>true</optional>
<!-- 简单说明下:
这是关键:optional 为 true 的 话 作 用 是 让 新 项 目 依 赖 本 项 目 的 时 候 , 不 去 依 赖 此 jar包,设 置 false或者不设值采用默认, 新 项 目 就 会 能 引 用 到 此 jar 包
compile,缺省值,适用于所有阶段,会随着项目一起发布。 编译范围依赖在所有的classpath 中可用,同时它们也会被打包
-->
</dependency>

浙公网安备 33010602011771号