日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

maven repository

 埃德溫會館 2015-12-15

什么是Maven倉庫

在不用Maven的時候,比如說以前我們用Ant構(gòu)建項目,在項目目錄下,往往會看到一個名為/lib的子目錄,那里存放著各類第三方依賴jar文件,如log4j.jar,junit.jar等等。每建立一個項目,你都需要建立這樣的一個/lib目錄,然后復(fù)制一對jar文件,這是很明顯的重復(fù)。重復(fù)永遠是噩夢的起點,多個項目不共用相同的jar文件,不僅會造成磁盤資源的浪費,也使得版本的一致性管理變得困難。此外,如果你使用版本管理工具,如SVN(你沒有使用版本管理工具?馬上試試SVN吧,它能幫你解決很多頭疼的問題),你需要將大量的jar文件提交到代碼庫里,可是版本管理工具在處理二進制文件方面并不出色。

Maven倉庫就是放置所有JAR文件(WAR,ZIP,POM等等)的地方,所有Maven項目可以從同一個Maven倉庫中獲取自己所需要的依賴JAR,這節(jié)省了磁盤資源。此外,由于Maven倉庫中所有的JAR都有其自己的坐標(biāo),該坐標(biāo)告訴Maven它的組ID,構(gòu)件ID,版本,打包方式等等,因此Maven項目可以方便的進行依賴版本管理。你也不在需要提交JAR文件到SCM倉庫中,你可以建立一個組織層次的Maven倉庫,供所有成員使用。

簡言之,Maven倉庫能幫助我們管理構(gòu)件(主要是JAR)。

 

本地倉庫 vs. 遠程倉庫

運行Maven的時候,Maven所需要的任何構(gòu)件都是直接從本地倉庫獲取的。如果本地倉庫沒有,它會首先嘗試從遠程倉庫下載構(gòu)件至本地倉庫,然后再使用本地倉庫的構(gòu)件。

比如說,你的項目配置了junit-3.8的依賴,在你運行mvn test 的時候,Maven需要使用junit-3.8的jar文件,它首先根據(jù)坐標(biāo)查找本地倉庫,如果找到,就直接使用。如果沒有,Maven會檢查可用的遠程倉庫配置,然后逐個嘗試這些遠程倉庫去下載junit-3.8的jar文件,如果遠程倉庫存在該文件,Maven會將其下載到本地倉庫中,繼而使用。如果嘗試過所有遠程倉庫之后,Maven還是沒能夠下載到該文件,它就會報錯。

Maven缺省的本地倉庫地址為${user.home}/.m2/repository 。也就是說,一個用戶會對應(yīng)的擁有一個本地倉庫。

你也可以自定義本地倉庫的位置,修改${user.home}/.m2/settings.xml

Xml代碼 復(fù)制代碼
  1. <settings>  
  2.   ...   
  3.   <localRepository>D:/java/repository</localRepository>  
  4.   ...   
  5. </settings>  
  1. <settings>  
  2.   ...  
  3.   <localRepository>D:/java/repository</localRepository>  
  4.   ...  
  5. </settings>  

你還可以在運行時指定本地倉庫位置:

mvn clean install -Dmaven.repo.local=/home/juven/myrepo/

還有一點需要理解的是,當(dāng)我們運行install的時候,Maven實際上是將項目生成的構(gòu)件安裝到了本地倉庫,也就是說,只有install了之后,其它項目才能使用此項目生成的構(gòu)件。

了解了本地倉庫,接著了解一下Maven缺省的遠程倉庫,即Maven中央倉庫。

安裝好Maven之后,我們可以建立一個簡單的項目,配置一些簡單的依賴,然后運行mvn clean install,項目就構(gòu)建好了。我們沒有手工的去下載任何jar文件,這一切都是因為Maven中央倉庫的存在,當(dāng)Maven在本地倉庫找不到需要的jar文件時,它會查找遠程倉庫,而一個原始的Maven安裝就自帶了一個遠程倉庫——Maven中央倉庫。

這個Maven中央倉庫是在哪里定義的呢?在我的機器上,我安裝了maven-2.0.10,我可以找到這個文件:${M2_HOME}/lib/maven-2.0.10-uber.jar ,打開該文件,能找到超級POM:/org/apache/maven/project/pom-4.0.0.xml ,它是所有Maven POM的父POM,所有Maven項目繼承該配置,你可以在這個POM中發(fā)現(xiàn)如下配置:

Xml代碼 復(fù)制代碼
  1. <repositories>  
  2.   <repository>  
  3.     <id>central</id>  
  4.     <name>Maven Repository Switchboard</name>  
  5.     <layout>default</layout>  
  6.     <url>http://repo1./maven2</url>  
  7.     <snapshots>  
  8.       <enabled>false</enabled>  
  9.     </snapshots>  
  10.   </repository>  
  11. </repositories>  
  1. <repositories>  
  2.   <repository>  
  3.     <id>central</id>  
  4.     <name>Maven Repository Switchboard</name>  
  5.     <layout>default</layout>  
  6.     <url>http://repo1./maven2</url>  
  7.     <snapshots>  
  8.       <enabled>false</enabled>  
  9.     </snapshots>  
  10.   </repository>  
  11. </repositories>  

關(guān)于遠程倉庫的配置,下面的小節(jié)我會詳細解釋,這里我們只要知道,中央倉庫的id為central,遠程url地址為http://repo1./maven2,它關(guān)閉了snapshot版本構(gòu)件下載的支持。

 

在POM中配置遠程倉庫

前面我們看到超級POM配置了ID為central的遠程倉庫,我們可以在POM中配置其它的遠程倉庫。這樣做的原因有很多,比如你有一個局域網(wǎng)的遠程倉庫,使用該倉庫能大大提高下載速度,繼而提高構(gòu)建速度,也有可能你依賴的一個jar在central中找不到,它只存在于某個特定的公共倉庫,這樣你也不得不添加那個遠程倉庫的配置。

這里我配置一個遠程倉庫指向中央倉庫的中國鏡像:

Xml代碼 復(fù)制代碼
  1. <project>  
  2. ...   
  3.   <repositories>  
  4.     <repository>  
  5.       <id>maven-net-cn</id>  
  6.       <name>Maven China Mirror</name>  
  7.       <url>http:///content/groups/public/</url>  
  8.       <releases>  
  9.         <enabled>true</enabled>  
  10.       </releases>  
  11.       <snapshots>  
  12.         <enabled>false</enabled>  
  13.       </snapshots>  
  14.     </repository>  
  15.   </repositories>  
  16.   <pluginRepositories>  
  17.     <pluginRepository>  
  18.       <id>maven-net-cn</id>  
  19.       <name>Maven China Mirror</name>  
  20.       <url>http:///content/groups/public/</url>  
  21.       <releases>  
  22.         <enabled>true</enabled>  
  23.       </releases>  
  24.       <snapshots>  
  25.         <enabled>false</enabled>  
  26.       </snapshots>       
  27.     </pluginRepository>  
  28.   </pluginRepositories>  
  29. ...   
  30. </project>  
  1. <project>  
  2. ...  
  3.   <repositories>  
  4.     <repository>  
  5.       <id>maven-net-cn</id>  
  6.       <name>Maven China Mirror</name>  
  7.       <url>http:///content/groups/public/</url>  
  8.       <releases>  
  9.         <enabled>true</enabled>  
  10.       </releases>  
  11.       <snapshots>  
  12.         <enabled>false</enabled>  
  13.       </snapshots>  
  14.     </repository>  
  15.   </repositories>  
  16.   <pluginRepositories>  
  17.     <pluginRepository>  
  18.       <id>maven-net-cn</id>  
  19.       <name>Maven China Mirror</name>  
  20.       <url>http:///content/groups/public/</url>  
  21.       <releases>  
  22.         <enabled>true</enabled>  
  23.       </releases>  
  24.       <snapshots>  
  25.         <enabled>false</enabled>  
  26.       </snapshots>      
  27.     </pluginRepository>  
  28.   </pluginRepositories>  
  29. ...  
  30. </project>  

我們先看一下<repositories>的配置,你可以在它下面添加多個<repository> ,每個<repository>都有它唯一的ID,一個描述性的name,以及最重要的,遠程倉庫的url。此外,<releases><enabled>true</enabled></releases>告訴Maven可以從這個倉庫下載releases版本的構(gòu)件,而<snapshots><enabled>false</enabled></snapshots>告訴Maven不要從這個倉庫下載snapshot版本的構(gòu)件。禁止從公共倉庫下載snapshot構(gòu)件是推薦的做法,因為這些構(gòu)件不穩(wěn)定,且不受你控制,你應(yīng)該避免使用。當(dāng)然,如果你想使用局域網(wǎng)內(nèi)組織內(nèi)部的倉庫,你可以激活snapshot的支持。

