一次讲清:maven中的scope、依赖传递、依赖冲突解决、type、classifier、exclusions、optional

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.

scope解释的翻译:

 编译

这是默认范围,在没有指定时使用。编译依赖项可在项目的所有类路径中使用。而且,这些依赖关系被传播到依赖的项目中。

提供

这非常类似于compile,但表明您希望JDK或容器在运行时提供依赖关系。例如,在为Java Enterprise Edition构建web应用程序时,您应该将对Servlet API和相关Java EE API的依赖设置为所提供的范围,因为web容器提供了这些类。此作用域仅在编译和测试类路径上可用,且不可传递。

运行时

此作用域表明,该依赖项不是编译所必需的,而是执行所必需的。它在运行时和测试类路径中,但不在编译类路径中。

测试

此范围表明,该依赖项对于应用程序的正常使用是不需要的,并且仅对测试编译和执行阶段可用。此作用域不是可传递的。

系统

这个作用域与提供的作用域相似,只是必须提供显式包含它的JAR。工件总是可用的,并且不会在存储库中查找。

导入(仅在Maven 2.0.9或更高版本中可用)

此范围仅支持部分中类型pom的依赖项。它指示要用指定POM部分中的有效依赖项列表替换依赖项。由于它们被替换,具有导入范围的依赖项实际上不会参与限制依赖项的传递性。

整理一下:

scope可用classpath作用域传递案例
compile编译、测试、运行时spring-core
provided编译、测试servlet-api
system编译、测试 
runtime测试、运行时jdbc驱动
test测试junit

作用域传递(依赖传递):A依赖B,B依赖C,则A依赖C。

依赖冲突解决原则:

A通过多条路径依赖C,尤其在C的版本不同,依赖哪个C呢?

(1)A->B->C1

(2)A->D->C2

(3)A->B->D->C3

如果同时有(1)、(3),即依赖路径长度不同时,短路径优先原则,A依赖C1。如果同时有(1)、(2),即路径相同时,先声明优先原则,如先声明(1),后声明(2),A依赖C1。

system用法:

    <dependency>
      <groupId>sun.jdk</groupId>
      <artifactId>tools</artifactId>
      <version>1.5.0</version>
      <scope>system</scope>
      <systemPath>${java.home}/../lib/tools.jar</systemPath>
    </dependency>
	<dependency>
		<groupid>org.hamcrest</groupid>
		<artifactid>hamcrest-core</artifactid>
		<version>1.5</version>
		<scope>system</scope>
		<systempath>${pom.basedir}/WebContent/WEB-INF/lib/hamcrest-core-1.3.jar</systempath>
	</dependency>

 system必须结合systempath指定依赖路径。

import用法:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.9.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

导入了包含多个子模块的模块,实现了依赖的多继承。同时不会使此模块不作为项目工程的父模块,如:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.9.RELEASE</version>
</parent>

 只是单纯地引入了一个系列的模块集。

 maven的type:

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.9.RELEASE</version>
                <type>pom</type>
            </dependency>

type的值一般有jar、war、pom等,声明引入的依赖的类型。默认为jar。

maven的classifier(可以写入任何内容,以下是一些规范):

<dependency>  
    <groupId>net.sf.json-lib</groupId>   
    <artifactId>json-lib</artifactId>   
    <version>2.2.2</version>  
    <classifier>jdk15</classifier>    
</dependency>  

对应:json-lib-2.2.2-jdk15.jar,表示针对jdk15。

<dependency>  
    <groupId>net.sf.json-lib</groupId>   
    <artifactId>json-lib</artifactId>   
    <version>2.2.2</version>  
    <classifier>jdk15-javadoc</classifier>    
</dependency> 

对应:json-lib-2.2.2-jdk15-javadoc.jar,表示针对jdk15,除了classes外,还包含有java文档。

<dependency>  
    <groupId>net.sf.json-lib</groupId>   
    <artifactId>json-lib</artifactId>   
    <version>2.2.2</version>  
    <classifier>jdk15-sources</classifier>    
</dependency> 

对应:json-lib-2.2.2-jdk15-sources.jar,表示针对jdk15,除了classes外,还包含有源码。

 maven中的exclusions:

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.4</version>

            <exclusions>
                    <exclusion>  
                        <groupId>com.alibaba</groupId>
                        <artifactId>jconsole</artifactId>
                    </exclusion>  
                    <exclusion>
                        <groupId>com.alibaba</groupId>
                        <artifactId>tools</artifactId>
                    </exclusion>
            </exclusions>
</dependency>

表示:依赖com.alibaba.druid的同时,不继承com.alibaba.druid对com.alibaba.jconsole和com.alibaba.tools的依赖。注意:exclusion中不写version。

简单讲:B的pom中,C的exclusions中有D,则B不依赖D。

maven中的optional:

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.4</version>
       <scope>test</scope>
       <optional>true</optional>
</dependency>

 optional默认false,即依赖可传递。B依赖com.alibaba.druid时,如果A依赖B,optional为true,则A不依赖com.alibaba.druid。A想依赖com.alibaba.druid,只能A中写依赖com.alibaba.druid。如果A依赖B,但A想依赖的C和B想依赖的C版本不一致,或者A不想依赖C,那么可以在B中C依赖的optional配置true。

简单讲:B的pom中,C的optional为true,则A依赖B时,A不依赖C。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风铃峰顶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值