Maven配置單倉庫與多倉庫的實現(xiàn)(Nexus)
單倉庫
當只配置一個倉庫時,操作比較簡單,直接在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配置即可。要做到單一倉庫,設置mirrorOf到*。
mirrorOf中配置的星號,表示匹配所有的artifacts,也就是everything使用這里的代理地址。上面的mirrorOf配置了具體的名字,指的是repository的名字。
鏡像配置說明:
1、id: 鏡像的唯一標識;
2、name: 名稱描述;
3、url: 地址;
4、mirrorOf: 指定鏡像規(guī)則,什么情況下從鏡像倉庫拉取。其中, *: 匹配所有,所有內容都從鏡像拉取;
external:*: 除了本地緩存的所有從鏡像倉庫拉取;
repo,repo1: repo或者repo1,這里的repo指的倉庫ID;
*,!repo1: 除了repo1的所有倉庫;
多倉庫
先看看我的settings配置文件
<?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">
<localRepository>D:\idea\Maven\apache-maven-3.8.4\maven-repo</localRepository>
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<!-- 私服信息 -->
<servers>
<server>
<id>kd-nexus</id>
<username>賬號</username>
<password>密碼</password>
</server>
<server>
<id>nexus</id>
<username>賬號</username>
<password>密碼</password>
</server>
</servers>
<!-- 倉庫信息 -->
<mirrors>
<mirror>
<!-- 與上面的id對應 -->
<id>kd-nexus</id>
<mirrorOf>*</mirrorOf>
<name>kd-nexus maven</name>
<url>http://ip:8081/repository/maven-public/</url>
</mirror>
<mirror>
<!-- 與上面的id對應 -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<name>nexus maven</name>
<url>http://ip:8081/repository/maven-public/</url>
</mirror>
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
</mirrors>
<!-- 項目配置文件信息 -->
<profiles>
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
<!-- 啟動那個配置 -->
<activeProfiles>
<activeProfile>jdk-1.8</activeProfile>
</activeProfiles>
</settings>
剛開始我以為這樣就可以了,如果在kd-nexus找不到jar包,會自動去nexus里面找jar包,可現(xiàn)實是maven只會在kd-nexus中找,找不到就報錯,根本不會在之后的倉庫中取尋找 因為:
雖然mirrors可以配置多個子節(jié)點,但是它只會使用其中的一個節(jié)點,即**默認情況下配置多個mirror的情況下,只有第一個生效,**只有當前一個mirror
無法連接的時候,才會去找后一個;而我們想要的效果是:當a.jar在第一個mirror中不存在的時候,maven會去第二個mirror中查詢下載,但是maven不會這樣做!
多倉庫配置
那么針對多倉庫的配置是否再多配置幾個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>

打包時,勾選所使用的profile即可。如果使用Maven命令打包執(zhí)行命令格式如下:
mvn -Paliyun ...
1.如果aliyun倉庫的id設置為central,則會覆蓋maven里默認的遠程倉庫。
2.aliyun的倉庫也可以不用配置,直接在mirrors標簽內配置一個鏡像倉庫,mirrors鏡像倉庫mirrorOf的值設置為central,則也可以實現(xiàn)覆蓋默認的倉庫。
好了你既然看到這里了,那么上訴可能基本沒效果,問就是我導包也沒成功,現(xiàn)提供兩個解決方案
前提
idea每次修改完settings之后,需要重新點擊一下,更新一下文件配置

一、挨個導包
首先kd-nexus先導一下包
<mirrors>
<mirror>
<id>kd-nexus</id>
<mirrorOf>*</mirrorOf>
<name>kd-nexus maven</name>
<url>http://ip:8081/repository/maven-public/</url>
</mirror>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<name>nexus maven</name>
<url>http://ip:8081/repository/maven-public/</url>
</mirror>
</mirrors>
之后修改maven配置,導一下nexus的包
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<name>nexus maven</name>
<url>http://ip:8081/repository/maven-public/</url>
</mirror>
<mirror>
<id>kd-nexus</id>
<mirrorOf>*</mirrorOf>
<name>kd-nexus maven</name>
<url>http://ip:8081/repository/maven-public/</url>
</mirror>
</mirrors>
二、手動導包(解決一切導包問題,就是過程很麻煩)

本地maven倉庫:如果連這個都不清楚,看nexus完全沒意義
就是localRepository標簽中的文件地址 <localRepository>D:\idea\Maven\apache-maven-3.8.4\maven-repo</localRepository>
不知道Nexus怎么搭建的家人們,可以看一下這篇文章:使用Nexus搭建Maven私服教程(附:nexus上傳、下載教程)
到此這篇關于Maven配置單倉庫與多倉庫的實現(xiàn)(Nexus)的文章就介紹到這了,更多相關Maven
相關文章
SpringTask-Timer實現(xiàn)定時任務的詳細代碼
在項目中開發(fā)定時任務應該一種比較常見的需求,今天通過示例代碼給大家講解SpringTask-Timer實現(xiàn)定時任務的相關知識,感興趣的朋友一起看看吧2024-06-06
springboot動態(tài)數(shù)據(jù)源+分布式事務的實現(xiàn)
本文主要介紹了springboot動態(tài)數(shù)據(jù)源+分布式事務的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-09-09
logback?EvaluatorFilter日志過濾器源碼解讀
這篇文章主要為大家介紹了logback?EvaluatorFilter日志過濾器源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11