關(guān)于<repositories>的更詳細的配置及相關(guān)解釋,請參考:http://www./books/maven-book/reference_zh/apas02s08.html。

至于<pluginRepositories>,這是配置Maven從什么地方下載插件構(gòu)件(Maven的所有實際行為都由其插件完成)。該元素的內(nèi)部配置和<repository>完全一樣,不再解釋。

 

在settings.xml中配置遠程倉庫

我們知道了如何在POM中配置遠程倉庫,但考慮這樣的情況:在一個公司內(nèi)部,同時進行這3個項目,而且以后隨著這幾個項目的結(jié)束,越來越多的項目會開始;同時,公司內(nèi)部建立一個Maven倉庫。我們統(tǒng)一為所有這些項目配置該倉庫,于是不得不為每個項目提供同樣的配置。問題出現(xiàn)了,這是重復(fù) !

其實我們可以做到只配置一次,在哪里配置呢?就是settings.xml。

不過事情沒有那么簡單,不是簡單的將POM中的<repositories>及<pluginRepositories>元素復(fù)制到settings.xml中就可以,setting.xml不直接支持 這兩個元素。但我們還是有一個并不復(fù)雜的解決方案,就是利用profile,如下:

Xml代碼 復(fù)制代碼
  1. <settings>  
  2.   ...   
  3.   <profiles>  
  4.     <profile>  
  5.       <id>dev</id>  
  6.       <!-- repositories and pluginRepositories here-->  
  7.     </profile>  
  8.   </profiles>  
  9.   <activeProfiles>  
  10.     <activeProfile>dev</activeProfile>  
  11.   </activeProfiles>  
  12.   ...   
  13. </settings>  
  1. <settings>  
  2.   ...  
  3.   <profiles>  
  4.     <profile>  
  5.       <id>dev</id>  
  6.       <!-- repositories and pluginRepositories here-->  
  7.     </profile>  
  8.   </profiles>  
  9.   <activeProfiles>  
  10.     <activeProfile>dev</activeProfile>  
  11.   </activeProfiles>  
  12.   ...  
  13. </settings>  

這里我們定義一個id為dev的profile,將所有repositories以及pluginRepositories元素放到這個profile中,然后,使用<activeProfiles>元素自動激活該profile。這樣,你就不用再為每個POM重復(fù)配置倉庫。

使用profile為settings.xml添加倉庫提供了一種用戶全局范圍的倉庫配置。

 

鏡像

如果你的地理位置附近有一個速度更快的central鏡像,或者你想覆蓋central倉庫配置,或者你想為所有POM使用唯一的一個遠程倉庫(這個遠程倉庫代理的所有必要的其它倉庫),你可以使用settings.xml中的mirror配置。

以下的mirror配置用覆蓋了Maven自帶的central:

Xml代碼 復(fù)制代碼
  1. <settings>  
  2. ...   
  3.   <mirrors>  
  4.     <mirror>  
  5.       <id>maven-net-cn</id>  
  6.       <name>Maven China Mirror</name>  
  7.       <url>http:///content/groups/public/</url>  
  8.       <mirrorOf>central</mirrorOf>  
  9.     </mirror>  
  10.   </mirrors>  
  11. ...   
  12. </settings>  
  1. <settings>  
  2. ...  
  3.   <mirrors>  
  4.     <mirror>  
  5.       <id>maven-net-cn</id>  
  6.       <name>Maven China Mirror</name>  
  7.       <url>http:///content/groups/public/</url>  
  8.       <mirrorOf>central</mirrorOf>  
  9.     </mirror>  
  10.   </mirrors>  
  11. ...  
  12. </settings>  

 

這里唯一需要解釋的是<mirrorOf>,這里我們配置central的鏡像,我們也可以配置一個所有倉庫的鏡像,以保證該鏡像是Maven唯一使用的倉庫:

Xml代碼 復(fù)制代碼
  1. <settings>  
  2. ...   
  3.   <mirrors>  
  4.     <mirror>  
  5.       <id>my-org-repo</id>  
  6.       <name>Repository in My Orgnization</name>  
  7.       <url>http://192.168.1.100/maven2</url>  
  8.       <mirrorOf>*</mirrorOf>  
  9.     </mirror>  
  10.   </mirrors>  
  11. ...   
  12. </settings>  
  1. <settings>  
  2. ...  
  3.   <mirrors>  
  4.     <mirror>  
  5.       <id>my-org-repo</id>  
  6.       <name>Repository in My Orgnization</name>  
  7.       <url>http://192.168.1.100/maven2</url>  
  8.       <mirrorOf>*</mirrorOf>  
  9.     </mirror>  
  10.   </mirrors>  
  11. ...  
  12. </settings>  

