Maven Dependency配置說明

Dependency Scope

依賴的範圍分爲以下幾種環境:編譯環境(compile classpath)、測試環境(test classpath)、運行環境(runtime classpath)。根據不同的修飾控制依賴的影響範圍

  • compile: 默認Scope, 依賴再項目的所有環境引用
  • provided: 運行時不提供依賴,運行時依賴由JDK或web容器提供,依賴不傳遞,如J2EE
  • runtime: 編譯環境不提供依賴,運行時和測試環境需要依賴,如數據庫驅動依賴
  • test: 測試環境依賴,依賴不傳遞
  • system: 與provided類似,但是使用的是本地jar包
  • import: 只有在type爲pom的時候使用,只能在dependencyManagement中使用,表示引用Pom文件中的那些依賴

官方文檔說明:

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.
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
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.

Dependency Type
沒有在官網上找到說明,在其他博客中看到這樣寫的

Adding the type of apk to the dependency allows the Maven Android plugin to find the Android package of the application.
不難看出,type=apk告訴maven使用maven android plugin來進行處理type=爲apk的依賴。從而推想type=ejb就是告訴maven使用maven ejb plugin來處理。

type應該就是“maven處理依賴的方式”?不確定,望指正
type默認值是jar

記錄一個關於type=pom的坑:
使用下面代碼配置,版本信息會unknow,需要刪除type與scope節點才能找到版本。

 <dependencyManagement>
        <dependencies>
			<dependency>
	               <groupId>com.baomidou</groupId>
	               <artifactId>mybatis-plus</artifactId>
	               <version>${mybatis-plus.version}</version>
	               <type>pom</type>
	               <scope>import</scope>
	           </dependency>
	        <dependencies>
<dependencyManagement>
<dependency>
     <groupId>com.baomidou</groupId>
     <artifactId>mybatis-plus</artifactId>
</dependency>

但是在配置spring-boot-dependencies時type需要設置爲pom

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
       <dependencies>
<dependencyManagement>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章