maven - mirrorOf 的坑、多鏡像切換(避免一切無厘頭報錯)


來源於  https://lawsssscat.blog.csdn.net/article/details/104863855

 

國內,用中央倉庫(https://repo1.maven.org/maven2/)獲取包太慢。
因此,我們都用鏡像。

# 鏡像配置

<mirror> <id>tz-mirror</id> <mirrorOf>external:*,!mmkj</mirrorOf> <name>tz test nexus repository</name> <url>http://xxxxx:30003/repository/maven-proxy</url> </mirror> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • id 唯一標識
  • mirrorOf 指定鏡像的規則。
    就是什么情況會從鏡像倉庫拉取,而不是從原本的倉庫拉取
    可選項參考鏈接:
    * 匹配所有
    external:* 除了本地緩存之后的所有倉庫
    repo,repo1 repo 或者 repo1。 這里repo指的是倉庫的id,下文會提到
    *,!repo1 除了repo1的所有倉庫
  • name 名稱描述
  • url 地址

# maven配置 mirrorOf 坑

記錄maven配置 mirrorOf 坑

我們用maven鏡像,通常就是無腦從網上粘貼這樣一段代碼 ↓

<mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

注意:
這里,mirrorOf 配置成 * 就代表我們通過訪問阿里雲的maven倉庫從而訪問網路上所有的遠程倉庫

也就是說,我們只會從 阿里雲 現有倉庫下包,如果阿里雲沒有,我們必須手動更改配置!

因此,我們需要改成 central

  • 如果配置成central,就代表我們訪問阿里雲的central倉庫,就是阿里的中央庫(http://central.maven.org/maven2)
  • 如果配置成repo1,就代表通過阿里雲訪問http://repo1.maven.org/maven2,
    阿里雲和這些遠程倉庫之間必須可以鏡像,存在一定的鏡像關系。

# 配置多個mirror 和 切換 mirror

配置多個 mirror 和簡單。
但是,每次切換 mirror 都要改配置文件,很麻煩,希望能夠不改動配置文件的情況下,動態切換mirror配置。

我們知道 settings.xml 中可以使用變量,可以嘗試使用變量解決。

<mirrors> <mirror> <id>aliyun</id> <url>https://maven.aliyun.com/repository/public</url> <mirrorOf>${aliyun}</mirrorOf> </mirror> <mirror> <id>netease</id> <url>http://mirrors.163.com/maven/repository/maven-public/</url> <mirrorOf>${netease}</mirrorOf> </mirror> <mirror> <id>default</id> <url>http://192.168.0.100/nexus/repository/maven-public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

我們知道,默認情況下配置多個mirror的情況下,只有第一個生效。
那么我們可以將 id 改為 default 作為默認值,前面配置的使用環境變量動態切換。
(如果沒有 default , 會用第一個 mirror)

默認情況下,執行: mvn help:effective-settings 可以看到使用的是私服。

  • 如果希望使用阿里雲鏡像,如下執行:
    mvn help-effective-settings -Daliyun=central

  • 同樣的道理,使用網易鏡像,則執行:
    mvn help:effective-settings -Dnetease=central


最后,附上幾個倉庫 mirror

   <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> <mirror> <id>ui</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://uk.maven.org/maven2/</url> </mirror> <mirror> <id>ibiblio</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url> </mirror> <mirror> <id>jboss-public-repository-group</id> <mirrorOf>central</mirrorOf> <name>JBoss Public Repository Group</name> <url>http://repository.jboss.org/nexus/content/groups/public</url> </mirror> <mirror> <id>OSCN</id> <name>OSChina Central</name> <url>http://maven.oschina.net/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

maven的中央(central)倉庫的鏡像地址

(最快) http://maven.ibiblio.org/maven2/
http://central.maven.org/maven2/
http://repo1.maven.apache.org/maven2/
http://repo1.maven.org/maven2/
http://mirrors.ibiblio.org/pub/mirrors/maven2/
https://repository.jboss.org/nexus/content/groups/public/
http://repo2.maven.org/maven2/


最快的 maven repository–阿里鏡像倉庫
國內最好的maven repository
第一步:修改maven根目錄下的conf文件夾中的setting.xml文件,內容如下:

    <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

第二步: pom.xml文件里添加

<repositories> <repository> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM