Dependency scope is used to limit the transitivity of a dependency, and also to affect the classpath used for various build tasks.html
依賴範圍用於限制依賴項的傳遞性,也影響用於各類構建任務的類路徑。java
There are 6 scopes available:web
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.apache
默認的 scope。編譯依賴項可在項目的全部類路徑中使用。此外,這些依賴關係被傳播到依賴項目。app
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.maven
provided scope 很像 compile scope,但表示您但願JDK或容器在運行時提供依賴性。例如,當爲Java企業版構建Web應用程序時,您會將servlet API和相關的JavaEE API的依賴項設置爲提供的做用域,由於Web容器提供了這些類。此範圍僅在編譯和測試類路徑上可用,而且不是傳遞性的。ide
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.測試
此範圍指示編譯不須要依賴項,而是用於執行。它在運行時和測試類中,而不是編譯類路徑。ui
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.this
此範圍指示應用程序的正常使用不須要依賴項,而且僅可用於測試編譯和執行階段。這個範圍不是傳遞的。
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.
此範圍與 provided 相似,只是必須顯式地提供包含它的jar。工件老是可用的,而不是在存儲庫中查找的。
This scope is only supported on a dependency of type pom in the <dependencyManagement> section. It indicates the dependency to be replaced with the effective list of dependencies in the specified 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.
此範圍僅支持在「依賴關係管理>節」中的類型POM的依賴項中。它指示要在指定的POM的<dependencyManagement>部分中用有效的依賴項列表替換的依賴項。因爲它們被替換,具備導入範圍的依賴項實際上並不參與限制依賴項的傳遞性。
Each of the scopes (except for import) affects transitive dependencies in different ways, as is demonstrated in the table below. If a dependency is set to the scope in the left column, transitive dependencies of that dependency with the scope across the top row will result in a dependency in the main project with the scope listed at the intersection. If no scope is listed, it means the dependency will be omitted. 每一個scope(除了導入)都會以不一樣的方式影響傳遞依賴關係,以下面的表所示。若是將依賴項設置爲左列中的範圍,則該依賴項與跨頂行的範圍的傳遞依賴項將致使主項目中的依賴項與交叉點列出的範圍。若是沒有列出範圍,則表示依賴項將被省略。
it is intended that this should be runtime scope instead, so that all compile dependencies must be explicitly listed - however, there is the case where the library you depend on extends a class from another library, forcing you to have available at compile time. For this reason, compile time dependencies remain as compile scope even when they are transitive. 這應該是運行時範圍,所以必須顯式列出全部編譯依賴項-可是,您所依賴的庫從另外一個庫擴展了一個類,迫使您在編譯時可用。因爲這個緣由,編譯時依賴關係即便在傳遞時仍然保留爲編譯範圍。
以上內容取自maven官網資料
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.4</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
https://www.jianshu.com/p/a4fc54b5a6bf
https://blog.csdn.net/kimylrong/article/details/50353161
https://howtodoinjava.com/maven/maven-dependency-scopes/
https://www.baeldung.com/maven-dependency-scopes
https://www.tutorialspoint.com/maven/maven_manage_dependencies.htm