一:
1.Maven中的依賴作用范圍概述
Maven中使用 scope 來指定當前包的依賴范圍和依賴的傳遞性。常見的可選值有:compile, provided, runtime, test, system 等。scope 主要是用在 pom.xml 文件中的依賴定義部分,例如:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>3.2.1.RELEASE</version> <scope>test</scope> </dependency>
2.scope各種取值詳解
| scope取值 | 有效范圍(compile, runtime, test) | 依賴傳遞 | 例子 |
|---|---|---|---|
| compile | all | 是 | spring-core |
| provided | compile, test | 否 | servlet-api |
| runtime | runtime, test | 是 | JDBC驅動 |
| test | test | 否 | JUnit |
| system | compile, test | 是 |
正如上表所示,
compile :為默認的依賴有效范圍。如果在定義依賴關系的時候,沒有明確指定依賴有效范圍的話,則默認采用該依賴有效范圍。此種依賴,在編譯、運行、測試時均有效。
provided :在編譯、測試時有效,但是在運行時無效。例如:servlet-api,運行項目時,容器已經提供,就不需要Maven重復地引入一遍了。
runtime :在運行、測試時有效,但是在編譯代碼時無效。例如:JDBC驅動實現,項目代碼編譯只需要JDK提供的JDBC接口,只有在測試或運行項目時才需要實現上述接口的具體JDBC驅動。
test :只在測試時有效,例如:JUnit。
system :在編譯、測試時有效,但是在運行時無效。和provided的區別是,使用system范圍的依賴時必須通過systemPath元素顯式地指定依賴文件的路徑。由於此類依賴不是通過Maven倉庫解析的,而且往往與本機系統綁定,可能造成構建的不可移植,因此應該謹慎使用。systemPath元素可以引用環境變量。例如:
<dependency> <groupId>javax.sql</groupId> <artifactId>jdbc-stdext</artifactId> <version>2.0</version> <scope>system</scope> <systemPath>${java.home}/lib/rt.jar</systemPath> </dependency>
轉自:https://www.jianshu.com/p/91655539e0ce
二:
scope的分類
compile
默認就是compile,什么都不配置也就是意味着compile。compile表示被依賴項目需要參與當前項目的編譯,當然后續的測試,運行周期也參與其中,是一個比較強的依賴。打包的時候通常需要包含進去。
test
scope為test表示依賴項目僅僅參與測試相關的工作,包括測試代碼的編譯,執行。比較典型的如junit。
runntime
runntime表示被依賴項目無需參與項目的編譯,不過后期的測試和運行周期需要其參與。與compile相比,跳過編譯而已,說實話在終端的項目(非開源,企業內部系統)中,和compile區別不是很大。比較常見的如JSR×××的實現,對應的API jar是compile的,具體實現是runtime的,compile只需要知道接口就足夠了。oracle jdbc驅動架包就是一個很好的例子,一般scope為runntime。另外runntime的依賴通常和optional搭配使用,optional為true。我可以用A實現,也可以用B實現。
provided
provided意味着打包的時候可以不用包進去,別的設施(Web Container)會提供。事實上該依賴理論上可以參與編譯,測試,運行等周期。相當於compile,但是在打包階段做了exclude的動作。
system
從參與度來說,也provided相同,不過被依賴項不會從maven倉庫抓,而是從本地文件系統拿,一定需要配合systemPath屬性使用。
scope的依賴傳遞
A–>B–>C。當前項目為A,A依賴於B,B依賴於C。知道B在A項目中的scope,那么怎么知道C在A中的scope呢?答案是:
當C是test或者provided時,C直接被丟棄,A不依賴C;
否則A依賴C,C的scope繼承於B的scope。
轉自:https://blog.csdn.net/kimylrong/article/details/50353161
三:
scope的值
官方解釋
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. This scope is not transitive.
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 supported on a dependency of type pom in the section. It indicates the dependency to be replaced with the effective list of dependencies in the specified POM’s section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.
中文解釋
1. compile 默認的范圍,編譯測試運行都有效。
2. provided 編譯和測試時有效,最后是在運行的時候不會被加入。官方舉了一個例子。比如在JavaEE web項目中我們需要使用servlet的API,但是呢Tomcat中已經提供這個jar,我們在編譯和測試的時候需要使用這個api,但是部署到tomcat的時候,如果還加入servlet構建就會產生沖突,這個時候就可以使用provided。
3. runtime 在測試和運行時有效。
4. test 在測試時有效。
5. system 與本機系統相關聯,可移植性差。編譯和測試時有效。
6. import 導入的范圍,它只在使用dependencyManagement中,表示從其他pom中導入dependecy的配置。
轉自:https://blog.csdn.net/sinat_25926481/article/details/76924780
