Maven 本地倉庫,遠程倉庫,中央倉庫,Nexus私服,鏡像 詳解

一. 本地倉庫

本地倉庫是遠程倉庫的一個緩衝和子集,當你構建Maven項目的時候,首先會從本地倉庫查找資源,如果沒有,那麼Maven會從遠程倉庫下載到你本地倉庫。這樣在你下次使用的時候就不需要從遠程下載了。如果你所需要的jar包版本在本地倉庫沒有,而且也不存在於遠程倉庫,Maven在構建的時候會報錯,這種情況可能發生在有些jar包的新版本沒有在Maven倉庫中及時更新。

Maven缺省的本地倉庫地址爲${user.home}/.m2/repository 。也就是說,一個用戶會對應的擁有一個本地倉庫。當然你可以通過修改${user.home}/.m2/settings.xml 配置這個地址:

Xml代碼

  1. <settings>  

  2.   <localRepository> E:/repository/maven/repos</localRepository>  

  3. </settings>   

如果你想讓所有的用戶使用統一的配置那麼你可以修改Maven主目錄下的setting.xml:

${M2_HOME}/conf/setting.xml


二. 遠程倉庫

除本地倉庫以外的倉庫都叫做遠程倉庫

本地倉庫配置在:  <localRepository> E:/repository/maven/repos</localRepository>  

遠程倉庫配置在: 

             <profiles>
                   <profile>  
                           <id></id>  
                           <repositories> 

                                    <repository>遠程倉庫配置</repository> 

                           </repositories> 

                   </profile>
              </profiles> 

三. 中央倉庫

中央倉庫也屬於遠程倉庫的一種,特徵就是 <id>central</id> id名爲 central,

即,告訴Maven從外網的哪個地方下載jar包
Maven的安裝目錄中,在lib目錄下,maven-model-builder-3.1.0.jar中,有一個默認的pom.xml文件
其中就配置了Maven默認連接的中心倉庫

Maven的中央倉庫地址默認是:https://repo.maven.apache.org/maven2/,可以通過修改settings.xml文件來修改默認的中央倉庫地址:

<profile>
  <id>repository-profile</id>
  <repositories>
    <repository>
      <id>central</id>
      <name>Central Repository</name>
      <layout>default</layout>
      <url>http://maven.aliyun.com/nexus/content/groups/public</url>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
      <releases>
        <enabled>true</enabled>
      </releases>
    </repository>
  </repositories>
</profile>

要注意的是如果修改的是中央倉庫地址,那麼repository下面的id標籤值一定得是central,此外,還需要激活這個profile才能生效,這裏的標籤值就是profile標籤下面的id標籤值

<activeProfiles>
	<activeProfile>repository-profile</activeProfile>
</activeProfiles>


四. Nexus私服

私服也屬於遠程倉庫的一種,只是這個遠程倉庫的地址是本地服務器而已

配置在局域網環境中,爲局域網中所有開發人員提供jar包的統一管理
本地倉庫(本機)--->私服(局域網)--->中心倉庫(外部網絡)
 
私服的安裝
1.下載NEXUS,http://www.sonatype.org
2.解壓
3.配置環境變量:
新建環境變量:NEXUS_HOME = E:\soft\nexus-2.5.1-01
加入到path中:%NEXUS_HOME%\bin;
4.打開CMD命令行
C:\Users\Administrator>nexus install     安裝服務
C:\Users\Administrator>nexus start        啓動服務
C:\Users\Administrator>nexus uninstall  卸載服務
5.訪問私服
使用默認賬戶:admin 密碼:admin123
NEXUS內部使用Jetty作爲服務器
 http://localhost:8081/nexus   【界面用extjs開發的】
倉庫的分類
查看Repository
host倉庫--->內部項目的發佈倉庫
Snapshots   發佈內部snapshots版本的倉庫
Releases     發佈內部release版本的倉庫
3rd party      發佈第3方jar包的倉庫,如oracle數據庫驅動,open-189.jar
 
proxy倉庫--->從遠程中心倉庫查找jar包的倉庫
Apache Snapshots    查找Apache項目的快照版本的倉庫
Central   中心倉庫http://repo1.maven.org/maven2/
Codehaus Snapshots    查找Codehaus 的快照版本的倉庫
 
group倉庫--->把倉庫按組劃分,以組爲單位進行管理
 
virtual倉庫  
 
