目录:
- scope的类型总览
- 详解
- compile
- test
- runtime
- privided
- system
- import
一. scope的类型总览
可参考:https://blog.csdn.net/lishuoboy/article/details/100554751
二. 详解
1.compile(默认选项)
表示为当前依赖参与项目的编译、测试和运行阶段,scope的默认选项。打包之时,会达到包里去。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
</dependency>
2. test
表示为当前依赖只参与测试阶段。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
3. runtime
表示为当前依赖只参与运行阶段,一般这种类库都是接口与实现相分离的类库,如mysql的驱动包
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
4.privided
表示为当前依赖在打包过程中,不需要打进去,需要由运行的环境来提供,比如tomcat或者基础类库,如a依赖于b和c,bc中都包含gson依赖(privided),则a中不会依赖bc中的gson,需由a自行引入gson依赖。区别在于打包阶段进行了exclude操作。
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<scope>provided</scope>
</dependency>
5. system
表示为当前依赖不从maven仓库获取,从本地系统获取,结合systempath使用,常用于无法从maven仓库获取的包。
<dependency>
<groupId>com.supermap</groupId>
<artifactId>data</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>
${project.basedir}/src/main/resources/supermap/com.supermap.data.jar
</systemPath>
</dependency>
6. import
这个是maven2.0.9版本后出的属性,import只能在dependencyManagement的中使用,能解决maven单继承问题,import依赖关系实际上并不参与限制依赖关系的传递性。
表示为当前项目依赖为多继承关系,常用于项目中自定义父工程,需要注意的是只能用在dependencyManagement里面,且仅用于type=pom的dependency。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>