Maven settings.xml中私有倉庫配置淺析

Maven setting中私有倉庫配置淺析

最近遇到過不少這樣那樣的問題,曾經(jīng)做過maven的分享,但是發(fā)現(xiàn)當(dāng)時部分內(nèi)容還是太想當(dāng)然了,下面經(jīng)過嘗試后簡單總結(jié)下:

首先幾個邏輯:

  • pom>啟用的profile>maven原有配置

  • mirror配置mirrorOf和id匹配優(yōu)先

    簡單maven配置

    一般大家的配置(略去無關(guān)私有倉庫配置)都是這樣的

    <mirrors>
      <mirror>
          <id>nexus</id>
          <name>mvn.xxx.com</name>
          <mirrorOf>central</mirrorOf>
          <url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>         
      </mirror>
    </mirrors>
      <profile>
          <id>dev</id>
          <repositories>       
          <repository>
            <id>nexus</id>
            <url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>
            <releases><enabled>true</enabled></releases>
                      <snapshots><enabled>true</enabled></snapshots>
          </repository>
                      
              <repository>
                  <id>alibaba</id>
                  <url>http://code.alibabatech.com/mvn/releases/</url>
                  <releases>
                      <enabled>true</enabled>
                  </releases>
                  <snapshots>
                      <enabled>false</enabled>
                  </snapshots>
              </repository>
             
          </repositories>
          
          <pluginRepositories>
              <pluginRepository>
            <id>nexus</id>
            <url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>
            <releases><enabled>true</enabled></releases>
                      <snapshots><enabled>true</enabled></snapshots>
          </pluginRepository>
          </pluginRepositories>
      </profile>  
    <activeProfiles>
      <activeProfile>dev</activeProfile>
    </activeProfiles>
    

其實上面這個配置不能說錯誤,只能說有部分是沒有生效的。幾個重要的點逐一說明

mirrors

這個標(biāo)簽重要的屬性包括idmirrorOf。id用來唯一區(qū)分。mirrorOf用來關(guān)聯(lián)repository。
url用來表示私服地址。
mirrorOf常見大家配置成*、central、repo啥的。這里剛才提到了是用來關(guān)聯(lián)respository的,等提到下面<respository>標(biāo)簽的時候在解釋。
推薦大家閱讀下http://blog.csdn.net/isea533/article/details/21560089

profile

這個就簡單說下吧,就是算是個配置,可以配多個,具體哪個生效可以通過mvn命令指定,或者配置<activeProfiles>

repositories

這里面算是配置的重點

<repository>
      <id>alibaba</id>
      <url>http://code.alibabatech.com/mvn/releases/</url>
      <releases>
              <enabled>true</enabled>
      </releases>
      <snapshots>
               <enabled>false</enabled>
      </snapshots>
</repository>

幾個重要的配置,一目了然吧,id標(biāo)識,url地址,是否從該倉庫下release,是否從該倉庫下快照版本。
這里就有人會懵逼了,這里怎么又配了個地址,跟mirrors里面的地址哪個生效呢?

好的,那咱們試試。先規(guī)定一下配置:

<mirrors>
    <mirror>
        <id>nexus</id>
        <name>mvn.ws.netease.com</name>
        <mirrorOf>central</mirrorOf>
        <url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>       
    </mirror>
  </mirrors>
  <repositories>       
        <repository>
          <id>nexus</id>
          <url>http://mvn.ccc.com/nexus/content/groups/t_repo_group/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
</repositories> 

把地址區(qū)分下,mirror里配成xxx,repository配成ccc
隨便找一個項目,設(shè)定一個不存在的依賴,mvn -U compile下:


image.png

可以發(fā)現(xiàn)去ccc找了。說明repository里的生效了。
那么mirror里的地址什么時候生效呢?其實剛才說了,mirror里的是靠mirrorOf中的內(nèi)容和repository中id關(guān)聯(lián)的。比如我們把剛才配置改為

<mirrors>
    <mirror>
        <id>nexus</id>
        <name>mvn.ws.netease.com</name>
            <mirrorOf>central</mirrorOf>
        <url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>       
    </mirror>
  </mirrors>
  <repositories>       
        <repository>
          <id>central</id>
          <url>http://mvn.ccc.com/nexus/content/groups/t_repo_group/</url>
          <releases><enabled>true</enabled></releases>
                    <snapshots><enabled>true</enabled></snapshots>
        </repository>
</repositories> 

把repository中的id改成central

image.png

這樣就行了。此外mirrorOf中可以配置通配符,例如*,表示任何repository都和這個關(guān)聯(lián)。

其實簡單來說就是如果repository的id能和mirrorOf關(guān)聯(lián)上,那么url以mirror的為準(zhǔn),否則以repository中自己的url為準(zhǔn)

其他還有一些點,repositories中可以配置多個repository,配置多個話,一個找不到會找下一個,比如我們在剛才基礎(chǔ)上加上阿里的配置

<repositories>       
        <repository>
          <id>nexus</id>
          <url>http://mvn.ccc.com/nexus/content/groups/t_repo_group/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
                <id>alibaba</id>
                <url>http://code.alibabatech.com/mvn/releases/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository> 
</repositories>

在構(gòu)建一次:

image.png

當(dāng)配置多個時,會逐一進(jìn)行下載嘗試。

總結(jié)

咱們在回顧下起初的配置,可以看到啟用的profile是dev,dev中的repository的id是nexus,跟mirrorOf沒有匹配,那么生效的配置就是repository中自己的url配置,所以這里完全可以省略掉mirror的配置。當(dāng)然如果多個profile公用一個私服地址,也可以指定mirror地址,然后repository中的id指定成和mirrorOf相同,同時可以省略掉自己標(biāo)簽中url。
?
此外還有幾個點要說,pluginRepositories,配置信息基本和repository一致,不過這個地址是用來下maven的插件的,就是pom中這樣的配置

        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
                    <webResources>
                        <resource>
                            <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                            <filtering>true</filtering>
                            <targetPath>WEB-INF</targetPath>
                            <includes>
                                <include>**</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
        </plugins>

還有,pom也可以指定repository:


image.png

這樣配置會和settings.xml中生效的配置合并,并優(yōu)先從這個庫找,找不到繼續(xù)走settings.xml配置。

?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,923評論 18 139
  • |-1-更新內(nèi)容[6.從倉庫解析依賴的機制(重要)] 1Maven倉庫作用 倉庫用來存儲所有項目使用到構(gòu)件,在ma...
    zlcook閱讀 6,141評論 0 25
  • 搭建 nexus 私服(centos6.7) 備注:Centos 6.7 、 nexus-2.12.1-01-bu...
    逐暗者閱讀 2,526評論 3 9
  • 1.遠(yuǎn)程倉庫的配置 在平時的開發(fā)中,我們往往不會使用默認(rèn)的中央倉庫,默認(rèn)的中央倉庫訪問的速度比較慢,訪問的人或許很...
    followtry閱讀 11,304評論 3 4
  • 宋莊前西淇水芳, 莊水之上雁成行。 藍(lán)天麗日朔風(fēng)冽, 途經(jīng)馬耳北過常。 征塵仆仆羽翼苦, 一路雁歌向衡陽。 迤邐溹...
    王守棟閱讀 358評論 0 6