栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Maven基础-依赖的范围管理 scope详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Maven基础-依赖的范围管理 scope详解

1.说明

What’s the mean of scope?
【answer】 : Scope is an attribute of dependency.
It indicates which life cycle of the project build the dependency works on.
==================================================
scope 是什么意思?
【答】:Scope 是依赖的一个属性。
它代表了这个依赖在项目的哪个生命周期起作用。

where does the scope appear?
【answer】 : It appears with gav of a dependency.
When it does’t appear,that’s means the dependency’s scope is compile.
=================================
scope 在哪里出现?
【答】:scope 往往和 依赖的 坐标一起出现。
当一个依赖中没有明确指明scope属性是,它的默认值 是 compile。

2.scope 的种类描述

There are three types of scopes : compile、test、provided。
compile : default. The dependency will participate in all construction phases of the project.
test : The dependency will participate in the test phases of the project only. Such as junit.
provided : The dependency will participate in the compile phase and test phase,but not the package phase.It means that the server will provide this dependency to run your program. Such as servlet依赖。
============================================================
scope有三种常见的类型 : compile、test、provided。
compile : 默认的类型。表示该依赖会伴随项目构建的整个过程,编译、测试、打包。
【对主程序有效、对测试程序有效、参与打包、参与部署】
test : 表示该依赖只会在测试阶段生效,比如 junit测试依赖。
【仅对测试程序有效】
provided : 表示该依赖会参与 项目的编译、测试阶段,但不会参与打包阶段。但是在项目部署时,该依赖会由 服务器进行提供,比如 servlet 的依赖,开发过程中需要使用,但在部署时由Tomcat服务器进行提供。
【对主程序有效、对测试程序有效、不参与打包、不参与部署】

3.实际的例子

compile类型 : 默认不写就是 compile 类型(最常见的类型)

    
    
      com.alibaba
      fastjson
      1.2.75
    

test 类型 : 仅测试时使用到的

    
      junit
      junit
      4.11
      test
    

provided类型 : 具体部署的服务器提供 servlet 和 jsp 。
【这种类型的依赖,如果范围使用错误了,则会导致jar包冲突的问题。】
【所以要对这种类型的依赖有特别的了解】

 
    
      javax.servlet
      javax.servlet-api
      4.0.1
      provided
    

    
    
      javax.servlet.jsp
      javax.servlet.jsp-api
      2.3.3
      provided
    

4.完成

Congratulations!
You are one step closer to success!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/584593.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号