Maven 依賴範圍(Dependency Scope) 轉

Dependency Scope

Dependency scope is used to limit the transitivity of a dependency, 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.

默認的 scope。編譯依賴項可在項目的所有類路徑中使用。此外,這些依賴關係被傳播到依賴項目。

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.

provided scope 很像 compile scope,但表示您希望JDK或容器在運行時提供依賴性。例如,當爲Java企業版構建Web應用程序時,您會將servlet API和相關的JavaEE API的依賴項設置爲提供的作用域,因爲Web容器提供了這些類。此範圍僅在編譯和測試類路徑上可用,並且不是傳遞性的。

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.

此範圍與 provided 類似,只是必須顯式地提供包含它的jar。工件總是可用的,而不是在存儲庫中查找的。

import

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>

其他關於scope的資料

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章