Maven的依赖与最佳配置(转载自Maven实战 作者许晓斌)
1、依賴的配置
依賴配置的屬性:
<dependencies><dependency><groupId>...</groupId><artifactId>...</artifactId><version>...</version><type>...</type><scope>...</scope><optional>...</optional><exclusions><exclusion>...</exclusion></exclusions></dependency></dependencies>
- groupId、artifactId和Version:依賴的基本坐標,Maven根據坐標找到相應的依賴。
- type:依賴的類型,對應于項目坐標定義的packaging。默認為jar
- scope:依賴的范圍
- optional:標記依賴的可選性
- exclusions:用來排出傳遞性的依賴
2、依賴的范圍
maven有六種依賴范圍compile、test、provided、runtime、system和import(Maven2.0.9及以上)
- compile:編譯依賴范圍。默認配置。對于編譯、測試和運行的classpath都有效。
- test:測試依賴范圍。對于測試的classpath有效。例如JUnit
- provided:已提供依賴范圍。對于編譯、測試有效。運行時無效。如servlet-api,編譯測試的時候需要但是運行時在服務器提供,就不需要了。
- runtime:運行時依賴。對于測試和運行有效。編譯無效。
- system:系統范圍依賴。與provide幾乎一致。
<dependency><groupId>javax.sql</groupId><artifactId>jdbc-stdext</artifactId><version>2.0</version><scope>system</scope><systemPath>${java.home}/lib/rt.jar</systemPath> </dependency> - import:導入依賴范圍。不對三種classpath有影響。
3、傳遞性依賴
一個基于Spring Framework的項目,如果不適用Maven,在項目中就需要手動下載依賴。而Spring Framework又依賴其他的開源類庫,所以還要下載別的jar包。Maven的傳遞依賴機制可以解決這個問題。Maven會解析各個直接依賴的POM,將那些必要的簡介依賴以傳遞依賴的形式引入到當前的項目中。
| ? | compile | test | provided | ?????? runtime |
| compile | compile | -- | -- | runtime |
| test | test | -- | -- | test |
| provided | provided | -- | provided | provided |
| runtime | runtime | -- | -- | runtime |
可以看到當第二直接依賴范圍是compile時,傳遞依賴的范圍與第一依賴相同;當第二直接依賴范圍是test時,依賴不會傳遞;當第二直接依賴是provided時,只有第一直接依賴是provided時,傳遞依賴才是provided;當第二直接依賴的范圍是runtime時,傳遞依賴范圍與第一依賴相同,出了compile的范圍,這時候傳遞依賴為runtime。
4、依賴的調解
當依賴傳遞造成問題時,我們需要清楚地知道該傳遞性依賴的路徑。依賴調解的第一原則是:路徑最近著優先。例如項目A有這樣的依賴關系:A-->B-->C--X(1.0)? A--D--X(2.0),其中X(1.0)的路徑長度為3,而X(2.0)的長度為2,因此X(2.0)會被解析使用。
當第一條原則不能解決問題時,使用依賴調解的第二原則:第一聲明優先。如A-->B-->Y(1.0)? A-->C--Y(2.0)此時依靠第一原則無法判斷,這是后在POM中依賴的聲明順序決定了誰會被解析使用。順序最靠前的會被解析。
5、可選依賴
有一個項目實現了兩個特性,其中一個特性依賴與X,另一個特性依賴與Y,而這兩個特性是互斥的,用戶不可能同時使用兩個特性。那么在依賴屬性中添加Optional元素。表名為可選依賴。只會對當前項目有影響,而不會傳遞。
6、最佳實現
1、排除依賴
? 傳遞性依賴會給項目隱式的引入很多依賴,這簡化了項目依賴的管理。有時候也會帶來問題。當一個項目有第三方依賴,而這個第三方依賴引用了一個類庫的SNAPSHOT版本,而這個依賴會傳遞到項目中。造成項目的不穩定性。或者引用的類庫不在Maven的中央類庫中。我們想把這種有危害的依賴踢出。可以使用exclusion元素:
<dependency><groupId>...</groupId><artifactId>...</artifactId><version>...</version><exclusions><exclusion><groupId>com.test</groupId><artifactId>project-a</artifactId></exclusion></exclusions> </dependency> <dependency><groupId>com.test</groupId><artifactId>project-a</artifactId><version>1.1.0</version> </dependency>這樣我們就可以把project-a不穩定的版本踢出,添加一個新的版本1.1.0.。
2、歸類依賴
可以在POM文件中增加屬性如:
<properties><spring-version>4.1.3.RELEASE</spring-version> </properties><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${spring-version}</version></dependency>這樣當我們要修改版本的時候,只需要修改property的值就可以了。
3、優化依賴
我們可以通過命令查看當前項目已解析的依賴:
mvn dependency:list [INFO] ------------------------------------------------------------------------ [INFO] Building Account Email 1.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-dependency-plugin:2.8:list (default-cli) @ account-email --- [INFO] [INFO] The following files have been resolved: [INFO] commons-logging:commons-logging:jar:1.2:compile [INFO] junit:junit:jar:4.7:test [INFO] org.springframework:spring-beans:jar:4.1.3.RELEASE:compile [INFO] org.springframework:spring-expression:jar:4.1.3.RELEASE:compile [INFO] javax.activation:activation:jar:1.1:compile [INFO] org.springframework:spring-aop:jar:4.1.3.RELEASE:compile [INFO] org.springframework:spring-core:jar:4.1.3.RELEASE:compile [INFO] aopalliance:aopalliance:jar:1.0:compile [INFO] org.springframework:spring-context:jar:4.1.3.RELEASE:compile [INFO] com.icegreen:greenmail:jar:1.3.1b:compile [INFO] org.springframework:spring-context-support:jar:4.1.3.RELEASE:compile [INFO] org.slf4j:slf4j-api:jar:1.3.1:compile [INFO] javax.mail:mail:jar:1.4.1:compile [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.686 s [INFO] Finished at: 2014-12-20T14:13:26+08:00 [INFO] Final Memory: 7M/12M [INFO] ------------------------------------------------------------------------我們可以看到所有的依賴及其依賴范圍。
同時可以使用命令查看以來的結構:
mvn dependency:tree得出結果如下:
[INFO] ------------------------------------------------------------------------ [INFO] Building Account Email 1.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ account-email --- [INFO] com.juven.mvnbook.account:account-email:jar:1.0.0-SNAPSHOT [INFO] +- org.springframework:spring-core:jar:4.1.3.RELEASE:compile [INFO] | \- commons-logging:commons-logging:jar:1.2:compile [INFO] +- org.springframework:spring-beans:jar:4.1.3.RELEASE:compile [INFO] +- org.springframework:spring-context:jar:4.1.3.RELEASE:compile [INFO] | +- org.springframework:spring-aop:jar:4.1.3.RELEASE:compile [INFO] | | \- aopalliance:aopalliance:jar:1.0:compile [INFO] | \- org.springframework:spring-expression:jar:4.1.3.RELEASE:compile [INFO] +- org.springframework:spring-context-support:jar:4.1.3.RELEASE:compile [INFO] +- javax.mail:mail:jar:1.4.1:compile [INFO] | \- javax.activation:activation:jar:1.1:compile [INFO] +- junit:junit:jar:4.7:test [INFO] \- com.icegreen:greenmail:jar:1.3.1b:compile [INFO] \- org.slf4j:slf4j-api:jar:1.3.1:compile [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.656 s [INFO] Finished at: 2014-12-20T14:15:52+08:00 [INFO] Final Memory: 6M/12M [INFO] ------------------------------------------------------------------------同時可以使用另外一個指令來分析依賴的關系及完整性:
mvn dependency:analyze結果中指出我們需要的依賴及沒有使用的依賴:
[INFO] --- maven-dependency-plugin:2.8:analyze (default-cli) @ account-email --- [WARNING] Used undeclared dependencies found: [WARNING] org.springframework:spring-context:jar:4.1.3.RELEASE:compile [WARNING] Unused declared dependencies found: [WARNING] org.springframework:spring-core:jar:4.1.3.RELEASE:compile [WARNING] org.springframework:spring-beans:jar:4.1.3.RELEASE:compile [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.701 s [INFO] Finished at: 2014-12-20T14:18:28+08:00 [INFO] Final Memory: 7M/13M [INFO] ------------------------------------------------------------------------而這種依賴的出錯時很難查找的,所以先進性依賴的分析是非常有用的。
總結
以上是生活随笔為你收集整理的Maven的依赖与最佳配置(转载自Maven实战 作者许晓斌)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转载】通过搜狗站长平台提交网站域名变更
- 下一篇: PHP数据库CURD接口与实现(接口实战