Maven倉庫配置研究

maven 倉庫地址有如下配置:

pom.xml

<pluginRepositories>
    <pluginRepository>
      <id>spring-snapshots</id>
      <name>Spring Snapshots</name>
      <url>https://repo.spring.io/snapshot</url>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </pluginRepository>
<pluginRepositories>    

<repositories>
    <repository>
      <id>spring-snapshots</id>
      <name>Spring Snapshots</name>
      <url>https://repo.spring.io/snapshot</url>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
</repositories>

settings.xml

<mirror>
    <id>CN</id>
    <name>OSChina Central</name>
    <url>http://maven.oschina.net/content/groups/public/</url>
    <mirrorOf>central</mirrorOf>
</mirror>
<profile>
     <id>dev</id>
     <repositories>
         <repository>
             <id>nexus-public</id>
             <name>Public Repositories</name>
             <url>http://192.168.2.126:8081/nexus/content/groups/public/</url>
         </repository>
    </repositories>
     <pluginRepositories>
         <pluginRepository>
             <id>public</id>
             <name>Public Repositories</name>
             <url>http://192.168.2.126:8081/nexus/content/groups/public/</url>
         </pluginRepository>
     </pluginRepositories>
     <activeProfiles>
        <activeProfile>dev</activeProfile>
    </activeProfiles>
</profile>   

首先我們有幾個問題:

  1. 這么多配倉庫的地方,配置的是什么倉庫?
  2. 多個倉庫使用哪一個,優(yōu)先級是什么?

第一個問題
配的倉庫其實就兩種
<repositories> 就一般的遠程倉庫
<pluginRepositories> 這個是下載插件的倉庫
私服和第三方遠程倉庫都是 遠程倉庫 只不過私服會緩存下來,方便第二次下載jar包局域網(wǎng)更快

第二個優(yōu)先級

  1. 按照settings.xml<repositories> 里的順序從上往下
  2. pom.xml<repositories> 優(yōu)先級是最低的
    也就是說你先 使用指定的倉庫 直接配置到<repositories> 第一個最快捷,更合理的方式是使用<activeProfiles>來切換吧

倉庫分類

  1. 本地倉庫
  2. 遠程倉庫
    1. 中央倉庫
    2. 私服
    3. 其它公共倉庫

本地倉庫 就是本地文件夾配置在settings.xml里的 <localRepositoryD:\maven_new_repository</localRepository

中央倉庫 只得就是maven剛安裝好后的maven中央倉庫<url>http://repo.maven.apache.org/maven2</url>

遠程倉庫 通過<repositories> <repository> 配置的就是遠程倉庫

私服 這是一種特殊的遠程倉庫,簡單來講就是為了緩存 其它遠程倉庫的請求

  1. 減少重復(fù)請求造成的外網(wǎng)帶寬消耗
  2. 有些構(gòu)件無法從外部倉庫獲得的時候,我們可以把這些構(gòu)件部署到內(nèi)部倉庫(私服)中,供內(nèi)部maven項目使用
  3. 降低中央倉庫的負荷:maven中央倉庫被請求的數(shù)量是巨大的,配置私服也可以大大降低中央倉庫的壓力
    插件倉庫 <pluginRepository> 這個是配置插件的下載倉庫
    學(xué)習(xí)下repositories配置
<repositories>  
        <repository>  
            <id>jboss</id>  
            <name>JBoss Repository</name>  
            <url>http://repository.jboss.com/maven2/</url>  
            <releases>  
                <updatePolicy>daily</updatePolicy><!-- never,always,interval n -->  
                <enabled>true</enabled>  
                <checksumPolicy>warn</checksumPolicy><!-- fail,ignore -->  
            </releases>  
            <snapshots>  
                <enabled>false</enabled>  
            </snapshots>  
            <layout>default</layout>  
        </repository>  
    </repositories>  

<updatePolicy>元素:表示更新的頻率,值有:never, always,interval,daily, daily 為默認(rèn)值
<checksumPolicy>元素:表示maven檢查和檢驗文件的策略,warn為默認(rèn)值
遠程倉庫的認(rèn)證配置在

 <server>  
       <id>same with repository id in pom</id>  
       <username>username</username>  
       <password>pwd</password>  
 </server>  

注:這里的id必須與POM中需要認(rèn)證的repository元素的Id一致。

如何將生成的項目部署到遠程倉庫
完成這項工作,也需要在POM中進行配置,這里有新引入了一個元素:<distributionManagement>
distributionManagement包含了2個子元素:repositorysnapshotRepository, 前者表示發(fā)布版本構(gòu)件的倉庫,后者表示快照版本的倉庫
這兩個元素都需要配置 id(該遠程倉庫的唯一標(biāo)識),name,url(表示該倉庫的地址)
向遠程倉庫中部署構(gòu)件,需要進行認(rèn)證。配置同上

maven到底是如何從倉庫中解析構(gòu)件的呢?
  1. 依賴范圍是system的時候->本地文件系統(tǒng)
  2. 嘗試直接從本地倉庫尋找構(gòu)件,如果發(fā)現(xiàn)相應(yīng)構(gòu)件,則解析成功
  3. 本地倉庫不存在相應(yīng)的構(gòu)件情況下,如果依賴的版本是顯示的發(fā)布版本構(gòu)件,則遍歷所有的遠程倉庫
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • |-1-更新內(nèi)容[6.從倉庫解析依賴的機制(重要)] 1Maven倉庫作用 倉庫用來存儲所有項目使用到構(gòu)件,在ma...
    zlcook閱讀 6,472評論 0 25
  • 簡介 概述 Maven 是一個項目管理和整合工具 Maven 為開發(fā)者提供了一套完整的構(gòu)建生命周期框架 Maven...
    閩越布衣閱讀 4,549評論 6 39
  • 在Maven世界中,依賴、插件、項目構(gòu)建完成后輸出的jar包都可以看作是一個構(gòu)件,任何一個構(gòu)件都有一組坐標(biāo)唯一標(biāo)識...
    SonyaBaby閱讀 716評論 0 0
  • 首先私服是一種衍生出來的特殊的Maven遠程倉庫,構(gòu)建私服的好處請看3.5私服 可以幫助大家建立私服的倉庫管理軟件...
    zlcook閱讀 10,838評論 0 32
  • 令我驚訝的是,有些人覺得編程并不令人興奮——只將它當(dāng)作是一份枯燥的工作。不過,雖然可能的確有很多無聊的編程工作,但...
    巴黎圣母小院閱讀 568評論 0 1

友情鏈接更多精彩內(nèi)容