Maven配置多倉庫無效的解決
在項(xiàng)目中使用Maven管理jar包依賴,往往會(huì)出現(xiàn)以下狀況:
1、國內(nèi)訪問maven默認(rèn)遠(yuǎn)程中央鏡像特別慢;
2、使用阿里的鏡像替代遠(yuǎn)程中央鏡像;
3、阿里云鏡像中缺少部分jar包;
4、同時(shí)使用私有倉庫和公有倉庫;
針對(duì)以上情況,我們就需要讓Maven支持多倉庫配置。
單獨(dú)倉庫配置
當(dāng)只配置一個(gè)倉庫時(shí),操作比較簡單,直接在Maven的settings.xml文件中進(jìn)行全局配置即可,以阿里云的鏡像為例:
<mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors>
只用新增一個(gè)mirror配置即可。要做到單一倉庫,設(shè)置mirrorOf到*。
mirrorOf中配置的星號(hào),表示匹配所有的artifacts,也就是everything使用這里的代理地址。上面的mirrorOf配置了具體的名字,指的是repository的名字。
鏡像配置說明:
1、id: 鏡像的唯一標(biāo)識(shí);
2、name: 名稱描述;
3、url: 地址;
4、mirrorOf: 指定鏡像規(guī)則,什么情況下從鏡像倉庫拉取。其中,
*: 匹配所有,所有內(nèi)容都從鏡像拉??;
external:*: 除了本地緩存的所有從鏡像倉庫拉?。?/p>
repo,repo1: repo或者repo1,這里的repo指的倉庫ID;
*,!repo1: 除了repo1的所有倉庫;
多倉庫配置
那么針對(duì)多倉庫的配置是否再多配置幾個(gè)mirror就可以了?如此配置并不會(huì)生效。
正確的操作是在profiles節(jié)點(diǎn)下配置多個(gè)profile,而且配置之后要激活。
<profiles> <profile> <id>boundlessgeo</id> <repositories> <repository> <id>boundlessgeo</id> <url>https://repo.boundlessgeo.com/main/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> </profile> <profile> <id>aliyun</id> <repositories> <repository> <id>aliyun</id> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> </profile> <profile> <id>maven-central</id> <repositories> <repository> <id>maven-central</id> <url>http://central.maven.org/maven2/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> </profile> <profiles>
通過配置activeProfiles子節(jié)點(diǎn)激活:
<activeProfiles> <activeProfile>boundlessgeo</activeProfile> <activeProfile>aliyun</activeProfile> <activeProfile>maven-central</activeProfile> </activeProfiles>
此時(shí)如果是在Idea中使用了本地的Maven配置,那么在項(xiàng)目的Maven管理中會(huì)看到類似如下圖中的profile選項(xiàng)。
打包時(shí),勾選所使用的profile即可。如果使用Maven命令打包執(zhí)行命令格式如下:
mvn -Paliyun ...
1.如果aliyun倉庫的id設(shè)置為central,則會(huì)覆蓋maven里默認(rèn)的遠(yuǎn)程倉庫。
2.aliyun的倉庫也可以不用配置,直接在mirrors標(biāo)簽內(nèi)配置一個(gè)鏡像倉庫,mirrors鏡像倉庫mirrorOf的值設(shè)置為central,則也可以實(shí)現(xiàn)覆蓋默認(rèn)的倉庫。
項(xiàng)目中配置鏡像
在項(xiàng)目中添加多個(gè)倉庫,是通過修改項(xiàng)目中的pom文件實(shí)現(xiàn)的。
思路:在項(xiàng)目中pom文件的repositories節(jié)點(diǎn)(如果沒有手動(dòng)添加)下添加多個(gè)repository節(jié)點(diǎn),每個(gè)repository節(jié)點(diǎn)是一個(gè)倉庫。
配置效果如下:
<!-- 特殊maven倉庫 --> <repositories> <repository> <id>central-repo1</id> <name>Maven Repository Switchboard</name> <url>http://repo1.maven.org/maven2/</url> <layout>default</layout> <releases> <enabled>true</enabled> </releases> </repository> </repositories>
這里的id就是mirrorOf要使用的ID。
在實(shí)踐的過程中發(fā)現(xiàn)單單配置該倉庫配置并不會(huì)生效,需要同時(shí)在setting.xml中定義一個(gè)mirrorOf為central-repo1的倉庫配置,與該配置的id對(duì)照。
setting.xml中的對(duì)照配置如下:
<mirror> <id>central</id> <name>Maven Repository Switchboard</name> <url>https://repo1.maven.org/maven2/</url> <mirrorOf>central-repo1</mirrorOf> </mirror>
值得收藏的國內(nèi)鏡像
<mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/mvn/view</url> <mirrorOf>central</mirrorOf> </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>ibiblio</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>https://maven.aliyun.com/mvn/view</url> </mirror> <mirror> <id>central</id> <name>Maven Repository Switchboard</name> <url>http://repo1.maven.org/maven2/</url> <mirrorOf>central</mirrorOf> </mirror> <mirror> <id>repo2</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://repo2.maven.org/maven2/</url> </mirror> </mirrors>
到此這篇關(guān)于Maven配置多倉庫無效的解決的文章就介紹到這了,更多相關(guān)Maven配置多倉庫 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(16)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07IDEA MyBatis Plugins自動(dòng)生成實(shí)體類和mapper.xml
這篇文章主要介紹了IDEA MyBatis Plugins自動(dòng)生成實(shí)體類和mapper.xml,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07UniApp?+?SpringBoot?實(shí)現(xiàn)微信支付和退款功能
這篇文章主要介紹了UniApp?+?SpringBoot?實(shí)現(xiàn)微信支付和退款功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06springboot項(xiàng)目數(shù)據(jù)庫密碼如何加密
在我們?nèi)粘i_發(fā)中,我們可能很隨意把數(shù)據(jù)庫密碼直接明文暴露在配置文件中,今天就來聊聊在springboot項(xiàng)目中如何對(duì)數(shù)據(jù)庫密碼進(jìn)行加密,感興趣的可以了解一下2021-07-07JAVA后端學(xué)習(xí)精華之網(wǎng)絡(luò)通信項(xiàng)目進(jìn)階
不同項(xiàng)目之間的通信方式分為,http、socket、webservice;其中socket通信的效率最高,youtube就采用的是原始的socket通信,他們信奉的原則是簡單有效2021-09-09詳解Maven項(xiàng)目缺少M(fèi)aven Dependencies解決方法總結(jié)
這篇文章主要介紹了詳解Maven項(xiàng)目缺少M(fèi)aven Dependencies解決方法總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11