Maven入門-4.Maven的依賴


1、Maven的依賴

Maven的依賴通過dependencis元素來配置依賴,這是Maven最強大的特性之一。它支持傳遞性依賴。

1.1 添加依賴

在Maven中需要使用在dependencies中定義一個或者多個dependency元素,來聲明項目的一個或者多個依賴。
每個依賴元素dependency包括:

例如:為項目添加junit測試的jar包,可以按如下方式定義

 
 
 
         
  1. <project xmlns = " http://maven.apache.org/POM/4.0.0 " xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation = " http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd " >
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.fz</groupId>
  4. <artifactId>ShiroTest</artifactId>
  5. <packaging>war</packaging>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <name>ShiroTest Maven Webapp</name>
  8. <url> http://maven.apache.org </url>
  9. <dependencies>
  10. <dependency>
  11. <groupId>junit</groupId>
  12. <artifactId>junit</artifactId>
  13. <version>4.11</version>
  14. <!--不聲明依賴范圍scope,默認是compile-->
  15. <scope>test</scope>
  16. </dependency>
  17. </dependencies>
  18. </project>

1.2 依賴范圍(sope)

 
 
 
         
  1. <dependency>
  2. <!--
  3. compile(默認):編譯范圍的依賴,它在編譯和打包的時候都會把該依賴打包進去
  4. test:測試依賴范圍,它在編譯和打包的時候都不會把該依賴打包進去
  5. provided:在編譯和測試范圍有效,最后生成war包時不會打包進去。
  6. runtime:運行時依賴,編譯的時候不依賴。
  7. system:系統依賴范圍
  8. import:導入依賴范圍
  9. -->
  10. <scope></scope>
  11. </dependency>

依賴范圍與classpath的關系

依賴范圍Scope 對於編譯classpath有效 對於測試classpath有效 對於運行時classpath有效 例子
compile Y Y Y spring-core
test Y JUnit
provided Y Y servlet-api
runtime Y Y JDBC驅動實現
system Y Y 本地的,Maven倉庫之外的類庫文件

1.3 依賴的傳遞性

在Maven中一個依賴不僅僅是一個JAR。它是一個POM文件,這個POM可能也聲明了對其它構件的依賴。這些依賴的依賴叫做傳遞性依賴
所謂傳遞性依賴就是: 如果項目A依賴於項目B,項目B自身依賴於項目C,那么項目A它也依賴於項目C的依賴。
這種依賴是基於compile這個范圍進行依賴
假如你的項目中需要依賴一個A庫,這個A庫自身又依賴了3個其它庫。當你在dependencies中配置了A庫的依賴的時候,則Maven會自動依賴3個其他庫。不用你再自己去依賴了。就像Spring和Hibernate一樣。你配置的時候,只需要配置Spring和Hibernate即可。
Maven在下載相應依賴的jar的時候,還會下載該jar的pom文件。這個文件對於傳遞性依賴特別重要。
這個pom文件跟我們現在編寫的pom.xml文件沒太大區別,打開后其實也是一樣。
如下是hibernate-core-4.3.10.Final.jar的pom文件(hibernate-core-4.3.10.Final.pom)

 
 
 
         
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>org.hibernate</groupId>
  5. <artifactId>hibernate-core</artifactId>
  6. <version>4.3.10.Final</version>
  7. <dependencies>
  8. <dependency>
  9. <groupId>org.jboss.logging</groupId>
  10. <artifactId>jboss-logging</artifactId>
  11. <version>3.1.3.GA</version>
  12. <scope>compile</scope>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.jboss.logging</groupId>
  16. <artifactId>jboss-logging-annotations</artifactId>
  17. <version>1.2.0.Beta1</version>
  18. <scope>compile</scope>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.jboss.spec.javax.transaction</groupId>
  22. <artifactId>jboss-transaction-api_1.2_spec</artifactId>
  23. <version>1.0.0.Final</version>
  24. <scope>compile</scope>
  25. </dependency>
  26. <dependency>
  27. <groupId>dom4j</groupId>
  28. <artifactId>dom4j</artifactId>
  29. <version>1.6.1</version>
  30. <scope>compile</scope>
  31. </dependency>
  32. <!-----------省略部分----------->
  33. </dependencies>
  34. <name>Core Hibernate O/RM functionality</name>
  35. <description>The core O/RM functionality as provided by Hibernate</description>
  36. <url>http://hibernate.org</url>
  37. <organization>
  38. <name>Hibernate.org</name>
  39. <url>http://hibernate.org</url>
  40. </organization>
  41. <issueManagement>
  42. <system>jira</system>
  43. <url>https://hibernate.atlassian.net/browse/HHH</url>
  44. </issueManagement>
  45. <scm>
  46. <url>http://github.com/hibernate/hibernate-orm</url>
  47. <connection>scm:git:http://github.com/hibernate/hibernate-orm.git</connection>
  48. <developerConnection>scm:git:git@github.com:hibernate/hibernate-orm.git</developerConnection>
  49. </scm>
  50. <licenses>
  51. <license>
  52. <name>GNU Lesser General Public License</name>
  53. <url>http://www.gnu.org/licenses/lgpl-2.1.html</url>
  54. <comments>See discussion at http://hibernate.org/license for more details.</comments>
  55. <distribution>repo</distribution>
  56. </license>
  57. </licenses>
  58. <developers>
  59. <developer>
  60. <id>hibernate-team</id>
  61. <name>The Hibernate Development Team</name>
  62. <organization>Hibernate.org</organization>
  63. <organizationUrl>http://hibernate.org</organizationUrl>
  64. </developer>
  65. </developers>
  66. </project>

