1.1 私服倉庫是什么
私服,私有服務器,是公司內部Maven項目經常需要的東東。Nexus是常用的私用Maven服務器,一般是公司內部使用。其作用指定私服的中央地址、將自己的Maven項目指定到私服地址、從私服下載中央庫的項目索引、從私服倉庫下載依賴組件、將第三方項目jar上傳到私服供其他項目組使用。
1.2 Nexus中的倉庫
1.2.1 類型介紹:
Ø hosted,本地倉庫,通常我們會部署自己的構件到這一類型的倉庫。比如公司的第二方庫。
Ø proxy,代理倉庫,它們被用來代理遠程的公共倉庫,如maven中央倉庫。
Ø group,倉庫組,用來合並多個hosted/proxy倉庫,當你的項目希望在多個repository使用資源時就不需要多次引用了,只需要引用一個group即可。

1.2.2 管理本地倉庫
我們前面講到類型為hosted的為本地倉庫,Nexus預定義了3個本地倉庫,分別是Releases, Snapshots, 3rd Party. 分別講一下這三個預置的倉庫都是做什么用的:
Releases:
這里存放我們自己項目中發布的構建, 通常是Release版本的, 比如我們自己做了一個FTP Server的項目, 生成的構件為ftpserver.war, 我們就可以把這個構建發布到Nexus的Releases本地倉庫. 關於符合發布后面會有介紹.
Snapshots:
這個倉庫非常的有用, 它的目的是讓我們可以發布那些非release版本, 非穩定版本, 比如我們在trunk下開發一個項目,在正式release之前你可能需要臨時發布一個版本給你的同伴使用, 因為你的同伴正在依賴你的模塊開發, 那么這個時候我們就可以發布Snapshot版本到這個倉庫, 你的同伴就可以通過簡單的命令來獲取和使用這個臨時版本.
3rd Party:
顧名思義, 第三方庫, 你可能會問不是有中央倉庫來管理第三方庫嘛,沒錯, 這里的是指可以讓你添加自己的第三方庫, 比如有些構件在中央倉庫是不存在的. 比如你在中央倉庫找不到Oracle 的JDBC驅動, 這個時候我們就需要自己添加到3rdparty倉庫。
1.2.3 maven項目索引
下載Maven項目索引,項目索引是為了使用者能夠在私服站點查找依賴使用的功能

保存后后台會運行一個任務,點擊菜單欄的Scheduled Tasks選項即可看到有個任務在RUNNING。 下載完成后,Maven索引就可以使用了,在搜索欄輸入要搜索的項,就可以查到相關的信息。例如:junit

1.3 配置私服鏈接
在項目的pom.xml或者settings.xml文件里加入一下配置信息(區別,pom.xml是針對當前項目,settings.xml是全局的針對所有項目)
1.3.1 pom.xml 針對當前項目
配置相關私服信息指定倉庫:
<repositories>
<repository>
<id>nexus</id>
<name>nexus</name>
<url>http://127.0.0.1:8081/nexus/content/groups/public/</url>
<!-- Release版本則代表穩定的版本 -->
<releases>
<enabled>true</enabled>
</releases>
<!-- Snapshot版本代表不穩定、尚處於開發中的版本,默認關閉,需要手動啟動 -->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
指定插件倉庫:
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>nexus</name>
<url>http://192.168.1.103:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
1.3.2 settings.xml 針對全局所有項目
1.3.2.1 Maven倉庫組:
我們可以建立多個proxy代理倉庫,hosted本地倉庫, 如果沒有倉庫組的概念,我們如果需要引用這些倉庫的是時候需要一一加入到我們的setting.xml里面去, 有了倉庫的組的概念, 我們只需要做一次引用就可以了,把我們需要的倉庫加入到倉庫組即可。如下:
<!-- 配置相關私服信息指定倉庫 -->
<profiles>
<profile>
<id>nexusProfile</id>
<repositories>
<repository>
<id>nexus</id>
<name>nexus Repository</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<layout>defult</layout>
<releases>
<enabled>true</enabled>
</releases>
<!-- snapshots默認是關閉的false,必須顯示的打開 -->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<!-- 激活 -->
<activeProfiles>
<activeProfile>nexusProfile</activeProfile>
</activeProfiles>
倉庫組默認包含本地倉庫Releases,snapshots, 3rd party和代理倉庫MavenCentral. 你可以在Configuration配置頁添加倉庫到這個倉庫組. 如果需要你還可以創建一個倉庫組,在Repositories菜單處選add-->Repository Group即可:

1.3.2.1 配置倉庫鏡像:
默認的,如果本地倉庫找不到依賴的構件,這時需要東西時先到Nexus上找,如果發現Nexus服務關閉后,會自動到中央倉庫找。
如果我們想覆蓋中央倉庫的默認地址,強制依賴的東西都到Nexus中去找,即使Nexus關閉也不會到中央工廠去下載:
<!--工廠的鏡像,只要工廠的訪問,都會自動來找鏡像,如果鏡像里面沒有找到就無法訪問,就停止下載了 -->
<mirrors>
<mirror>
<id>nexusMirror</id>
<mirrorOf>nexus</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</mirror>
</mirrors>
1.3.3 配置項目發布
<distributionManagement>
<repository>
<id>user-release</id>
<name>User Project Release</name>
<url>http://192.168.1.103:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>user-snapshots</id>
<name>User Project SNAPSHOTS</name>
<url>http://192.168.1.103:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
1.4 宿主庫——3rd party


1.5 如果本地倉庫有東西,但Nexus中沒有
需要將該東西考到Nexus倉庫中,然后更新倉庫索引:

本文參考:zec0910的專欄、大鵬