私服的配置/ Repository的配置
在parent模塊的pom.xml中加入私服的配置,讓Maven從私服下載jar包,而不直接去遠程倉庫下載。
默認情況下,Maven下載jar包將直接連接到外網http://repo1.maven.org/maven2/去下載;
安裝私服之後,讓Maven下載jar包先從私服查找,如果沒有,再從外網下載並保存在私服上
在POM在加入下面的配置,其中url爲NEXUS私服的Public Repository對外的地址
以後,Maven下載構建(jar包或插件)都將從這裏開始下載
Xml代碼  
<project>
  
  ...
  
  <!-- 配置私服地址 -->
  <repositories>
    <repository>
      <id>nexus</id>
      <url>http://localhost:8081/nexus/content/groups/public/</url>
      <snapshots><enabled>true</enabled></snapshots>
      <releases><enabled>true</enabled></releases>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>nexus</id>
      <url>http://localhost:8081/nexus/content/groups/public/</url>
      <snapshots><enabled>true</enabled></snapshots>
      <releases><enabled>true</enabled></releases>
    </pluginRepository>
  </pluginRepositories>

  ...

<project>
 

通過settings.xml來配置私服
由於所有的Maven項目都會用settings.xml中的配置進行解析,如果將Repository配置到這個文件中,那麼對所有的Maven項目都將生效。
此時,Maven項目中的POM文件就不需要再配置私服地址了!
注意:修改settings.xml文件時,看IDE中關聯的是哪個settings文件。
如C:\user\.m2目錄下可能存在,Maven的解壓目錄下也存在,具體修改哪個根據實際情況而定。如,Eclipse下,查看Maven的User Settings選項即能看到關聯。
我的IDE關聯的是Maven\conf目錄下的settings.xml:
E:\soft\apache-maven-3.1.0\conf\settings.xml
首先,通過<profile/>添加Repository和pluginRepository

Xml代碼 

<settings>  
  
  ...  
  
  <profiles>  
     <profile>  
      <id>profile-nexus</id>  
  
      <repositories>  
        <repository>  
          <id>nexus</id>  
          <url>http://localhost:8081/nexus/content/groups/public/</url>  
          <snapshots><enabled>true</enabled></snapshots>  
          <releases><enabled>true</enabled></releases>  
        </repository>  
      </repositories>  
      <pluginRepositories>  
        <pluginRepository>  
          <id>nexus</id>  
          <url>http://localhost:8081/nexus/content/groups/public/</url>  
          <snapshots><enabled>true</enabled></snapshots>  
          <releases><enabled>true</enabled></releases>  
        </pluginRepository>  
      </pluginRepositories>  
    </profile>  
  </profiles>  
  
  ...  
  
</settings> 


然後,使用<activeProfiles>對上面的配置進行激活(通過配置的id標識進行激活)
 
Xml代碼
<activeProfiles>  
  <activeProfile>profile-nexus</activeProfile>  
</activeProfiles> 

 
現在,本地機器上創建Maven項目,都會使用settings中有關倉庫的配置了
本地倉庫:
<localRepository>E:/repository/maven/repos</localRepository>
本地Maven下載的依賴包和插件都將放到E:/repository/maven/repos目錄中
私服:
本地所有Maven項目,下載構建都統一從http://localhost:8081/nexus/content/groups/public/ 下載!
【私服上不存在某個構建時,再從遠程倉庫下載】
遠程倉庫:
如果遠程倉庫連接不上,則通過nexus修改central的地址即可!
當前使用Maven的默認配置:http://repo1.maven.org/maven2/

五. 鏡像

Maven的鏡像是指如果倉庫X可以提供倉庫Y存儲的所有內容,那麼就可以認爲X是Y的一個鏡像,也就是說任何一個可以從倉庫Y獲得的依賴,都能夠從它的鏡像中獲取。比如阿里的Maven倉庫http://maven.aliyun.com/nexus/content/groups/public就可以理解爲是中央倉庫https://repo.maven.apache.org/maven2/在中國的鏡像,由於地理位置的因素,該鏡像往往能夠提供比中央倉庫更快的服務。要爲一個倉庫配置鏡像只需要修改settings.xml文件,如下,其中的mirrorOf標籤值就是倉庫的id標籤值,中央倉庫就是默認就是central,表示這是爲中央倉庫配置的鏡像,以後所有的依賴下載都會從這個鏡像中進行下載。

