原帖地址:http://uule.iteye.com/blog/2087485
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.
-
- <!-- 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項目中的包配置