關(guān)于更加高級的鏡像配置,可以參考:http://maven./guides/mini/guide-mirror-settings.html。

 

分發(fā)構(gòu)件至遠程倉庫

mvn install 會將項目生成的構(gòu)件安裝到本地Maven倉庫,mvn deploy 用來將項目生成的構(gòu)件分發(fā)到遠程Maven倉庫。本地Maven倉庫的構(gòu)件只能供當(dāng)前用戶使用,在分發(fā)到遠程Maven倉庫之后,所有能訪問該倉庫的用戶都能使用你的構(gòu)件。

我們需要配置POM的distributionManagement來指定Maven分發(fā)構(gòu)件的位置,如下:

Xml代碼 復(fù)制代碼
  1. <project>     
  2.   ...     
  3.   <distributionManagement>     
  4.     <repository>     
  5.       <id>nexus-releases</id>     
  6.       <name>Nexus Release Repository</name>     
  7.       <url>http://127.0.0.1:8080/nexus/content/repositories/releases/</url>     
  8.     </repository>     
  9.     <snapshotRepository>     
  10.       <id>nexus-snapshots</id>     
  11.       <name>Nexus Snapshot Repository</name>     
  12.       <url>http://127.0.0.1:8080/nexus/content/repositories/snapshots/</url>     
  13.     </snapshotRepository>     
  14.   </distributionManagement>     
  15.   ...     
  16. </project>    
  1. <project>    
  2.   ...    
  3.   <distributionManagement>    
  4.     <repository>    
  5.       <id>nexus-releases</id>    
  6.       <name>Nexus Release Repository</name>    
  7.       <url>http://127.0.0.1:8080/nexus/content/repositories/releases/</url>    
  8.     </repository>    
  9.     <snapshotRepository>    
  10.       <id>nexus-snapshots</id>    
  11.       <name>Nexus Snapshot Repository</name>    
  12.       <url>http://127.0.0.1:8080/nexus/content/repositories/snapshots/</url>    
  13.     </snapshotRepository>    
  14.   </distributionManagement>    
  15.   ...    
  16. </project>    

Maven區(qū)別對待release版本的構(gòu)件和snapshot版本的構(gòu)件,snapshot為開發(fā)過程中的版本,實時,但不穩(wěn)定,release版本則比較穩(wěn)定。Maven會根據(jù)你項目的版本來判斷將構(gòu)件分發(fā)到哪個倉庫。

一般來說,分發(fā)構(gòu)件到遠程倉庫需要認(rèn)證,如果你沒有配置任何認(rèn)證信息,你往往會得到401錯誤。這個時候,如下在settings.xml中配置認(rèn)證信息:

Xml代碼 復(fù)制代碼
  1. <settings>     
  2.   ...     
  3.   <servers>     
  4.     <server>     
  5.       <id>nexus-releases</id>     
  6.       <username>admin</username>     
  7.       <password>admin123</password>     
  8.     </server>     
  9.     <server>     
  10.       <id>nexus-snapshots</id>     
  11.       <username>admin</username>     
  12.       <password>admin123</password>     
  13.     </server>       
  14.   </servers>     
  15.   ...     
  16. </settings>  
  1. <settings>    
  2.   ...    
  3.   <servers>    
  4.     <server>    
  5.       <id>nexus-releases</id>    
  6.       <username>admin</username>    
  7.       <password>admin123</password>    
  8.     </server>    
  9.     <server>    
  10.       <id>nexus-snapshots</id>    
  11.       <username>admin</username>    
  12.       <password>admin123</password>    
  13.     </server>      
  14.   </servers>    
  15.   ...    
  16. </settings>  

需要注意的是,settings.xml中server元素下id的值必須與POM中repository或snapshotRepository下id的值完全一致。將認(rèn)證信息放到settings下而非POM中,是因為POM往往是它人可見的,而settings.xml是本地的。

 

小結(jié)

本文介紹了Maven倉庫,它是什么?本地倉庫,遠程倉庫,中央倉庫具體是指什么?并介紹了如何在POM中配置項目層次的倉庫,在settings中配置用戶層次的倉庫,以及mirror。本文還介紹了如何安裝構(gòu)件到本地倉庫,如何分發(fā)構(gòu)件至倉庫。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多