關于Maven混合配置私有倉庫和公共倉庫的問題
背景
近期在調(diào)研一個開源倉庫,于是將 代碼 從github下載后,當IDEA sync依賴時出現(xiàn)Cannot resolve org.fourthline.cling:cling-support:2.1.1
的問題,詳情如下:
Cannot resolve org.fourthline.cling:cling-support:2.1.1 Clean up the broken artifacts data (.lastUpdated files) and reload the project. Cannot resolve org.fourthline.cling:cling-core:2.1.1 Clean up the broken artifacts data (.lastUpdated files) and reload the project.
很明顯這個問題是Maven倉庫找不到需要的jar包。那么如何解決這個問題呢?
私有和公共倉庫混合配置
Maven倉庫
首先,需要知道Maven倉庫分為私有倉庫、公有倉庫、第三方倉庫。其中:
- 私有倉庫:一般是公司或個人自己搭建的倉庫。
- 公共倉庫
首選是Maven官方的倉庫,地址:repo1.maven.org/maven2/
官方倉庫在國內(nèi)訪問極慢,因此國內(nèi)一般會使用阿里云倉庫替代,地址:maven.aliyun.com/nexus/conte…
- 第三方倉庫:一些個人、團體、公司開放的倉庫,倉庫中的jar包并沒有發(fā)布到公共倉庫中。
熟悉倉庫的分類對于解決上文中遇到的問題有這個重要的作用。
解決步驟
一、驗證私有倉庫
遇到依賴缺失的問題后,首先需要確認私有倉庫
是否存在依賴,查看nexus
發(fā)現(xiàn)并沒有需要的依賴,可以斷定私有倉庫
沒有此依賴。
二、搜索共有倉庫
Maven官方的倉庫提供了Web搜索頁面,地址:repo1.maven.org/maven2/,嘗試搜索后發(fā)現(xiàn)也沒有需要的依賴。如下:
三、搜索第三方倉庫
經(jīng)過以上搜索后,可以斷定 代碼 需要的依賴,一定是由第三方倉庫提供的。如何找到是在哪個第三方倉庫呢?
此時,就需要mvnrepository
來提供幫助了,地址: mvnrepository.com/ 。mvnrepository
提供了公共倉庫和第三方倉庫中jar包的索引、查詢、下載等實用功能。
我們嘗試搜索需要的依賴, mvnrepository.com/artifact/or…,結(jié)果如下圖:
可以看到在mvnrepository
中找到了需要的依賴。那么問題來,如何知道第三方倉庫的地址呢?可以詳細看上圖中箭頭指向的區(qū)域,這里展示了第三方倉庫的maven url。
在實踐中,完全可以跳過搜索
公共倉庫
,因為mvnrepository
已經(jīng)包含了公共倉庫
的依賴。
四、maven配置
maven倉庫的配置是在setting.xml
配置的。如果要混合配置私有倉庫和公共倉庫,需要在setting.xml
增加新的mirror
和profile
,并激活新的activeProfile
。
mirror
setting.xml
有mirrors
節(jié)點,是用來配置鏡像URL的。mirrors
可以配置多個mirror
,每個mirror
有id
,name
,url
,mirrorOf
屬性,詳細解釋如下:
- id是唯一標識一個mirror
- name貌似沒多大用,相當于描述
- url是官方的庫地址
- mirrorOf代表了一個鏡像的替代位置,其中,
- *: 匹配所有,所有內(nèi)容都從鏡像拉?。?/li>
- external:*: 除了本地緩存的所有從鏡像倉庫拉??;
- repo,repo1: repo或者repo1,這里的repo指的倉庫ID;
- *,!repo1: 除了repo1的所有倉庫;
我們這次需要添加的mirror
如下:
<mirror> <id>nexus-4thline</id> <mirrorOf>4thline</mirrorOf> <name>4thline nexus</name> <url>http://4thline.org/m2/</url> </mirror>
這里又產(chǎn)生了一個問題,配置了多個mirror
,Maven如何排序?答案是,Maven并不是按照setting.xml
中配置的順序執(zhí)行,而是根據(jù)字母排序來指定第一個,然后會遍歷所有的倉庫,直至找到需要的依賴。
profile
作用:根據(jù)環(huán)境參數(shù)來調(diào)整構(gòu)建配置的列表,settings.xml
中的profile
元素是pom.xml
中profile
元素的裁剪版本。
id
:profile的唯一標識activation
:自動觸發(fā)profile的條件邏輯repositories
:遠程倉庫列表pluginRepositories
:插件倉庫列表properties
:擴展屬性列表
這里的profile元素只包含這五個子元素是因為這里只關心構(gòu)建系統(tǒng)這個整體(這正是settings.xml文件的角色定位),而非單獨的項目對象模型設置。如果一個settings.xml
中的profile
被激活,它的值會覆蓋任何其它定義在pom.xml
中帶有相同id的profile
。
我們這次需要添加的profile
如下:
<profile> <id>4thline</id> <repositories> <repository> <id>4thline</id> <url>http://4thline.org/m2/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> </profile>
activeProfile
作用:手動激活profiles的列表,按照profile
被應用的順序定義activeProfile
。
該元素包含了一組
activeProfile
元素,每個activeProfile
都含有一個profile id。任何在activeProfile
中定義的profile id,不論環(huán)境設置如何,其對應的profile
都會被激活。如果沒有匹配的profile
,則什么都不會發(fā)生。
我們這次需要添加的activeProfile
如下:
<activeProfiles> <activeProfile>corp</activeProfile> <activeProfile>aliyun</activeProfile> <activeProfile>4thline</activeProfile> </activeProfiles>
配置結(jié)果
經(jīng)過上述配置后,首先我們可以在IDEA的Maven
側(cè)邊欄中,可以看到多了4thline
的profile
。(可以看到IDEA的profiles
是按照首字母排序的,和上文中mirror
的定義是一致的。)
重新執(zhí)行Reload All Maven Projects
,所有的依賴都正常import。
setting.xml完整配置
完整版的setting.xml
如下:
<?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:\Users\xxx\.m2\repository\</localRepository> <pluginGroups> </pluginGroups> <mirrors> <mirror> <id>nexus</id> <mirrorOf>corp</mirrorOf> <name>corp nexus</name> <url>http://maven.corp.com/nexus/content/groups/public</url> </mirror> <mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> <name>aliyun nexus</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror> <mirror> <id>nexus-4thline</id> <mirrorOf>4thline</mirrorOf> <name>4thline nexus</name> <url>http://4thline.org/m2/</url> </mirror> </mirrors> <servers> <server> <id>releases</id> <username>xxx</username> <password>yyy</password> </server> <server> <id>snapshots</id> <username>xxx</username> <password>yyy</password> </server> </servers> <profiles xmlns=""> <profile> <id>corp</id> <repositories> <repository> <id>public</id> <name>all repoes</name> <url>http://maven.corp.com/nexus/content/groups/public</url> <releases> <enabled>true</enabled> <checksumPolicy>warn</checksumPolicy> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> <checksumPolicy>fail</checksumPolicy> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>corp</id> <url>http://maven.corp.com/nexus/content/groups/public</url> <releases> <enabled>true</enabled> <checksumPolicy>warn</checksumPolicy> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> <checksumPolicy>fail</checksumPolicy> </snapshots> </pluginRepository> </pluginRepositories> </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>4thline</id> <repositories> <repository> <id>4thline</id> <url>http://4thline.org/m2/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> </profile> </profiles> <activeProfiles> <activeProfile>corp</activeProfile> <activeProfile>aliyun</activeProfile> <activeProfile>4thline</activeProfile> </activeProfiles> </settings>
結(jié)論
Maven
是每一個Java程序員都再熟悉不過的工具,但是我們真的了解它嗎?
任何一個優(yōu)秀的框架、組件、工具,除了特殊的另外的特殊優(yōu)化外,很多時候設計思路是大體相同的,我們在實際工作中不僅僅要擴展自己的涉獵領域,更需要在某些領域深入進入,對個人的提升是更為有益處的。
到此這篇關于Maven混合配置私有倉庫和公共倉庫的文章就介紹到這了,更多相關Maven配置私有倉庫和公共倉庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring?Boot最經(jīng)典的20道面試題你都會了嗎
Spring Boot是現(xiàn)代化的Java應用程序開發(fā)框架,具有高度的靈活性和可擴展性,下面這篇文章主要給大家介紹了關于Spring?Boot最經(jīng)典的20道面試題,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-06-06Spring Security 表單登錄功能的實現(xiàn)方法
這篇文章主要介紹了Spring Security 表單登錄,本文將構(gòu)建在之前簡單的 Spring MVC示例 之上,因為這是設置Web應用程序和登錄機制的必不可少的。需要的朋友可以參考下2019-06-06如何解決java中遇到的for input string: "" 報錯問題
在本篇文章里小編給大家整理的是一篇關于如何解決java中遇到的(for input string: "")報錯內(nèi)容,需要的朋友們可以學習下。2020-02-02

Java中l(wèi)ist.foreach()和list.stream().foreach()用法詳解