如何使用Maven scope
maven 有6個scope類型,下面簡單總結備忘下
<dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.4</version><scope>provided</scope> </dependency>maven官方描述
Dependency Scope
Dependency scope is used to limit the transitivity of a depedency, and also to affect the classpath used for various build tasks.
There are 6 scopes available:
compile
This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.?
provided
This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, 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.?
runtime
This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.?
test
This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases.?
system
This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.?
import (only available in Maven 2.0.9 or later)
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.?
------------------------------------------------------------------------------------------------------------------------------------
翻譯下(以下內容載自:http://acooly.iteye.com/blog/1788890)
1.compile
編譯范圍,默認scope,在工程環境的classpath(編譯環境)和打包(如果是WAR包,會包含在WAR包中)時候都有效。
2.provided
容器或JDK已提供范圍,表示該依賴包已經由目標容器(如tomcat)和JDK提供,只在編譯的classpath中加載和使用,打包的時候不會包含在目標包中。最常見的是j2ee規范相關的servlet-api和jsp-api等jar包,一般由servlet容器提供,無需在打包到war包中,如果不配置為provided,把這些包打包到工程war包中,在tomcat6以上版本會出現沖突無法正常運行程序(版本不符的情況)。
3.runtime
一般是運行和測試環境使用,編譯時候不用加入classpath,打包時候會打包到目標包中。一般是通過動態加載或接口反射加載的情況比較多。也就是說程序只使用了接口,具體的時候可能有多個,運行時通過配置文件或jar包掃描動態加載的情況。典型的包括:JDBC驅動等。
4.test
測試范圍,一般是單元測試場景使用,在編譯環境加入classpath,但打包時不會加入,如junit等。
5.system
系統范圍,與provided類似,只是標記為該scope的依賴包需要明確指定基于文件系統的jar包路徑。因為需要通過systemPath指定本地jar文件路徑,所以該scope是不推薦的。如果是基于組織的,一般會建立本地鏡像,會把本地的或組織的基礎組件加入本地鏡像管理,避過使用該scope的情況。
實踐:
- provided是沒有傳遞性的,也就是說,如果你依賴的某個jar包,它的某個jar的范圍是provided,那么該jar不會在你的工程中依靠jar依賴傳遞加入到你的工程中。
- provided具有繼承性,上面的情況,如果需要統一配置一個組織的通用的provided依賴,可以使用parent,然后在所有工程中繼承。
總結
以上是生活随笔為你收集整理的如何使用Maven scope的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 消防基础常识是什么
- 下一篇: 水浒传作文600字左右