<scope>的取值范围有:compile、provided、runtime、system、test、import
- compile(默认)
使用compile修饰的的依赖,会在编译 和 打包 的时候被加入进来 - provided
编译和测试时有效。例如在开发一个web应用时,编译时需要servlet-api.jar,但是运行时不需要该jar包,因为就这个jar包已由web服务器提供,如果在打包时又被加入进去,就可能产生冲突,此时就用provided进行范围修饰。 - system
与provided相同,不过被依赖项不会从maven仓库获取,而是从本地文件系统拿,需要配合systemPath使用。 - runtime
运行时才会依赖,编译时不会依赖。比如,在编译时不需要JDBC API的jar包,而在运行时才需要JDBC驱动包,就可以用runtime修饰。 - test
mvn test时有效,编译和打包时都不会使用这个依赖。 - import
maven多模块项目结构中,可以使用parent定义父项目,实现从父项目中继承依赖。但maven只能单继承,即一个项目只能使用一个parent标签只能使用一个parent标签定义父项目。
maven2.9之后的版本引入了新的功能,可以实现依赖上的多重继承。就这个功能可以将依赖配置复杂的pom文件拆分成多个独立的pom文件。这样处理可以使得maven的pom配置更加简洁。现时可以复用这些依赖。比如,在开发spring boot项目的时候,pom中会有如下配置:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
但是如果该项目是个maven子模块的话,就会出问题。由于maven类似java是单继承,不能有两个parent,现在<parent>标签已经用来引用父模块,现在又用来引用springboot,就会产生冲突。那怎么解决呢?
解决办法就是:使用dependencyManagemeng引入dependency,并且scope属性改为import。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
scope的import属性只能在<depencencyManagement>中使用,表示从其它的pom文件中导入dependency配置。