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