maven 倉庫地址有如下配置:
pom.xml
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepositories>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
settings.xml
<mirror>
<id>CN</id>
<name>OSChina Central</name>
<url>http://maven.oschina.net/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<profile>
<id>dev</id>
<repositories>
<repository>
<id>nexus-public</id>
<name>Public Repositories</name>
<url>http://192.168.2.126:8081/nexus/content/groups/public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>public</id>
<name>Public Repositories</name>
<url>http://192.168.2.126:8081/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
</profile>
首先我們有幾個問題:
- 這么多配倉庫的地方,配置的是什么倉庫?
- 多個倉庫使用哪一個,優(yōu)先級是什么?
第一個問題
配的倉庫其實就兩種
<repositories>
就一般的遠程倉庫
<pluginRepositories>
這個是下載插件的倉庫
私服和第三方遠程倉庫都是 遠程倉庫 只不過私服會緩存下來酪刀,方便第二次下載jar包局域網(wǎng)更快
第二個優(yōu)先級
- 按照
settings.xml
的<repositories>
里的順序從上往下 -
pom.xml
的<repositories>
優(yōu)先級是最低的
也就是說你先 使用指定的倉庫 直接配置到<repositories>
第一個最快捷最蕾,更合理的方式是使用<activeProfiles>
來切換吧
倉庫分類
- 本地倉庫
- 遠程倉庫
- 中央倉庫
- 私服
- 其它公共倉庫
本地倉庫 就是本地文件夾配置在settings.xml
里的 <localRepositoryD:\maven_new_repository</localRepository
中央倉庫 只得就是maven剛安裝好后的maven中央倉庫<url>http://repo.maven.apache.org/maven2</url>
遠程倉庫 通過<repositories> <repository>
配置的就是遠程倉庫
私服 這是一種特殊的遠程倉庫,簡單來講就是為了緩存 其它遠程倉庫的請求
- 減少重復請求造成的外網(wǎng)帶寬消耗
- 有些構件無法從外部倉庫獲得的時候瓢湃,我們可以把這些構件部署到內(nèi)部倉庫(私服)中姻采,供內(nèi)部maven項目使用
- 降低中央倉庫的負荷:maven中央倉庫被請求的數(shù)量是巨大的妈橄,配置私服也可以大大降低中央倉庫的壓力
插件倉庫<pluginRepository>
這個是配置插件的下載倉庫
學習下repositories配置
<repositories>
<repository>
<id>jboss</id>
<name>JBoss Repository</name>
<url>http://repository.jboss.com/maven2/</url>
<releases>
<updatePolicy>daily</updatePolicy><!-- never,always,interval n -->
<enabled>true</enabled>
<checksumPolicy>warn</checksumPolicy><!-- fail,ignore -->
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<layout>default</layout>
</repository>
</repositories>
<updatePolicy>
元素:表示更新的頻率,值有:never, always,interval,daily, daily 為默認值
<checksumPolicy>
元素:表示maven檢查和檢驗文件的策略痕檬,warn為默認值
遠程倉庫的認證配置在
<server>
<id>same with repository id in pom</id>
<username>username</username>
<password>pwd</password>
</server>
注:這里的id必須與POM中需要認證的repository元素的Id一致哼鬓。
如何將生成的項目部署到遠程倉庫
完成這項工作,也需要在POM中進行配置知押,這里有新引入了一個元素:<distributionManagement>
distributionManagement包含了2個子元素:repository
和snapshotRepository
, 前者表示發(fā)布版本構件的倉庫叹螟,后者表示快照版本的倉庫
這兩個元素都需要配置 id(該遠程倉庫的唯一標識),name台盯,url(表示該倉庫的地址)
向遠程倉庫中部署構件,需要進行認證畏线。配置同上
maven到底是如何從倉庫中解析構件的呢静盅?
- 依賴范圍是system的時候->本地文件系統(tǒng)
- 嘗試直接從本地倉庫尋找構件,如果發(fā)現(xiàn)相應構件寝殴,則解析成功
- 在本地倉庫不存在相應的構件情況下蒿叠,如果依賴的版本是顯示的發(fā)布版本構件,則遍歷所有的遠程倉庫