Java Maven settings.xml中私有倉庫配置詳解
Maven setting中私有倉庫配置淺析
最近遇到過不少這樣那樣的問題,曾經(jīng)做過maven的分享,但是發(fā)現(xiàn)當(dāng)時部分內(nèi)容還是太想當(dāng)然了,下面經(jīng)過嘗試后簡單總結(jié)下:
首先幾個邏輯:
- pom>啟用的profile>maven原有配置
- mirror配置mirrorOf和id匹配優(yōu)先
簡單maven配置
一般大家的配置(略去無關(guān)私有倉庫配置)都是這樣的
<mirrors>
<mirror>
<id>nexus</id>
<name>mvn.xxx.com</name>
<mirrorOf>central</mirrorOf>
<url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>
</mirror>
</mirrors>
<profile>
<id>dev</id>
<repositories>
<repository>
<id>nexus</id>
<url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>alibaba</id>
<url>http://code.alibabatech.com/mvn/releases/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
mirrors
這個標(biāo)簽重要的屬性包括id、mirrorOf。id用來唯一區(qū)分。mirrorOf用來關(guān)聯(lián)repository。
url用來表示私服地址。
mirrorOf常見大家配置成*、central、repo啥的。這里剛才提到了是用來關(guān)聯(lián)respository的,等提到下面<respository>標(biāo)簽的時候在解釋。
profile
這個就簡單說下吧,就是算是個配置,可以配多個,具體哪個生效可以通過mvn命令指定,或者配置<activeProfiles>
repositories
這里面算是配置的重點(diǎn)
<repository>
<id>alibaba</id>
<url>http://code.alibabatech.com/mvn/releases/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
幾個重要的配置,一目了然吧,id標(biāo)識,url地址,是否從該倉庫下release,是否從該倉庫下快照版本。
這里就有人會懵逼了,這里怎么又配了個地址,跟mirrors里面的地址哪個生效呢?
好的,那咱們試試。先規(guī)定一下配置:
<mirrors>
<mirror>
<id>nexus</id>
<name>mvn.ws.netease.com</name>
<mirrorOf>central</mirrorOf>
<url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>
</mirror>
</mirrors>
<repositories>
<repository>
<id>nexus</id>
<url>http://mvn.ccc.com/nexus/content/groups/t_repo_group/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
把地址區(qū)分下,mirror里配成xxx,repository配成ccc
隨便找一個項(xiàng)目,設(shè)定一個不存在的依賴,mvn -U compile下:

可以發(fā)現(xiàn)去ccc找了。說明repository里的生效了。
那么mirror里的地址什么時候生效呢?其實(shí)剛才說了,mirror里的是靠mirrorOf中的內(nèi)容和repository中id關(guān)聯(lián)的。比如我們把剛才配置改為
<mirrors>
<mirror>
<id>nexus</id>
<name>mvn.ws.netease.com</name>
<mirrorOf>central</mirrorOf>
<url>http://mvn.xxx.com/nexus/content/groups/t_repo_group/</url>
</mirror>
</mirrors>
<repositories>
<repository>
<id>central</id>
<url>http://mvn.ccc.com/nexus/content/groups/t_repo_group/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
把repository中的id改成central

這樣就行了。此外mirrorOf中可以配置通配符,例如*,表示任何repository都和這個關(guān)聯(lián)。
其實(shí)簡單來說就是如果repository的id能和mirrorOf關(guān)聯(lián)上,那么url以mirror的為準(zhǔn),否則以repository中自己的url為準(zhǔn)。
其他還有一些點(diǎn),repositories中可以配置多個repository,配置多個話,一個找不到會找下一個,比如我們在剛才基礎(chǔ)上加上阿里的配置
<repositories>
<repository>
<id>nexus</id>
<url>http://mvn.ccc.com/nexus/content/groups/t_repo_group/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>alibaba</id>
<url>http://code.alibabatech.com/mvn/releases/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
在構(gòu)建一次:

當(dāng)配置多個時,會逐一進(jìn)行下載嘗試。
總結(jié)
咱們在回顧下起初的配置,可以看到啟用的profile是dev,dev中的repository的id是nexus,跟mirrorOf沒有匹配,那么生效的配置就是repository中自己的url配置,所以這里完全可以省略掉mirror的配置。當(dāng)然如果多個profile公用一個私服地址,也可以指定mirror地址,然后repository中的id指定成和mirrorOf相同,同時可以省略掉自己標(biāo)簽中url。
此外還有幾個點(diǎn)要說,pluginRepositories,配置信息基本和repository一致,不過這個地址是用來下maven的插件的,就是pom中這樣的配置
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
<webResources>
<resource>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<targetPath>WEB-INF</targetPath>
<includes>
<include>**</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
還有,pom也可以指定repository:

這樣配置會和settings.xml中生效的配置合并,并優(yōu)先從這個庫找,找不到繼續(xù)走settings.xml配置。
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
java webApp異步上傳圖片實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了java webApp異步上傳圖片實(shí)現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
Nacos-SpringBoot框架啟動不加載bootstrap.yml的解決
這篇文章主要介紹了Nacos-SpringBoot框架啟動不加載bootstrap.yml的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
Java進(jìn)階學(xué)習(xí):網(wǎng)絡(luò)服務(wù)器編程
Java進(jìn)階學(xué)習(xí):網(wǎng)絡(luò)服務(wù)器編程...2006-12-12
Javabean轉(zhuǎn)換成json字符并首字母大寫代碼實(shí)例
這篇文章主要介紹了javabean轉(zhuǎn)成json字符并首字母大寫代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02
基于logback 實(shí)現(xiàn)springboot超級詳細(xì)的日志配置
java web 下有好幾種日志框架,比如:logback,log4j,log4j2(slj4f 并不是一種日志框架,它相當(dāng)于定義了規(guī)范,實(shí)現(xiàn)了這個規(guī)范的日志框架就能夠用 slj4f 調(diào)用)。這篇文章主要介紹了基于logback springboot超級詳細(xì)的日志配置,需要的朋友可以參考下2019-06-06
SpringBoot集成Spring security JWT實(shí)現(xiàn)接口權(quán)限認(rèn)證
這篇文章主要介紹了SpringBoot集成Spring security JWT實(shí)現(xiàn)接口權(quán)限認(rèn)證,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
java 設(shè)計(jì)模式(DAO)的實(shí)例詳解
這篇文章主要介紹了java 設(shè)計(jì)模式(DAO)的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09

