一、指令指導文檔:
官方文檔:https://maven.apache.org/plugins/maven-dependency-plugin/tree-mojo.html
https://maven.apache.org/plugins/maven-dependency-plugin/examples/filtering-the-dependency-tree.html
二、常用參數詳解
-
includes
類型:String
描述:顯示包含依賴的jar包。
格式:[groupId]:[artifactId]:[type]:[version]
,支持通配符*。例如,org.apache.*
將匹配groupid 以org.apache.
開頭的所有內容;:::*-SNAPSHOT
匹配所有jar。 -
excludes
類型:String
描述:顯示不包含依賴的jar包。
格式:[groupId]:[artifactId]:[type]:[version]
,支持通配符* -
appendOutput
類型:boolean
描述:是否將輸出內容追加到文件或覆蓋它。
默認值:false
-
outputEncoding
類型:String
描述:無
默認值:${project.reporting.outputEncoding}
-
outputFile
類型:File
描述:如果指定,此參數將將依賴關系樹寫入指定的路徑,而不是寫入控制台。 -
outputType
類型:String
描述:如果指定,此參數將使用指定格式寫入依賴關系樹。當前支持的格式有:(text
默認)dot
、graphml
和tgf
。這些附加格式可以繪制到圖像文件中。
默認值:text
。 -
verbose
類型:boolean
描述:查看沖突和重復的具體情況
默認值:false
。
三、一般常用的命令
-
查看項目依賴:
mvn dependency:analyze
-
查看項目直接和傳遞依賴:
mvn dependency:tree
-
沖突和重復的具體情況,用verbose參數
mvn dependency:tree -Dverbose
-
查看依賴樹中包含某個groupId和artifactId的依賴鏈(注意-Dincludes后面是等於號)
mvn dependency:tree -Dincludes=com.alibaba:fastjson
-
查看依賴樹中包含某個artifactId的依賴鏈(artifactId前面加上冒號)
mvn dependency:tree -Dincludes=:fastjson
-
查看依賴樹中包含某個groupId的依賴鏈(-Dincludes后面跟上groupId)
mvn dependency:tree -Dincludes=com.alibaba
-
查看maven構建時有效的pom:
mvn help:effective-pom
-
重定向至文本文件
mvn dependency:tree -D outputFile=dependency_tree.txt
mvn dependency:tree -D outputFile=dependency_tree.txt -D outputType=dot
-
查看沖突和重復的具體情況,用verbose參數
mvn dependency:tree -Dverbose
“+-”符號表示該包后面還有其它依賴包,“-”表示該包后面不再依賴其它jar包。每個層級結尾處用 - 標記
以下面springboot項目為例,運行上述命令顯示的結果:
https://gitee.com/tiankong0310/springboot-weixin-alipay?_from=gitee_search
執行mvn dependency:tree
后的依賴(僅展示部分):
[INFO] --- maven-dependency-plugin:3.0.2:tree (default-cli) @ springboot-weixin-alipay ---
[INFO] com.fengdu:springboot-weixin-alipay:war:0.0.1-SNAPSHOT
[INFO] +- com.github.pagehelper:pagehelper-spring-boot-starter:jar:1.2.5:compile
[INFO] | +- org.mybatis.spring.boot:mybatis-spring-boot-starter:jar:1.3.2:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-jdbc:jar:2.0.3.RELEASE:compile
[INFO] | | | +- com.zaxxer:HikariCP:jar:2.7.9:compile
[INFO] | | | \- org.springframework:spring-jdbc:jar:5.0.7.RELEASE:compile
[INFO] | | | \- org.springframework:spring-tx:jar:5.0.7.RELEASE:compile
[INFO] | | +- org.mybatis.spring.boot:mybatis-spring-boot-autoconfigure:jar:1.3.2:compile
[INFO] | | +- org.mybatis:mybatis:jar:3.4.6:compile
[INFO] | | \- org.mybatis:mybatis-spring:jar:1.3.2:compile
[INFO] | +- com.github.pagehelper:pagehelper-spring-boot-autoconfigure:jar:1.2.5:compile
[INFO] | \- com.github.pagehelper:pagehelper:jar:5.1.4:compile
[INFO] | \- com.github.jsqlparser:jsqlparser:jar:1.0:compile
[INFO] +- com.alibaba:druid-spring-boot-starter:jar:1.1.9:compile
[INFO] | +- com.alibaba:druid:jar:1.1.9:compile
[INFO] | +- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] | \- org.springframework.boot:spring-boot-autoconfigure:jar:2.0.3.RELEASE:compile
執行mvn dependency:tree -Dincludes=com.alibaba:
后的依賴樹:
[INFO] --- maven-dependency-plugin:3.0.2:tree (default-cli) @ springboot-weixin-alipay ---
[INFO] com.fengdu:springboot-weixin-alipay:war:0.0.1-SNAPSHOT
[INFO] \- com.alibaba:druid-spring-boot-starter:jar:1.1.9:compile
[INFO] \- com.alibaba:druid:jar:1.1.9:compile
執行mvn dependency:tree -Dverbose
后的依賴樹,可以看到依賴是否存在版本沖突。由於示例代碼沒有沖突,所以截取了其他項目掃描的結果:
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ esshop ---
[INFO] esshop:esshop:war:0.0.1-SNAPSHOT
[INFO] +- commons-collections:commons-collections:jar:3.2.1:compile
[INFO] +- org.hibernate:hibernate:jar:3.2.2.ga:compile
[INFO] | \- (commons-collections:commons-collections:jar:2.1.1:compile - omitted for conflict with 3.2.1)
[INFO] +- org.hibernate:hibernate-annotations:jar:3.4.0.GA:compile
[INFO] | \- org.hibernate:hibernate-core:jar:3.3.0.SP1:compile
[INFO] | \- (commons-collections:commons-collections:jar:3.1:compile - omitted for conflict with 3.2.1)
[INFO] +- org.springframework.security:spring-security-core:jar:2.0.4:compile
[INFO] | \- (commons-collections:commons-collections:jar:3.2:compile - omitted for conflict with 3.2.1)
[INFO] +- org.apache.velocity:velocity:jar:1.5:compile
[INFO] | \- (commons-collections:commons-collections:jar:3.1:compile - omitted for conflict with 3.2.1)
[INFO] \- net.sf.json-lib:json-lib:jar:jdk15:2.4:compile
[INFO] \- (commons-collections:commons-collections:jar:3.2.1:compile - omitted for duplicate)