Maven配置單倉(cāng)庫(kù)與多倉(cāng)庫(kù)的實(shí)現(xiàn)(Nexus)
單倉(cāng)庫(kù)
當(dāng)只配置一個(gè)倉(cāng)庫(kù)時(shí),操作比較簡(jiǎn)單,直接在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配置即可。要做到單一倉(cāng)庫(kù),設(shè)置mirrorOf到*。
mirrorOf中配置的星號(hào),表示匹配所有的artifacts,也就是everything使用這里的代理地址。上面的mirrorOf配置了具體的名字,指的是repository的名字。
鏡像配置說(shuō)明:
1、id: 鏡像的唯一標(biāo)識(shí);
2、name: 名稱描述;
3、url: 地址;
4、mirrorOf: 指定鏡像規(guī)則,什么情況下從鏡像倉(cāng)庫(kù)拉取。其中, *: 匹配所有,所有內(nèi)容都從鏡像拉?。?/p>
external:*: 除了本地緩存的所有從鏡像倉(cāng)庫(kù)拉??;
repo,repo1: repo或者repo1,這里的repo指的倉(cāng)庫(kù)ID;
*,!repo1: 除了repo1的所有倉(cāng)庫(kù);
多倉(cāng)庫(kù)
先看看我的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>賬號(hào)</username> <password>密碼</password> </server> <server> <id>nexus</id> <username>賬號(hào)</username> <password>密碼</password> </server> </servers> <!-- 倉(cāng)庫(kù)信息 --> <mirrors> <mirror> <!-- 與上面的id對(duì)應(yīng) --> <id>kd-nexus</id> <mirrorOf>*</mirrorOf> <name>kd-nexus maven</name> <url>http://ip:8081/repository/maven-public/</url> </mirror> <mirror> <!-- 與上面的id對(duì)應(yīng) --> <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> <!-- 項(xiàng)目配置文件信息 --> <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> <!-- 啟動(dòng)那個(gè)配置 --> <activeProfiles> <activeProfile>jdk-1.8</activeProfile> </activeProfiles> </settings>
剛開(kāi)始我以為這樣就可以了,如果在kd-nexus找不到j(luò)ar包,會(huì)自動(dòng)去nexus里面找jar包,可現(xiàn)實(shí)是maven只會(huì)在kd-nexus中找,找不到就報(bào)錯(cuò),根本不會(huì)在之后的倉(cāng)庫(kù)中取尋找 因?yàn)椋?/p>
雖然mirrors可以配置多個(gè)子節(jié)點(diǎn),但是它只會(huì)使用其中的一個(gè)節(jié)點(diǎn),即**默認(rèn)情況下配置多個(gè)mirror的情況下,只有第一個(gè)生效,**只有當(dāng)前一個(gè)mirror
無(wú)法連接的時(shí)候,才會(huì)去找后一個(gè);而我們想要的效果是:當(dāng)a.jar在第一個(gè)mirror中不存在的時(shí)候,maven會(huì)去第二個(gè)mirror中查詢下載,但是maven不會(huì)這樣做!
多倉(cāng)庫(kù)配置
那么針對(duì)多倉(cāng)庫(kù)的配置是否再多配置幾個(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>
通過(guò)配置activeProfiles子節(jié)點(diǎn)激活:
<activeProfiles> <activeProfile>boundlessgeo</activeProfile> <activeProfile>aliyun</activeProfile> <activeProfile>maven-central</activeProfile> </activeProfiles>
打包時(shí),勾選所使用的profile即可。如果使用Maven命令打包執(zhí)行命令格式如下:
mvn -Paliyun ...
1.如果aliyun倉(cāng)庫(kù)的id設(shè)置為central,則會(huì)覆蓋maven里默認(rèn)的遠(yuǎn)程倉(cāng)庫(kù)。
2.aliyun的倉(cāng)庫(kù)也可以不用配置,直接在mirrors標(biāo)簽內(nèi)配置一個(gè)鏡像倉(cāng)庫(kù),mirrors鏡像倉(cāng)庫(kù)mirrorOf的值設(shè)置為central,則也可以實(shí)現(xiàn)覆蓋默認(rèn)的倉(cāng)庫(kù)。
好了你既然看到這里了,那么上訴可能基本沒(méi)效果,問(wèn)就是我導(dǎo)包也沒(méi)成功,現(xiàn)提供兩個(gè)解決方案
前提
idea每次修改完settings之后,需要重新點(diǎn)擊一下,更新一下文件配置
一、挨個(gè)導(dǎo)包
首先kd-nexus先導(dǎo)一下包
<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配置,導(dǎo)一下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>
二、手動(dòng)導(dǎo)包(解決一切導(dǎo)包問(wèn)題,就是過(guò)程很麻煩)
本地maven倉(cāng)庫(kù):如果連這個(gè)都不清楚,看nexus完全沒(méi)意義
就是localRepository標(biāo)簽中的文件地址 <localRepository>D:\idea\Maven\apache-maven-3.8.4\maven-repo</localRepository>
不知道Nexus怎么搭建的家人們,可以看一下這篇文章:使用Nexus搭建Maven私服教程(附:nexus上傳、下載教程)
到此這篇關(guān)于Maven配置單倉(cāng)庫(kù)與多倉(cāng)庫(kù)的實(shí)現(xiàn)(Nexus)的文章就介紹到這了,更多相關(guān)Maven
相關(guān)文章
java 數(shù)值類型分秒時(shí)間格式化的實(shí)例代碼
這篇文章主要介紹了java 數(shù)值類型分秒時(shí)間格式化的實(shí)例代碼的相關(guān)資料,將秒或分鐘的值轉(zhuǎn)換為xx天xx小時(shí)xx分鐘xx秒 如果 “xx” 為0 自動(dòng)缺省,需要的朋友可以參考下2017-07-07java中BeanNotOfRequiredTypeException的問(wèn)題解決(@Autowired和@Resourc
本文主要介紹了java中BeanNotOfRequiredTypeException的問(wèn)題解決(@Autowired和@Resource注解的不同),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07SpringBoot實(shí)現(xiàn)文件的上傳、下載和預(yù)覽功能
在Spring Boot項(xiàng)目中實(shí)現(xiàn)文件的上傳、下載和預(yù)覽功能,可以通過(guò)使用Spring MVC的MultipartFile接口來(lái)處理文件上傳,并使用HttpServletResponse或Resource來(lái)實(shí)現(xiàn)文件下載和預(yù)覽,下面是如何實(shí)現(xiàn)這些功能的完整示例,需要的朋友可以參考下2024-08-08Java中數(shù)組如何轉(zhuǎn)為字符串的幾種方法
數(shù)組是java中一個(gè)重要的類型,小伙伴們知道如何將數(shù)組轉(zhuǎn)為字符串嗎,這篇文章主要給大家介紹了關(guān)于Java中數(shù)組如何轉(zhuǎn)為字符串的幾種方法,需要的朋友可以參考下2024-03-03Java?Servlet實(shí)現(xiàn)表白墻的代碼實(shí)例
最近用Servlet做了個(gè)小項(xiàng)目,分享給大家,下面這篇文章主要給大家介紹了關(guān)于Java?Servlet實(shí)現(xiàn)表白墻的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02spring boot創(chuàng)建和數(shù)據(jù)庫(kù)關(guān)聯(lián)模塊詳解
這篇文章主要給大家介紹了關(guān)于spring boot創(chuàng)建和數(shù)據(jù)庫(kù)關(guān)聯(lián)模塊的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10