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

本文详细解读了Java项目中的scope概念,包括其含义、在依赖管理中的体现,以及compile、test、provided三种常见类型的应用实例。理解scope有助于正确配置项目依赖,避免部署时的冲突问题。
摘要由CSDN通过智能技术生成

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 类型(最常见的类型)

    <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.75</version>
    </dependency>

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

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

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

 <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>javax.servlet.jsp-api</artifactId>
      <version>2.3.3</version>
      <scope>provided</scope>
    </dependency>

4.完成

Congratulations!
You are one step closer to success!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值