一、依賴樹是什麼?
在介紹Maven查看依賴樹之前,我們先來了解一下什麼是依賴樹。在Maven中,當我們在一個項目中引入外部依賴時,這些依賴項本身又可能引入其他的依賴項,這就形成了一個以當前項目為根節點的依賴樹結構。如下圖所示:
my-project ├── org.springframework:spring-context:jar:5.2.8.RELEASE:compile │ ├── org.springframework:spring-aop:jar:5.2.8.RELEASE:compile │ ├── org.springframework:spring-beans:jar:5.2.8.RELEASE:compile │ └── org.springframework:spring-core:jar:5.2.8.RELEASE:compile ├── org.springframework:spring-web:jar:5.2.8.RELEASE:compile │ └── org.springframework:spring-webmvc:jar:5.2.8.RELEASE:compile ├── org.springframework.security:spring-security-web:jar:5.3.5.RELEASE:compile │ ├── org.springframework.security:spring-security-core:jar:5.3.5.RELEASE:compile │ └── org.springframework:spring-expression:jar:5.2.8.RELEASE:compile └── org.slf4j:slf4j-log4j12:jar:1.7.30:compile └── log4j:log4j:jar:1.2.17:compile
上圖展示了一個簡單的依賴樹結構,其中根節點為my-project,my-project依賴於四個直接子節點:spring-context、spring-web、spring-security-web和slf4j-log4j12。這些直接子節點又會繼續引入其他的依賴項。
二、Maven查看依賴樹可以幹什麼?
Maven查看依賴樹的主要作用是幫助我們分析項目依賴關係,方便我們在引入新依賴時避免版本衝突甚至不必要的重複引入。下面就來介紹一下Maven查看依賴樹功能的具體使用方法。
三、Maven查看依賴樹的命令
我們可以通過Maven命令行工具的「mvn」命令來查看一個Maven項目的依賴樹。具體命令如下:
mvn dependency:tree
執行這個命令後,Maven就會在控制台輸出當前項目的依賴樹結構。
$ mvn dependency:tree [INFO] Scanning for projects... [INFO] [INFO] ------------------------ [INFO] Building my-project 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-dependency-plugin:3.1.2:tree (default-cli) @ my-project --- [INFO] com.example:my-project:jar:1.0-SNAPSHOT [INFO] \- org.springframework:spring-context:jar:5.2.8.RELEASE:compile [INFO] +- org.springframework:spring-aop:jar:5.2.8.RELEASE:compile [INFO] +- org.springframework:spring-beans:jar:5.2.8.RELEASE:compile [INFO] +- org.springframework:spring-core:jar:5.2.8.RELEASE:compile [INFO] \- org.springframework:spring-expression:jar:5.2.8.RELEASE:compile [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.748 s [INFO] Finished at: 2021-08-05T14:21:39+08:00 [INFO] ------------------------------------------------------------------------
四、Maven查看依賴樹的輸出內容
命令執行後,Maven會在控制台輸出當前項目的依賴樹結構,具體內容包括:
- 當前項目信息,包括項目名稱、版本和構建類型等
- 依賴樹結構,以樹形結構展示當前項目的依賴關係
- 依賴關係信息,包括依賴包名稱、版本和依賴範圍等
五、依賴範圍的含義
在依賴關係信息中,我們可以看到一個參數叫做「依賴範圍」。依賴範圍描述了當前依賴項被所依賴的項目使用的程度。具體的依賴範圍包括:
- compile
- provided
- runtime
- test
- system
這些依賴範圍的含義分別是:
- compile:默認的依賴範圍,表示在編譯、測試、執行時都需要使用這個依賴。
- provided:該依賴項在編譯、測試中需要,但是在打包時不需要包含,因為在運行時由JDK或者伺服器提供,比如servlet-api。
- runtime:該依賴項在編譯時不需要,但是在測試和運行時需要,例如JDBC驅動。
- test:該依賴項只在測試時使用,不能在編譯和運行時使用。
- system:該依賴項由系統提供,需要在POM文件中顯示指定它的路徑,一般不建議使用此依賴範圍。
六、排除依賴的方法
Maven的依賴管理可以自動引入依賴包的版本,但是當同一項依賴被不同的模塊引用時,版本衝突會導致編譯失敗或者運行時錯誤。在這種情況下,我們就需要指定排除某個模塊的依賴。在POM文件中可以通過<exclusions>標籤進行排除。例如:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.8.RELEASE</version> <exclusions> <exclusion> <groupId>org.springframework.security</groupId> <artifactId>spring-security-core</artifactId> </exclusion> </exclusions> </dependency>
上述代碼中,排除了spring-context依賴中的spring-security-core組件。
七、依賴樹的過濾
在實際項目開發中,可能會遇到需要篩選特定依賴的需求。Maven提供了-d和-D兩個參數用於過濾依賴樹,具體含義如下:
- -D包含:只顯示符合條件的依賴項。
- -D排除:不顯示符合條件的依賴項。
- -D輸出類型:以文本方式輸出或者以DOT格式輸出(便於繪製依賴圖)。
具體命令如下:
mvn dependency:tree -Dincludes=groupId:artifactId:version
其中,includes參數表示指定需要包含的依賴,可以指定groupId、artifactId、version等多個條件,各個條件之間使用冒號隔開,例如:
mvn dependency:tree -Dincludes=org.springframework:spring-web,com.google.guava:*
上述命令的含義是,只顯示groupId為org.springframework且artifactId為spring-web或者groupId 為com.google.guava的依賴。
八、結語
本文主要介紹了Maven查看依賴樹的相關知識,包括了依賴樹的概念、命令、輸出內容、依賴範圍、排除依賴和依賴樹的過濾等方面。掌握了這些知識,我們可以更加方便快捷地管理Maven項目的依賴關係,提高開發效率。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/227473.html