一:

1.Maven中的依赖作用范围概述

Maven中使用 scope 来指定当前包的依赖范围和依赖的传递性。常见的可选值有:compile, provided, runtime, test, system 等。scope 主要是用在 pom.xml 文件中的依赖定义部分,例如:

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-test</artifactId>

<version>3.2.1.RELEASE</version>

<scope>test</scope>

</dependency>

2.scope各种取值详解

scope取值 有效范围(compile, runtime, test) 依赖传递 例子
compile all spring-core
provided compile, test servlet-api
runtime runtime, test JDBC驱动
test test JUnit
system compile, test  

正如上表所示,

compile :为默认的依赖有效范围。如果在定义依赖关系的时候,没有明确指定依赖有效范围的话,则默认采用该依赖有效范围。此种依赖,在编译、运行、测试时均有效。

provided :在编译、测试时有效,但是在运行时无效。例如:servlet-api,运行项目时,容器已经提供,就不需要Maven重复地引入一遍了。

runtime :在运行、测试时有效,但是在编译代码时无效。例如:JDBC驱动实现,项目代码编译只需要JDK提供的JDBC接口,只有在测试或运行项目时才需要实现上述接口的具体JDBC驱动。

test :只在测试时有效,例如:JUnit。

system :在编译、测试时有效,但是在运行时无效。和provided的区别是,使用system范围的依赖时必须通过systemPath元素显式地指定依赖文件的路径。由于此类依赖不是通过Maven仓库解析的,而且往往与本机系统绑定,可能造成构建的不可移植,因此应该谨慎使用。systemPath元素可以引用环境变量。例如:

<dependency>

<groupId>javax.sql</groupId>

<artifactId>jdbc-stdext</artifactId>

<version>2.0</version>

<scope>system</scope>

<systemPath>${java.home}/lib/rt.jar</systemPath>

</dependency>

转自:https://www.jianshu.com/p/91655539e0ce

 

二:

scope的分类
compile
默认就是compile,什么都不配置也就是意味着compile。compile表示被依赖项目需要参与当前项目的编译,当然后续的测试,运行周期也参与其中,是一个比较强的依赖。打包的时候通常需要包含进去。

test
scope为test表示依赖项目仅仅参与测试相关的工作,包括测试代码的编译,执行。比较典型的如junit。

runntime
runntime表示被依赖项目无需参与项目的编译,不过后期的测试和运行周期需要其参与。与compile相比,跳过编译而已,说实话在终端的项目(非开源,企业内部系统)中,和compile区别不是很大。比较常见的如JSR×××的实现,对应的API jar是compile的,具体实现是runtime的,compile只需要知道接口就足够了。oracle jdbc驱动架包就是一个很好的例子,一般scope为runntime。另外runntime的依赖通常和optional搭配使用,optional为true。我可以用A实现,也可以用B实现。

provided
provided意味着打包的时候可以不用包进去,别的设施(Web Container)会提供。事实上该依赖理论上可以参与编译,测试,运行等周期。相当于compile,但是在打包阶段做了exclude的动作。

system
从参与度来说,也provided相同,不过被依赖项不会从maven仓库抓,而是从本地文件系统拿,一定需要配合systemPath属性使用。

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。

转自:https://blog.csdn.net/kimylrong/article/details/50353161

三:

scope的值
官方解释

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 
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 
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 
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 
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 section. It indicates the dependency to be replaced with the effective list of dependencies in the specified POM’s section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

中文解释 
1. compile 默认的范围,编译测试运行都有效。 
2. provided 编译和测试时有效,最后是在运行的时候不会被加入。官方举了一个例子。比如在JavaEE web项目中我们需要使用servlet的API,但是呢Tomcat中已经提供这个jar,我们在编译和测试的时候需要使用这个api,但是部署到tomcat的时候,如果还加入servlet构建就会产生冲突,这个时候就可以使用provided。 
3. runtime 在测试和运行时有效。 
4. test 在测试时有效。 
5. system 与本机系统相关联,可移植性差。编译和测试时有效。 
6. import 导入的范围,它只在使用dependencyManagement中,表示从其他pom中导入dependecy的配置。

转自:https://blog.csdn.net/sinat_25926481/article/details/76924780

