maven依赖关系中Scope的作用
2015-09-23 11:15
369 查看
Dependency scope 是用来限制Dependency的作用范围的, 影响maven项目在各个生命周期时导入的package的状态。
自从2.0.9后,新增了1种,现在有了6种scope:
compile
默认的scope,表示 dependency 都可以在生命周期中使用。而且,这些dependencies 会传递到依赖的项目中。
provided
跟compile相似,但是表明了dependency 由JDK或者容器提供,例如Servlet AP和一些Java EE APIs。这个scope 只能作用在编译和测试时,同时没有传递性。
使用这个时,不会将包打入本项目中,只是依赖过来。
使用默认或其他时,会将依赖的项目打成jar包,放入本项目的Lib里
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.
Xml代码
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
runtime
表示dependency不作用在编译时,但会作用在运行和测试时
test
表示dependency作用在测试时,不作用在运行时。
system
跟provided 相似,但是在系统中要以外部JAR包的形式提供,maven不会在repository查找它。 例如:
import (Maven 2.0.9 之后新增)
它只使用在<dependencyManagement>中,表示从其它的pom中导入dependency的配置,例如: This scope is only used on a dependency of
type pom in the <dependencyManagement> section. It indicates that the specified POM should be replaced with the dependencies in that POM's <dependencyManagement> 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项目中的包配置
自从2.0.9后,新增了1种,现在有了6种scope:
compile
默认的scope,表示 dependency 都可以在生命周期中使用。而且,这些dependencies 会传递到依赖的项目中。
provided
跟compile相似,但是表明了dependency 由JDK或者容器提供,例如Servlet AP和一些Java EE APIs。这个scope 只能作用在编译和测试时,同时没有传递性。
使用这个时,不会将包打入本项目中,只是依赖过来。
使用默认或其他时,会将依赖的项目打成jar包,放入本项目的Lib里
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.
Xml代码
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
runtime
表示dependency不作用在编译时,但会作用在运行和测试时
test
表示dependency作用在测试时,不作用在运行时。
system
跟provided 相似,但是在系统中要以外部JAR包的形式提供,maven不会在repository查找它。 例如:
<project>
...
<dependencies>
<dependency>
<groupId>javax.sql</groupId>
<artifactId>jdbc-stdext</artifactId>
<version>2.0</version>
<scope>system</scope>
<systemPath>${java.home}/lib/rt.jar</systemPath>
</dependency>
</dependencies>
...
</project>
import (Maven 2.0.9 之后新增)
它只使用在<dependencyManagement>中,表示从其它的pom中导入dependency的配置,例如: This scope is only used on a dependency of
type pom in the <dependencyManagement> section. It indicates that the specified POM should be replaced with the dependencies in that POM's <dependencyManagement> 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项目中的包配置
相关文章推荐
- linux:SUID、SGID详解
- eclipse,tomcat不能部署项目,提示Project facet Java version 1.8 is not supported
- Tomcat__Connector的三种运行模式
- Linux SVN 命令大全
- VS2010+opencv2.4.9安装
- 使用Topshelf创建Windows服务
- vc监控鼠标键盘动作
- linux下32位汇编hello, world!
- Media---I,P,B帧区别
- Linux各个服务的配置命令
- linux运维实战练习-2015年9月13日-9月15日课程作业
- Centos6.5安装与配置JDK-8(简单实用)
- linux /etc/hosts文件作用
- Tomcat安全配置
- vim使用
- dd if=/dev/zero of=的含义是什么?Linux 下的dd命令使用详解
- 揭秘Apache顶级项目大数据分析引擎 Flink:与Spark的对比与分析
- openSession和getCurrentSession
- solr + zookeeper 组建 solrcloud
- centos6.5下Zabbix系列之Zabbix安装搭建及汉化