Java Maven settings.xml中私有倉庫配置詳解
Maven setting中私有倉庫配置淺析
最近遇到過不少這樣那樣的問題,曾經做過maven的分享,但是發(fā)現當時部分內容還是太想當然了,下面經過嘗試后簡單總結下:
首先幾個邏輯:
- pom>啟用的profile>maven原有配置
- mirror配置mirrorOf和id匹配優(yōu)先
簡單maven配置
一般大家的配置(略去無關私有倉庫配置)都是這樣的
<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
這個標簽重要的屬性包括id、mirrorOf。id用來唯一區(qū)分。mirrorOf用來關聯repository。
url用來表示私服地址。
mirrorOf常見大家配置成*、central、repo啥的。這里剛才提到了是用來關聯respository的,等提到下面<respository>標簽的時候在解釋。
profile
這個就簡單說下吧,就是算是個配置,可以配多個,具體哪個生效可以通過mvn命令指定,或者配置<activeProfiles>
repositories
這里面算是配置的重點
<repository>
<id>alibaba</id>
<url>http://code.alibabatech.com/mvn/releases/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
幾個重要的配置,一目了然吧,id標識,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
隨便找一個項目,設定一個不存在的依賴,mvn -U compile下:

可以發(fā)現去ccc找了。說明repository里的生效了。
那么mirror里的地址什么時候生效呢?其實剛才說了,mirror里的是靠mirrorOf中的內容和repository中id關聯的。比如我們把剛才配置改為
<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都和這個關聯。
其實簡單來說就是如果repository的id能和mirrorOf關聯上,那么url以mirror的為準,否則以repository中自己的url為準。
其他還有一些點,repositories中可以配置多個repository,配置多個話,一個找不到會找下一個,比如我們在剛才基礎上加上阿里的配置
<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>
在構建一次:

當配置多個時,會逐一進行下載嘗試。
總結
咱們在回顧下起初的配置,可以看到啟用的profile是dev,dev中的repository的id是nexus,跟mirrorOf沒有匹配,那么生效的配置就是repository中自己的url配置,所以這里完全可以省略掉mirror的配置。當然如果多個profile公用一個私服地址,也可以指定mirror地址,然后repository中的id指定成和mirrorOf相同,同時可以省略掉自己標簽中url。
此外還有幾個點要說,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配置。
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內容!
相關文章
Nacos-SpringBoot框架啟動不加載bootstrap.yml的解決
這篇文章主要介紹了Nacos-SpringBoot框架啟動不加載bootstrap.yml的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
基于logback 實現springboot超級詳細的日志配置
java web 下有好幾種日志框架,比如:logback,log4j,log4j2(slj4f 并不是一種日志框架,它相當于定義了規(guī)范,實現了這個規(guī)范的日志框架就能夠用 slj4f 調用)。這篇文章主要介紹了基于logback springboot超級詳細的日志配置,需要的朋友可以參考下2019-06-06
SpringBoot集成Spring security JWT實現接口權限認證
這篇文章主要介紹了SpringBoot集成Spring security JWT實現接口權限認證,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04

