maven依赖的作用域说明


Maven中的dependency的scope作用域.

转载自:https://juejin.cn/post/6870758607659646983

Dependency scope是用来限制Dependency作用范围的,影响Maven项目在各个声明周期导入的package状态。

配置示例如下:

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

compile.

这是默认作用域。compile作用域范围的依赖项在所有情况下都是有效的,包括编译、运行和测试。

provided.

这个作用域表示该依赖项有JDK或运行容器在运行时提供。也就是说,该依赖项在测试和编译下才会用到。

runtime.

这个作用域表示该依赖项在运行时需要,在编译的时候不需要。在测试下,该依赖项也会被使用。

test.

这个作用域表示该依赖项只在测试时有用,在编译和运行时不会被用到。

system.

表示该依赖项是由我们提供的,不需要maven到仓库中寻找。使用的时候需要和另一个标签systemPath配合使用,这个标签表示该依赖项在系统中的位置,使用的是绝对路径。官网给出一个关于使用JDK的tools.jar的例子。

<dependency>
  <groupId>sun.jdk</groupId>
  <artifactId>tools</artifactId>
  <version>1.5.0</version>
  <scope>system</scope>
  <systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
复制代码

import.

我们知道通过继承一个项目的依赖,我们可以在子项目中很好的申明需要使用父项目的dependencyMangement中定义的依赖项。但是每一个项目只能申明唯一的一个父项目,那么在某些时候就会限制我们项目的建立。

为此,Maven为我们提供了一种方法,那就是通过设置依赖项的socpe为import。注意,这种方式只在Maven2.0.9以上的版本才生效。

下面为父项目artifactA的pom.xml文件:

<project>  
       ...  
       <groupId>groupA</groupId>  
       <artifactId>artifactA</artifactId>  
       <version>1.0</version>  
       <packaging>pom</packaging>  
       <dependencyManagement>  
              <dependencies>  
                     <dependency>  
                            <groupId>test</groupId>  
                            <artifactId>A</artifactId>  
                            <version>1.0</version>  
                     </dependency>  
                     <dependency>  
                            <groupId>test</groupId>  
                            <artifactId>B</artifactId>  
                            <version>1.1</version>  
                     </dependency>  
                     <dependency>  
                            <groupId>test</groupId>  
                            <artifactId>C</artifactId>  
                            <version>1.2</version>  
                     </dependency>  
                     <dependency>  
                            <groupId>test</groupId>  
                            <artifactId>D</artifactId>  
                            <version>1.3</version>  
                     </dependency>  
              </dependencies>  
       </dependencyManagement>  
       ...  
</project> 

如果使用之前的继承机制,那么这个时候子项目artifactB的pom.xml文件是这样定义的:

<project>  
       ...  
       <parent>  
              <groupId>groupA</groupId>  
              <artifactId>artifactA</artifactId>  
              <version>1.0</version>  
       </parent>  

       <groupId>groupA</groupId>  
       <artifactId>artifactB</artifactId>  
       <version>1.0</version>  
       <packaging>pom</packaging> 

       <dependencies>  
              <dependency>  
                     <groupId>test</groupId>  
                     <artifactId>A</artifactId>  
              </dependency>  
              <dependency>  
                     <groupId>test</groupId>  
                     <artifactId>D</artifactId>  
                     <scope>runtime</scope>  
              </dependency>  
       </dependencies>  
       ...  
</project>  

按照import方式,是这样定义的:

<project>  
       ...  
       <groupId>groupA</groupId>  
       <artifactId>artifactB</artifactId>  
       <version>1.0</version>  
       <packaging>pom</packaging>  

       <dependencyManagement>  
              <dependencies>  
                     <dependency>  
                            <groupId>groupA</groupId>  
                            <artifactId>artifactA</artifactId>  
                            <version>1.0</version>  
                            <type>pom</type>  
                            <scope>import</scope>  
                     </dependency>  
              </dependencies>  
       </dependencyManagement>  

       <dependencies>  
              <dependency>  
                     <groupId>test</groupId>  
                     <artifactId>A</artifactId>  
              </dependency>  
              <dependency>  
                     <groupId>test</groupId>  
                     <artifactId>D</artifactId>  
                     <scope>runtime</scope>  
              </dependency>  
       </dependencies>  
       ...  

</project>  

文章作者: liuminkai
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 liuminkai !
评论
 上一篇
SSM整合ElasticSearch SSM整合ElasticSearch
参考:https://blog.csdn.net/raveee/article/details/80063320 1、新建普通maven项目 过程略 2、配置 3、导入依赖 4、
2020-12-05
下一篇 
ElasticSearch7.6入门学习笔记 ElasticSearch7.6入门学习笔记
笔记记录 B站狂神说Java的ElasticSearch课程:https://www.bilibili.com/video/BV17a4y1x7zq 在学习ElasticSearch之前,先简单了解一下Lucene: Doug Cut
2020-11-24
  目录