<mirror>
  <id>central-repository-mirror</id>
  <name>Central Repository Mirror</name>
  <mirrorOf>central</mirrorOf>
  <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

repository是指在局域網內部搭建的repository,它跟central repository, jboss repository等的區別僅僅在於其URL是一個內部網址 
mirror則相當於一個代理,它會攔截去指定的遠程repository下載構件的請求,然後從自己這裏找出構件回送給客戶端。配置mirror的目的一般是出於網速考慮。 

不過,很多internal repository搭建工具往往也提供mirror服務,比如Nexus就可以讓同一個URL,既用作internal repository,又使它成爲所有repository的mirror。

高級的鏡像配置: 
1.<mirrorOf>*</mirrorOf> 
匹配所有遠程倉庫。這樣所有pom中定義的倉庫都不生效
2.<mirrorOf>external:*</mirrorOf> 
匹配所有遠程倉庫,使用localhost的除外,使用file://協議的除外。也就是說,匹配所有不在本機上的遠程倉庫。 
3.<mirrorOf>repo1,repo2</mirrorOf> 
匹配倉庫repo1和repo2,使用逗號分隔多個遠程倉庫。 
4.<mirrorOf>*,!repo1</miiroOf> 
匹配所有遠程倉庫,repo1除外,使用感嘆號將倉庫從匹配中排除。

mirrors可以配置多個mirror,每個mirror有id,name,url,mirrorOf屬性,id是唯一標識一個mirror就不多說了,name貌似沒多大用,相當於描述,url是官方的庫地址,mirrorOf代表了一個鏡像的替代位置,例如central就表示代替官方的中央庫。

我本以爲鏡像庫是一個分庫的概念,就是說當a.jar在第一個mirror中不存在的時候,maven會去第二個mirror中查詢下載。但事實卻不是這樣,當第一個mirror中不存在a.jar的時候,並不會去第二個mirror中查找,甚至於,maven根本不會去其他的mirror地址查詢。

後來終於知道,maven的mirror是鏡像,而不是“分庫”,只有當前一個mirror無法連接的時候,纔會去找後一個,類似於備份和容災。

還有,mirror也不是按settings.xml中寫的那樣的順序來查詢的。

所謂的第一個並不一定是最上面的那個。

當有id爲B,A,C的順序的mirror在mirrors節點中,maven會根據字母排序來指定第一個,所以不管怎麼排列,一定會找到A這個mirror來進行查找,當A無法連接,出現意外的情況下,纔會去B查詢。

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

    <servers>
        <server>
            <id>repo-iss</id>
            <username>deployment</username>
            <password>deployment123</password>
        </server>
    </servers>

    <mirrors>
        <!-- osc鏡像 -->
        <mirror>
            <!-- 鏡像所有遠程倉庫,但不包括指定的倉庫 -->
            <id>mirror-osc</id>
            <mirrorOf>external:*,!repo-osc-thirdparty,!repo-iss</mirrorOf>
            <url>http://maven.oschina.net/content/groups/public/</url>
        </mirror>
<!--
        <mirror>
            <id>mirror-iss</id>
            <mirrorOf>external:*</mirrorOf>
            <url>http://10.24.16.99:5555/nexus/content/groups/public/</url>
        </mirror>
-->
    </mirrors>

    <profiles>
        <profile>
            <id>profile-default</id>
            <repositories>
                <repository>
                    <id>central</id>
                    <url>http://central</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </repository>
                <repository>
                    <id>repo-osc-thirdparty</id>
                    <url>http://maven.oschina.net/content/repositories/thirdparty/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>central</id>
                    <url>http://central</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
        <profile>
            <id>profile-iss</id>
            <repositories>
                <repository>
                    <id>repo-iss</id>
                    <url>http://10.24.16.99:5555/nexus/content/groups/public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>repo-iss</id>
                    <url>http://10.24.16.99:5555/nexus/content/groups/public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>

    <activeProfiles>
        <activeProfile>profile-default</activeProfile>
        <!--<activeProfile>profile-iss</activeProfile>-->
    </activeProfiles>

<!--
    <proxies>
        <proxy>
            <active>true</active>
            <protocol>http</protocol>
            <host>10.10.204.160</host>
            <port>80</port>
        </proxy>
    </proxies>
-->
</settings>


Reference:

https://my.oschina.net/zhanghaiyang/blog/606130

關注公衆號,分享乾貨,討論技術


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章