聊一聊 Maven 依賴管理

轉(zhuǎn)自:data4
鏈接:www.jianshu.com/p/f6ca45865025
管理包依賴是 Maven 核心功能之一,下面通過如何引入 jar 包;如何解析 jar 包依賴;包沖突是如何產(chǎn)生;如何解決包沖突;依賴管理解決什么問題;什么是依賴范圍;使用包依賴的最佳實(shí)踐等 6 個(gè)問題來介紹。
如何引入 jar 包
例如:使用 zookeeper client
<dependencies><dependency><groupId>org.apache.hadoopgroupId><artifactId>zookeeperartifactId><version>3.3.1version>dependency>dependencies>
Maven 如何解析 jar 包依賴——傳遞依賴
例如:上述 pom.xml 引入 zookeeper 依賴,實(shí)際引入的 jar 包有:

包沖突如何產(chǎn)生?
如果 pom.xml 文件中引入了 A 和 E 之后,按照 Maven 傳遞依賴原則,工程內(nèi)需要引入的實(shí)際 Jar 包將會(huì)有:A B C D1 和 E F D2,因此 D1,D2 將會(huì)產(chǎn)生包沖突。
如何解決包沖突
Maven 默認(rèn)處理策略
最短路徑優(yōu)先
Maven 面對(duì) D1 和 D2 時(shí),會(huì)默認(rèn)選擇最短路徑的那個(gè) jar 包,即 D2。E->F->D2 比 A->B->C->D1 路徑短 1。最先聲明優(yōu)先
如果路徑一樣的話,舉個(gè)??:A->B->C1, E->F->C2 ,兩個(gè)依賴路徑長度都是 2,那么就選擇最先聲明。
移除依賴
舉個(gè)??:將 zookeeper 的 jline 依賴排除
<dependency><groupId>org.apache.hadoopgroupId><artifactId>zookeeperartifactId><version>3.3.1version><exclusions><exclusion><groupId>jlinegroupId><artifactId>jlineartifactId>exclusion>exclusions>dependency>
mvn dependency:helpmvn dependency:analyzemvn dependency:treemvn dependency:tree -Dverbose依賴管理解決什么問題
舉個(gè)??:有兩個(gè)模塊 projectA, projectB,它們的依賴分別如下所示:
projectA:
<project>...<dependencies><dependency><groupId>group-agroupId><artifactId>artifact-aartifactId><version>1.0version><exclusions><exclusion><groupId>group-cgroupId><artifactId>excluded-artifactartifactId>exclusion>exclusions>dependency><dependency><groupId>group-agroupId><artifactId>artifact-bartifactId><version>1.0version><type>bartype><scope>runtimescope>dependency>dependencies>project>projectB:
<project>...<dependencies><dependency><groupId>group-cgroupId><artifactId>artifact-bartifactId><version>1.0version><type>wartype><scope>runtimescope>dependency><dependency><groupId>group-agroupId><artifactId>artifact-bartifactId><version>1.0version><type>bartype><scope>runtimescope>dependency>dependencies>project>
<project>...<dependencyManagement><dependencies><dependency><groupId>group-agroupId><artifactId>artifact-bartifactId><version>1.0version><type>bartype><scope>runtimescope>dependency>dependencies>dependencyManagement>project>則 projectA 和 projectB 均不需要指定 group-a/artifact-b 的 version 信息。 未來升級(jí) version 信息時(shí),只需要在 parent 內(nèi)部指定。 projectA:
<project>...<dependencies><dependency><groupId>group-agroupId><artifactId>artifact-aartifactId><version>1.0version><exclusions><exclusion><groupId>group-cgroupId><artifactId>excluded-artifactartifactId>exclusion>exclusions>dependency><dependency><groupId>group-agroupId><artifactId>artifact-bartifactId>dependency>dependencies>project>projectB:
<project>...<dependencies><dependency><groupId>group-cgroupId><artifactId>artifact-bartifactId><version>1.0version><type>wartype><scope>runtimescope>dependency><dependency><groupId>group-agroupId><artifactId>artifact-bartifactId>dependency>dependencies>project>依賴范圍
scope 有哪些屬性:compile, provided, runtime, test, system 等。
詳細(xì)參考:依賴范圍
最佳實(shí)踐
項(xiàng)目中源代碼使用的 jar 包一定在 pom.xml 中顯示引用。
經(jīng)常 check 一下包沖突,檢查是否需要處理。
當(dāng)使用多個(gè)模塊時(shí),parent 一定要使用包管理模塊來規(guī)范 Jar 包版本,而不是包依賴模塊直接引入依賴。dependencyManagement vs dependencies
一個(gè)注解搞定 SpringBoot 接口防刷,還有誰不會(huì)?
面試官:談?wù)凪ySQL的limit用法、邏輯分頁和物理分頁
評(píng)論
圖片
表情
