一、前言
私服是一種特殊的 Maven 遠程倉庫,通過建立自己的私服,就可以降低中央倉庫負荷、節省外網帶寬、加速Maven構建、自己部署構件等,從而高效地使用 Maven。
一、Nexus安裝
Nexus是典型的JavaWeb應用,它有兩種安裝包:
- 一種是包含Jetty容器的Bundle包
- 另一種是不包含Web容器的war包。
1.下載地址
2.安裝
將下載後的zip解壓之後,會發現包含如下兩個子目錄:
- nexus-3.14.0-04 :即HOME目錄,該目錄包含了Nexus運行所需要的文件,如啓動腳本、依賴jar包等。
- sonatype-work :即工作目錄,該目錄包含Nexus生成的配置文件、日誌文件、倉庫文件等。Nexus會在運行時創建此目錄。需要備份nexus的時候,備份此目錄即可。
3.啓動
用戶只需要調用對應操作系統的腳本就可以啓動Nexus。
windows下,以管理員權限運行cmd,進入到bin目錄,然後執行如下命令:
nexus.exe /run
二、Nexus的倉庫與倉庫組
1.內置倉庫
1.1 倉庫信息
倉庫主要包含如下信息
- 倉庫類型:
- group(倉庫組)
- hosted(宿主)
- proxy(代理)
- virtual(虛擬)
- 倉庫格式:
- maven2
- maven1
- Policy(策略)
- Release(發佈版本倉庫)
- Snapshot(快照版本倉庫)
- 倉庫狀態
- 倉庫路徑
1.2 內置倉庫列表
序號 | 倉庫 | 描述 |
---|---|---|
1 | MavenCentral | 該倉庫代理Maven中央倉庫,其策略爲Release,因此只會下載和緩存中央倉庫中的發佈版本構件 |
2 | Releases | 這是一個策略爲Release的宿主類型倉庫,用來部署組織內部的發佈版本構件 |
3 | Snapshots | 這是一個策略爲Snapshot的宿主類型倉庫,用來部署組織內部的快照版本構件 |
4 | 3rd party | 這是一個策略爲Release的宿主類型倉庫,用來部署無法從公共倉庫獲得的第三方發佈版本構件 |
5 | Apache Snapshots | 這是一個策略爲Snapshot的代理倉庫,用來代理ApacheMaven倉庫的快照版本構件 |
6 | Codehaus Snapshots | 這是一個策略爲Snapshot的代理倉庫,用來代理CodehausMaven倉庫的快照版本構件 |
7 | Google Code | 這是一個策略爲Release的代理倉庫,用來代理GoogleCodeMaven倉庫的發佈版本構件 |
8 | java.net-Maven 2 | 這是一個策略爲Release的代理倉庫,用來代理java.netMaven倉庫的發佈版本構件 |
9 | Public Repositories | 該倉庫組將上述所有策略爲Release的倉庫聚合並通過一致的地址提供服務 |
10 | Public Snapshot Repositories | 該倉庫組將上述所有策略爲Snapshot的倉庫聚合並通過一致的地址提供服務 |
2.倉庫分類
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-ke6E2rq4-1593434122500)(images/1546837552969.png)]
- Maven可以直接從宿主倉庫下載構件;
- Maven也可以從代理倉庫下載構件,而代理倉庫會間接地從遠程倉庫下載並緩存構件;
- 最後,爲了方便,Maven可以從倉庫組下載構件,而倉庫組沒有實際內容(圖中用虛線表示),它會轉向其包含的宿主倉庫或者代理倉庫獲得實際構件的內容。
2.1 創建宿主倉庫
2.2 創建代理倉庫
2.3 創建倉庫組
三、Nexus相關配置
1.配置Maven從Nexus下載構建
1.1 配置Nexus倉庫
配置Nexus倉庫有兩種方式:
pom.xml
: 項目範圍~/.m2/setting.xml
: 用戶範圍
1.1.1 pom.xml
通過 project 根元素下的 repositories 、pluginRepositories 元素,可以添加nexus遠程倉庫。
這樣當maven需要下載構建的時候就會從nexus下載了,不過pom.xml中的配置只對當前項目生效,作用範圍是項目範圍。
<!-- 私有倉庫 -->
<repositories>
<repository>
<id>RDC thirdparty</id>
<name>RDC thirdparty Repository</name>
<url>http://nexus.saas.hand-china.com/content/repositories/thirdparty</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>RDC thirdparty</id>
<name>RDC thirdparty Repository</name>
<url>http://nexus.saas.hand-china.com/content/repositories/thirdparty</url>
</pluginRepository>
</pluginRepositories>
1.1.2 ~/.m2/setting.xml
pom.xml 中的配置只對當前Maven項目有效,在實際應用中,我們往往想要通過一次配置就能讓本機所有的Maven項目都使用自己的Maven私服,這時就需要用到 ~/.m2/setting.xml
<settings>
...
<profiles>
<profile>
<id>Nexus</id>
<repositories>
<repository>
<id>nexus</id>
<url>http://nexus.saas.hand-china.com/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://nexus.saas.hand-china.com/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>
Nexus
</activeProfile>
</activeProfiles>
...
</settings>
該配置中使用了一個id爲nexus的profile,這個profile包含了相關的倉庫配置,同時配置中又使用activeProfile元素將nexus這個profile激活,這樣當執行Maven構建的時候,激活的profile會將倉庫配置應用到項目中去。
1.2 配置鏡像
配置鏡像讓Maven只使用私服:
可以創建一個匹配任何倉庫的鏡像,鏡像的地址爲私服,這樣,Maven對任何倉庫的構件下載請求都會轉到私服中
<settings>
...
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://nexus.saas.hand-china.com/content/groups/public</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>Nexus</id>
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>
Nexus
</activeProfile>
</activeProfiles>
...
</settings>
2.部署構件至Nexus
對於一些組織內部發布的,或者一些無法從公共倉庫中獲得的第三方構件,可以將其部署至Nexus,供大家下載使用。
用戶可以配置Maven自動部署構件至Nexus的宿主倉庫,也可以通過界面手動上傳構件。
日常開發生成的快照版本構件可以直接部署到Nexus中策略爲Snapshot的宿主倉庫中,項目正式發佈的構件則應該部署到Nexus中策略爲Release的宿主倉庫中。
2.1 配置Maven部署構件至Nexus
2.1.1 distributionManagement
在 pom.xm
中通過distributionManagement
配置構件要發佈至倉庫
<distributionManagement>
<repository>
<id>releases</id>
<url>http://nexus.saas.hand-china.com/content/repositories/panda-release</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://nexus.saas.hand-china.com/content/repositories/panda-snapshot</url>
</snapshotRepository>
</distributionManagement>
2.1.2 servers
Nexus的倉庫對於匿名用戶是隻讀的。爲了能夠部署構件,還需要在settings.xml中配置認證信息。
<servers>
<server>
<id>releases</id>
<username>hec-deployer</username>
<password>123456</password>
</server>
<server>
<id>snapshots</id>
<username>hec-deployer</username>
<password>123456</password>
</server>
<server>
<id>3rd</id>
<username>hec-deployer</username>
<password>123456</password>
</server>
</servers>
2.2 手動部署第三方構件至Nexus
去nexus中操作。