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