Dependency scope 是用来限制Dependency的作用范围的, 影响maven项目在各个生命周期时导入的package的状态。
自从2.0.9后,新增了1种,现在有了6种scope:
1.compile
编译范围,默认scope,在工程环境的classpath(编译环境)和打包(如果是WAR包,会包含在WAR包中)时候都有效。
默认的scope,表示 dependency 都可以在生命周期中使用。而且,这些dependencies 会传递到依赖的项目中。
2.provided
容器或JDK已提供范围,表示该依赖包已经由目标容器(如tomcat)和JDK提供,只在编译的classpath中加载和使用,打包的时候不会包含在目标包中。最常见的是j2ee规范相关的servlet-api和jsp-api等jar包,一般由servlet容器提供,无需在打包到war包中,如果不配置为provided,把这些包打包到工程war包中,在tomcat6以上版本会出现冲突无法正常运行程序(版本不符的情况)。
跟compile相似,但是表明了dependency 由JDK或者容器提供,例如Servlet AP和一些Java EE APIs。这个scope 只能作用在编译和测试时,同时没有传递性。
使用这个时,不会将包打入本项目中,只是依赖过来。
使用默认或其他时,会将依赖的项目打成jar包,放入本项目的Lib里
3.runtime
一般是运行和测试环境使用,编译时候不用加入classpath,打包时候会打包到目标包中。一般是通过动态加载或接口反射加载的情况比较多。也就是说程序只使用了接口,具体的时候可能有多个,运行时通过配置文件或jar包扫描动态加载的情况。典型的包括:JDBC驱动等。
表示dependency不作用在编译时,但会作用在运行和测试时
4.test
测试范围,一般是单元测试场景使用,在编译环境加入classpath,但打包时不会加入,如junit等。
表示dependency作用在测试时,不作用在运行时。
5.system
系统范围,与provided类似,只是标记为该scope的依赖包需要明确指定基于文件系统的jar包路径。因为需要通过systemPath指定本地jar文件路径,所以该scope是不推荐的。如果是基于组织的,一般会建立本地镜像,会把本地的或组织的基础组件加入本地镜像管理,避过使用该scope的情况。
跟provided 相似,但是在系统中要以外部JAR包的形式提供,maven不会在repository查找它
6.import
(仅在 Maven 2.0.9 或者更新版本中有效)
它只使用在中,表示从其它的pom中导入dependency的配置,例如: This scope is only used on a dependency of type pom in the section. It indicates that the specified POM should be replaced with the dependencies in that POM’s 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项目中的包配置
参考站点:
http://blog.youkuaiyun.com/u010456982/article/details/52044358