佳佳的博客
Menu
首页
Maven 依赖中的 scope 选项
Posted by
佳佳
on 2018-08-29
IT
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` 相似,但是表明了 *dependency* 由 *JDK* 或者容器提供,例如 *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. | | compile | provided | runtime | test | | --- | --- | --- | --- | --- | | compile | compile(*) | - | runtime | - | | provided | provided | - | provided | - | | runtime | runtime | - | runtime | - | | test | test | - | test | - | ## 参考 1. [Maven依赖Scope标签用法](https://www.cnblogs.com/telwanggs/p/7064527.html) 2. [Introduction to the Dependency Mechanism](https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html) 3. [Maven](https://maven.apache.org/ref/3.5.4/maven-model/maven.html) 4. [Maven依赖中的scope详解](https://blog.csdn.net/kimylrong/article/details/50353161)
版权声明:原创文章,未经允许不得转载。
https://www.liujiajia.me/2018/8/29/maven-依赖中的-scope-选项
“Buy me a nongfu spring”
« 【IDEA】Error occurred during initialization of VM
Spring Boot 中统一配置日期型字段的序列化(格式+默认时区) »
昵称
*
电子邮箱
*
回复内容
*
(回复审核后才会显示)
提交
目录
AUTHOR
刘佳佳
江苏 - 苏州
软件工程师
梦嘉集团