maven依赖关系中Scope的作用

本文介绍了Maven中六种依赖范围(compile、provided、runtime、test、system、import)的概念及应用场景,包括如何指定依赖范围及它们在项目构建周期中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Dependency scope 是用来限制Dependency的作用范围的, 影响maven项目在各个生命周期时导入的package的状态。

自从2.0.9后,新增了1种,现在有了6种scope:

  • compile
    默认的scope,表示 dependency 都可以在生命周期中使用。而且,这些dependencies 会传递到依赖的项目中。
  • provided
    跟compile相似,但是表明了dependency 由JDK或者容器提供,例如Servlet AP和一些Java EE APIs。这个scope 只能作用在编译和测试时,同时没有传递性。
  • 使用这个时,不会将包打入本项目中,只是依赖过来。   
  • 使用默认或其他时,会将依赖的项目打成jar包,放入本项目的Lib里
  • 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.
  • Xml代码  收藏代码
    1. <!-- Servlet -->  
    2.         <dependency>  
    3.             <groupId>javax.servlet</groupId>  
    4.             <artifactId>servlet-api</artifactId>  
    5.             <version>2.5</version>  
    6.             <scope>provided</scope>  
    7.         </dependency>  
    8.         <dependency>  
    9.             <groupId>javax.servlet.jsp</groupId>  
    10.             <artifactId>jsp-api</artifactId>  
    11.             <version>2.1</version>  
    12.             <scope>provided</scope>  
    13.         </dependency>  
     
  • runtime
    表示dependency不作用在编译时,但会作用在运行和测试时
  • test
    表示dependency作用在测试时,不作用在运行时。
  • system
    跟provided 相似,但是在系统中要以外部JAR包的形式提供,maven不会在repository查找它。 例如:

<project>
...
<dependencies>
  <dependency>
   <groupId>javax.sql</groupId>
   <artifactId>jdbc-stdext</artifactId>
   <version>2.0</version>
   <scope>system</scope>
   <systemPath>${java.home}/lib/rt.jar</systemPath>
  </dependency>
</dependencies>
...
</project>

 

 

  • import (Maven 2.0.9 之后新增)
    它只使用在<dependencyManagement>中,表示从其它的pom中导入dependency的配置,例如:    This scope is only used on a dependency of type pom in the <dependencyManagement> section. It indicates that the specified POM should be replaced with the dependencies in that 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.

<project>

<modelVersion>4.0.0</modelVersion>

<groupId>maven</groupId>

<artifactId>B</artifactId>

<packaging>pom</packaging>

<name>B</name>

<version>1.0</version>

 

<dependencyManagement>

    <dependencies>

      <dependency>

        <groupId>maven</groupId>

        <artifactId>A</artifactId>

        <version>1.0</version>

        <type>pom</type>

        <scope>import</scope>

      </dependency>

      <dependency>

        <groupId>test</groupId>

        <artifactId>d</artifactId>

        <version>1.0</version>

      </dependency>

    </dependencies>

</dependencyManagement>

</project>

B项目导入A项目中的包配置

### 关于 `pip` 安装过程中由 `urllib3` 引发的异常错误解决方案 在使用 `pip` 进行包管理可能会遇到由于网络连接超时或其他原因导致的 `urllib3` 异常。以下是针对此类问题的具体分析和解决方法。 #### 1. 超时错误 (`ReadTimeoutError`) 当执行命令如 `pip3 install jupyterlab` 或其他依赖下载操作,如果目标服务器响应时间过长,则可能出现如下错误: ``` pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. ``` 此错误表明客户端未能及接收到来自远程主机的数据流[^1]。可以尝试通过增加超时参数来缓解该问题: ```bash pip --default-timeout=100 install <package_name> ``` 这里设置了一个更大的默认超时(单位为秒),从而允许更长时间等待服务端响应[^4]。 #### 2. 清理旧版本并重新安装 `pip` 有本地环境中残留损坏或不兼容的组件也可能引起类似的异常行为。按照以下步骤清理环境后再试可能有效果: - 删除已有的站点包目录下的所有文件: ```bash rm -rf ~/.local/lib/python<version>/site-packages/* ``` - 下载官方脚本重新部署最新版 `pip` : ```bash curl -sS https://bootstrap.pypa.io/get-pip.py | sudo python3 - type pip3 hash -r pip3 ``` 这一步骤能够确保使用的工具链是最新的状态,减少因程序本身缺陷造成的冲突风险[^2]. #### 3. 使用镜像源加速获取资源 考虑到国外某些网站访问速度较慢甚至不可达的情况,在国内推荐切换至阿里云、清华大学开源软件镜像站等提供更快捷稳定的服务地址作为替代方案之一。修改配置方式如下所示: 编辑或者创建名为 `.pip/pip.conf`(Linux/Mac OS X) 的全局配置文档加入下面内容即可生效: ```ini [global] index-url = https://mirrors.aliyun.com/pypi/simple/ trusted-host = mirrors.aliyun.com ``` 对于 Windows 用户来说路径应改为 `%APPDATA%\Python\pip\pip.ini`. 另外还有临指定的方法适用于单次调用场景下无需永久更改设定的情形 : ```bash pip install some-package -i http://pypi.douban.com/simple --trusted-host pypi.douban.com ``` 以上措施均有助于改善因地理因素带来的延迟现象进而规避潜在隐患[^4]. #### 4. 更新或替换底层库 `urllib3` 既然问题是围绕着 `urllib3`, 那么单独升级它也是一个得考虑的方向 。运行下列指令完成更新过程: ```bash pip install --upgrade urllib3 ``` 当然也可以手动卸载再重装一遍确认效果如何变化 : ```bash pip uninstall urllib3 && pip install urllib3 ``` 得注意的是 , 如果项目里头绑定了特定版本号的话记得查阅对应说明文档调整策略适配需求[^3]. --- ### 总结 综上所述,面对 `pip` 和其内部实现所依托的 `urllib3` 出现的各种异常状况可以从多个角度切入排查处理。无论是优化网络条件还是修正自身软硬件设施都不可或缺。希望上述建议能帮助到您解决问题!
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值