可以看到hibernate-core-4.3.10.Final.pom文件中已經定義了hibernate-core-4.3.10.jar自己所依賴的jar包了。

1.2.1 依賴傳遞性的沖突問題

1. 第一種情況


項目A依賴於項目B1.0 ,項目C依賴於項目B1.1。項目D同時依賴於項目A和C,那么哪一個寫在前面就先依賴哪個版本。
也就是說到底依賴於log4j1.2.17還是log4j1.2.16,決定於它的dependency出現的先后順序。
哪個依賴的順序在前面就依賴哪個
可見上面user-core在user-log前面,而user-core的log4j是1.2.17。那么最終user-dao的log4j就是1.2.17版本
同理,如果換個位置。user-log在user-core的前面。

 
 
 
         
  1. <dependencies>
  2. <dependency>
  3. <groupId>com.fz.user</groupId>
  4. <artifactId>user-log</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.fz.user</groupId>
  9. <artifactId>user-core</artifactId>
  10. <version>0.0.1-SNAPSHOT</version>
  11. </dependency>
  12. </dependencies>

那么結果就是

2. 第二種情況


如果路徑的長短不一致,就選擇最小路徑
user-dao中的log4j路徑:user-dao=====>user-core=====>log4j1.2.17
user-log中的log4j路徑:user-log=====>log4j1.2.16
可見user-log的log4j路徑要短一些,所以這里是1.2.16

如果這里把user-log換成user-core的話,那么user-core的log4j路徑也是比user-dao的路徑短。結果肯定是log4j1.2.17

假如我不想依賴user-core和user-log兩個模塊中的log4j呢?
那就自己再添加一個dependency試試。

可以看出Maven會優先依賴自己定義的那個dependeny

1.2.2 通過exclusions元素排除不想要的傳遞性依賴

以上的問題可以通過更改相應的順序來確定依賴的版本,當然也可以使用依賴的排除功能。來精確的控制所使用的版本。
排除依賴:就是將該模塊中的**jar包給移除掉。
例如:以下的情況肯定是使用了user-log的log4j版本,因為它的順序在前面。假如現在不想通過調整順序來改版本那就可以使用排除依賴。

排除依賴是在dependency元素下的exclusions元素,例如:我們把user-log中的log4j排除掉,那就只能去user-core中找log4j了

可以看出雖然user-log寫在了上面,但是通過exclusion還是會把該模塊中的log4j給排除掉。

1.2.3 依賴傳遞性沖突問題解決辦法總結

  1. 通過調整dependency的順序來解決:哪個依賴的順序在前面就依賴哪個
  2. 自己添加一個dependeny來解決:因為該路徑是最小的。
  3. 通過exclusions元素排除不想要的傳遞性依賴

1.4 依賴版本的界限

在依賴某個項目的時候,你可以不必指定特定的某個版本。也可以指定一個版本范圍
(,) 不包含
[,] 包含
例如:依賴一個Junit的大於等於3.8 但是小於4.0 的版本

 
 
 
         
  1. <dependency>
  2. <groupId>junit</groupId>
  3. <artifactId>junit</artifactId>
  4. <version>[3.8,4.0)</version>
  5. </dependency>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM