Skip to content

Maven 依赖中的 scope 选项

🏷️ Maven

Dependency scope is used to limit the transitivity of a dependency, and also to affect the classpath used for various build tasks.

xml
<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>6.2.2.jre8</version>
    <scope>test</scope>
</dependency>

scope 共有 6 个可选值

  • compile
    默认的 scope,表示 dependency 都可以在生命周期中使用。而且,这些 dependencies 会传递到依赖的项目中。适用于所有阶段,会随着项目一起发布。

    This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.

  • provided
    compile 相似,但是表明了 dependencyJDK 或者容器提供,例如 Servlet API 和一些 Java EE APIs。这个 scope 只能作用在编译和测试时,同时没有传递性。????????

    This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.

  • runtime
    表示 dependency 不作用在编译时,但会作用在运行和测试时,如 JDBC 驱动,适用运行和测试阶段。

    This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.

  • test
    表示 dependency 作用在测试时,不作用在运行时。只在测试时使用,用于编译和运行测试代码。不会随项目发布。

    This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases. This scope is not transitive.

  • system
    provided 相似,但是在系统中要以外部 JAR 包的形式提供,maven 不会在 repository 查找它。

    This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.

  • import (only available in Maven 2.0.9 or later)

    This scope is only supported on a dependency of type pom in the <dependencyManagement> section. It indicates the dependency to be replaced with the effective list of dependencies in the specified POM's <dependencyManagement> section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

scope 的依赖传递

A–>B–>C。当前项目为 A,A 依赖于 B,B 依赖于 C。知道 B 在 A 项目中的 scope,那么怎么知道 C 在 A 中的 scope 呢?答案是:
当 C 是 test 或者 provided 时,C 直接被丢弃,A 不依赖 C;
否则 A 依赖 C,C 的 scope 继承于 B 的 scope。

Direct Scope vs. Transitive Scope

Each of the scopes (except for import) affects transitive dependencies in different ways, as is demonstrated in the table below. If a dependency is set to the scope in the left column, transitive dependencies of that dependency with the scope across the top row will result in a dependency in the main project with the scope listed at the intersection. If no scope is listed, it means the dependency will be omitted.

compileprovidedruntimetest
compilecompile(*)-runtime-
providedprovided-provided-
runtimeruntime-runtime-
testtest-test-

参考

  1. Maven 依赖 Scope 标签用法
  2. Introduction to the Dependency Mechanism
  3. Maven
  4. Maven 依赖中的 scope 详解