maven scope 的作用的更多相关文章

  1. Maven Scope取值的含义

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt257 maven依赖关系中Scope的作用 Dependency Scope ...

  2. java maven scope compile,provide,system,test,runtime

    在一个maven项目中,如果存在编译需要而发布不需要的jar包,可以用scope标签,值设为provided.如下: <dependency>            <groupId ...

  3. maven scope和项目发布需要注意的地方

    Maven Scope的使用: http://www.cnblogs.com/wangyonghao/p/5976055.html servlet-api和jsp-api等jar包,一般由servle ...

  4. maven依赖关系中Scope的作用

      目前<scope>可以使用5个值: (1) compile (编译) compile是默认的范围:如果没有提供一个范围,那该依赖的范围就是编译范围.编译范围依赖在所有的classpat ...

  5. maven pom.xml 里scope的作用

    <dependency>中<scope>,它主要管理依赖的部署.目前<scope>可以使用5个值:     * compile,缺省值,适用于所有阶段,会随着项目一 ...

  6. maven中scope标签作用

    scope 是用来限制 dependency 的作用范围的,影响 maven 项目在各个生命周期时导入的 package 的状态,主要管理依赖的部署. scope 的作用范围: (1)compile: ...

  7. maven中的pom.xml中的scope的作用

    pom.xml配置文件中, <dependency>中的<scope>,它主要管理依赖的生效范围.目前<scope>可以使用5个值: * compile,缺省值,适 ...

  8. maven中pom文件中scope的作用

    Dependency Scope  <dependency>中还引入了<scope>,它主要管理依赖的部署.目前<scope>可以使用5个值: compile 默认 ...

  9. 关于maven依赖中的scope的作用和用法

    举例如下: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>s ...

随机推荐

  1. Java设计模式之Iterator

    public interface Aggregate { //调用iterator方法生成实现Iterator接口的类的实例 public abstract Iterator iterator(); ...

  2. 灵活运用SQL Server2008 SSIS变量

      在SSIS开发ETL(Extract-Transform-Load),数据抽取.转换.装载的过程.我们需要自己定义变量 一.SSIS变量简介 SSIS(SQL Server Integration ...

  3. Zabbbix之十二------Zabbix实现微信报警通知及创建聚合图形

    实战一:实现zabbix监控微信报警 1.在企业微信上注册账号 1.注册企业微信,管理员需要写上自己的真实姓名,扫描以下的二维码,与微信关联真实姓名. 2.登陆企业微信,然后创建一个微信故障通知应用 ...

  4. 二、继续学习(主要参考Python编程从入门到实践)

    操作列表 具体内容如下: # 操作列表 # 使用for循环遍历整个列表. # 使用for循环处理数据是一种对数据集执行整体操作的不错的方式. magicians = ['alice', 'david' ...

  5. ZedGraph5.1.5源码分析去掉鼠标悬浮内容闪烁问题(附源码下载)

    场景 在使用ZedGraph绘制曲线图时,将鼠标悬浮时内容闪烁,且频率很高. 找到其源码,发现不论鼠标移动的范围大小,甚至乎不论鼠标是否移动,都要刷新一次Tooltip. 注: 博客主页:https: ...

  6. Python学习笔记一:变量、函数

    变量.函数是Python语言的最基本单元,下面是我作为初学者的当前理解,随着学习的深入今后会做刷新. 变量:表示操作对象是谁. 变量的方法:表示能做什么事情. 如何设计变量:先分析需要解决的问题,基于 ...

  7. Vue开发环境跨域访问

    Vue开发环境跨域访问其他服务器或者本机其他端口,需要配置项目中config/index.js文件,修改如下 module.exports = { dev: { // Paths assetsSubD ...

  8. 虚拟机NAT模式连接外网

    虚拟机三种联网方式: 一.NAT(推荐使用)                功能:①可以和外部网络连通    ②可以隔离外部网络 二.桥接模式                        功能:直接 ...

  9. py 二级习题(重新输出文本-----每行一句话)

    #需要的一小段文本 txt = "人生得意须尽欢,莫使金樽空对月.天生我才必有用,千金散尽还复来." #对文本进行分割,转换成列表形式 def txt_split(txt): li ...

  10. AI算法:1. 决策树

    今天,我们介绍的机器学习算法叫决策树. 跟之前一样,介绍算法之前先举一个案例,然后看一下如何用算法去解决案例中的问题. 我把案例简述一下:某公司开发了一款游戏,并且得到了一些用户的数据.如下所示: 图 ...