Maven 中的scope含义

There are 6 scopes:

  • compile : 这是默认范围,如果未指定则使用。 编译依赖项在项目的所有类路径中都可用。 此外,这些依赖关系会传播到依赖项目。

    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 ,在运行时提供依赖项。 例如,在为 Java Enterprise Edition 构建 Web 应用程序时,您可以将 Servlet API 和相关 Java EE API 的依赖关系设置为提供的范围,因为 Web 容器提供了这些类。 具有此范围的依赖项将添加到用于编译和测试的类路径,但不会添加到运行时类路径。 它不具有传递性。

    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. A dependency with this scope is added to the classpath used for compilation and test, but not the runtime classpath. It is not transitive.

  • runtime : 此范围表示编译不需要依赖项,但执行需要依赖项。 Maven 在运行时和测试类路径中包含对此范围的依赖项,但在编译类路径中不包含此范围。

    This scope indicates that the dependency is not required for compilation, but is for execution. Maven includes a dependency with this scope in the runtime and test classpaths, but not the compile classpath.

  • test : 此范围表示应用程序的正常使用不需要该依赖项,并且仅在测试编译和执行阶段可用。 这个范围是不可传递的。 通常,此范围用于 JUnit 和 Mockito 等测试库。 它也可用于非测试库,例如 Apache Commons IO,如果这些库用于单元测试 (src/test/java) 但不在模型代码 (src/main/java) 中使用。

    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. Typically this scope is used for test libraries such as JUnit and Mockito. It is also used for non-test libraries such as Apache Commons IO if those libraries are used in unit tests (src/test/java) but not in the model code (src/main/java).

  • system : 此范围与 provided 的范围类似,只是您必须显式提供包含它的 JAR。 该工件始终可用,并且不会在存储库中查找。

    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 : 仅<dependencyManagement> 部分中 pom 类型的依赖项支持此范围。 它指示依赖项将替换为指定 POM 的 <dependencyManagement> 部分中的有效依赖项列表。 由于它们被替换,具有导入范围的依赖项实际上并不参与限制依赖项的传递性。

    This scope is only supported on a dependency of type pom in the <dependencyManagement> section. It indicates the dependency is 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.

每个范围(导入除外)都会以不同的方式影响传递依赖项,如下表所示。 如果将依赖项设置为左列中的范围,则该依赖项与顶行范围内的传递依赖项会导致主项目中的依赖项与交叉点处列出的范围相关。 如果没有列出范围,则意味着省略了依赖关系。

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, a transitive dependency of that dependency with the scope across the top row results in a dependency in the main project with the scope listed at the intersection. If no scope is listed, it means the dependency is omitted.

scope compile provided runtime test
compile compile(*) - runtime -
provided provided - provided -
runtime runtime - runtime -
test test - test -

(*) 注意:这应该是运行时范围,因此必须显式列出所有编译依赖项。 但是,如果您依赖的库从另一个库扩展了一个类,则两者都必须在编译时可用。 因此,即使编译时依赖项是可传递的,编译时依赖项仍保留为编译范围。 * 简单理解就是 - compile 指的是 编译可用、运行、测试可用、打包需包含 - provided 指的是 编译可用、运行、测试可用、打包不包含 - runtime 指的是 编译不可用、运行、测试可用、打包不包含 - test

Note: it is intended that this should be runtime scope instead, so that all compile dependencies must be explicitly listed. However, if a library you depend on extends a class from another library, both must be available at compile time. For this reason, compile time dependencies remain as compile scope even when they are transitive.