maven 私服
1. 需求
正式開發,不同的項目組開發不同的工程。
ssm_dao 工程開發完畢,釋出到私服。
ssm_service 從私服下載下傳 dao
2. 分析
公司在自己的區域網路内搭建自己的遠端倉庫伺服器,稱為私服,私服伺服器即是公司内部的 maven 遠端倉庫,每個員工的電腦上安裝 maven 軟體并且連接配接私服伺服器,員工将自己開發的項目打成 jar 并釋出到私服伺服器,其它項目組從私服伺服器下載下傳所依賴的構件(jar)。
私服還充當一個代理伺服器,當私服上沒有 jar 包會從網際網路中央倉庫自動下載下傳。
3. 搭建私服環境
3.1 下載下傳 nexus
Nexus 是 Maven 倉庫管理器,通過 nexus 可以搭建 maven 倉庫,同時 nexus 還提供強大的倉庫管理功能,構件搜尋功能等。
下載下傳 Nexus, 下載下傳位址:http://www.sonatype.org/nexus/archived/
下載下傳:nexus-2.12.0-01-bundle.zip
3.2 安裝 nexus
解壓 nexus-2.12.0-01-bundle.zip,本教程将它解壓在 F 盤,進入 bin 目錄:cmd 進入 bin 目錄,執行 nexus.bat install安裝成功在服務中檢視有 nexus 服務:
3.3 解除安裝 nexus
cmd 進入 nexus 的 bin 目錄,執行:nexus.bat uninstall
檢視 window 服務清單 nexus 已被删除。
3.4 啟動 nexus
方法 1:
cmd 進入 bin 目錄,執行 nexus.bat start
方法 2:
在系統的服務中直接啟動 nexus 服務
檢視 nexus 的配置檔案 conf/nexus.properties:
#Jetty section
application-port=8081 # nexus 的通路端口配置
application-host=0.0.0.0 # nexus 主機監聽配置(不用修改)
nexus-webapp=${bundleBasedir}/nexus # nexus 工程目錄
nexus-webapp-context-path=/nexus # nexus 的 web 通路路徑
#Nexus section
nexus-work=${bundleBasedir}/../sonatype-work/nexus # nexus 倉庫目錄
runtime=${bundleBasedir}/nexus/WEB-INF # nexus 運作程式目錄
通路檔案中得到的資訊:
http://localhost:8081/nexus/
使用 Nexus 内置賬戶 admin/admin123 登陸:
點選右上角的 Log in,輸入賬号和密碼 登陸
3.5 倉庫類型
檢視 nexus 的倉庫:
nexus 的倉庫有 4 種類型:
-
hosted,宿主倉庫,部署自己的 jar 到這個類型的倉庫,包括 releases 和 snapshot 兩部
分,Releases 公司内部釋出版本倉庫、 Snapshots 公司内部測試版本倉庫
- proxy,代理倉庫,用于代理遠端的公共倉庫,如 maven 中央倉庫,使用者連接配接私服,私服自動去中央倉庫下載下傳 jar 包或者插件。
- group,倉庫組,用來合并多個 hosted/proxy 倉庫,通常我們配置自己的 maven 連接配接倉庫組。
- virtual(虛拟):相容 Maven1 版本的 jar 或者插件
nexus 倉庫預設在 sonatype-work 目錄中,其中:
central:代理倉庫,代理中央倉庫
apache-snapshots:代理倉庫
存儲 snapshots 構件,代理位址 https://repository.apache.org/snapshots/
central-m1:virtual 類型倉庫,相容 Maven1 版本的 jar 或者插件
releases:本地倉庫,存儲 releases構件。
snapshots:本地倉庫,存儲 snapshots構件。
thirdparty:第三方倉庫
public:倉庫組
4. 将項目釋出到私服
4.1 需求
企業中多個團隊協作開發通常會将一些公用的元件、開發子產品等釋出到私服供其它團隊或子產品開發人員使用。
本例子假設多團隊分别開發 ssm_dao、ssm_service、ssm_web,某個團隊開發完在ssm_dao 會将 ssm_dao 釋出到私服供 ssm_service 團隊使用。
4.2 配置
第一步:需要在用戶端即部署 ssm_dao 工程的電腦上配置 maven環境,并修改 settings.xml檔案,配置連接配接私服的使用者和密碼 。
此使用者名和密碼用于私服校驗,因為私服需要知道上傳的賬号和密碼是否和私服中的賬号和
密碼一緻。
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
releases 連接配接釋出版本項目倉庫
snapshots 連接配接測試版本項目倉庫
第二步: 配置項目 pom.xml
配置私服倉庫的位址,本公司的自己的 jar 包會上傳到私服的宿主倉庫,根據工程的版本号決定上傳到哪個宿主倉庫,如果版本為 release 則上傳到私服的 release 倉庫,如果版本為snapshot 則上傳到私服的 snapshot 倉庫
注意:pom.xml 這裡<id> 和 settings.xml 配置 <id> 對應!
<distributionManagement>
<repository>
<id>releases</id>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
4.3 測試
将項目 dao 工程打成 jar 包釋出到私服:
1、首先啟動 nexus
2、對 ssm_dao 工程執行 deploy 指令
根據本項目pom.xml中version定義決定釋出到哪個倉庫,如果version定義為snapshot,執行 deploy後檢視 nexus 的 snapshot倉庫,如果 version定義為 release則項目将釋出到 nexus的 release 倉庫。也可以通過 私服的http 方式檢視。
5. 從私服下載下傳 jar 包
5.1 需求
沒有配置 nexus 之前,如果本地倉庫沒有,去中央倉庫下載下傳,通常在企業中會在區域網路内部署一台私服伺服器,有了私服本地項目首先去本地倉庫找 jar,如果沒有找到則連接配接私服從私服下載下傳 jar 包,如果私服沒有 jar 包私服同時作為代理伺服器從中央倉庫下載下傳 jar 包,這樣做的好處是一方面由私服對公司項目的依賴 jar 包統一管理,一方面提高下載下傳速度,項目連接配接私服下載下傳 jar 包的速度要比項目連接配接中央倉庫的速度快的多。
5.2 管理倉庫組
nexus中包括很多倉庫,hosted中存放的是企業自己釋出的jar包及第三方公司的jar包,
proxy 中存放的是中央倉庫的 jar,為了友善從私服下載下傳 jar 包可以将多個倉庫組成一個倉庫組,每個工程需要連接配接私服的倉庫組下載下傳 jar 包。
5.3 在 setting.xml 中配置倉庫
在用戶端的 setting.xml 中配置私服的倉庫,由于 setting.xml 中沒有 repositories 的配置标簽需要使用 profile 定義倉庫。
<profile>
<!--profile 的 id-->
<id>dev</id>
<repositories>
<repository>
<!--倉庫 id,repositories 可以配置多個倉庫,保證 id 不重複-->
<id>nexus</id>
北京市昌平區建材城西路金燕龍辦公樓一層 電話:400-618-9090
<!--倉庫位址,即 nexus 倉庫組的位址-->
<url>http://localhost:8081/nexus/content/groups/public/</url>
<!--是否下載下傳 releases 構件-->
<releases>
<enabled>true</enabled>
</releases>
<!--是否下載下傳 snapshots 構件-->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!-- 插件倉庫,maven 的運作依賴插件,也需要從私服下載下傳插件 -->
<pluginRepository>
<!-- 插件倉庫的 id 不允許重複,如果重複後邊配置會覆寫前邊 -->
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
使用 profile 定義倉庫需要激活才可生效。
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
配置成功後通過 eclipse 檢視有效 pom,有效 pom 是 maven 軟體最終使用的 pom 内容,程式員不直接編輯有效 pom,打開有效 pom
有效 pom 内容如下:
下邊的 pom 内容中有兩個倉庫位址,maven 會先從前邊的倉庫的找,如果找不到 jar 包再從下邊的找,進而就實作了從私服下載下傳 jar 包。
<repositories>
<repository>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</repository>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</pluginRepository>
<pluginRepository>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
</pluginRepository>
</pluginRepositories>
5.4 測試從私服下載下傳 jar 包
測試 1:區域網路環境或本地網絡即可
在 ssm_service 工程中添加以上配置後,添加 ssm_dao 工程的依賴,删除本地倉庫中 ssm_dao工程,同時在 eclipse 中關閉 ssm_dao 工程。
項目先從本地倉庫找 ssm_dao,找不到從私服找,由于之前執行 deploy 将 ssm_dao 部署到私服中,是以成功從私服下載下傳 ssm_dao 并在本地倉庫儲存一份。
測試 2:需要網際網路環境
在項目的 pom.xml 添加一個依賴,此依賴在本地倉庫和私服都不存在,maven 會先從本地倉庫找,本地倉庫沒有再從私服找,私服沒有再去中央倉庫下載下傳,jar 包下載下傳成功在私服、本地倉庫分别存儲一份。
把第三方 jar 包放入本地倉庫或私服
1. 導入本地庫
随便找一個 jar 包測試,可以先 CMD進入到 jar 包所在位置,運作
mvn install:install-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dfile= fastjson-1.1.37.jar -Dpackaging=jar
2. 導入私服
需要在 maven 軟體的核心配置檔案 settings.xml 中配置第三方倉庫的 server 資訊
<server>
<id>thirdparty</id>
<username>admin</username>
<password>admin123</password>
</server>
才能在cmd視窗執行指令,需要先在cmd視窗進入jar包所在位置
mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37
-Dpackaging=jar -Dfile=fastjson-1.1.37.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty
3. 參數說明
DgroupId 和 DartifactId 構成了該 jar 包在 pom.xml 的坐标,項目就是依靠這兩個屬性定位。自己起名字也行。
Dfile 表示需要上傳的 jar 包的絕對路徑。
Durl 私服上倉庫的位置,打開 nexus——>repositories 菜單,可以看到該路徑。
DrepositoryId 伺服器的表示 id,在 nexus 的 configuration 可以看到。
Dversion 表示版本資訊,
關于 jar 包準确的版本:
包的名字上一般會帶版本号,如果沒有那可以解壓該包,會發現一個叫 MANIFEST.MF 的檔案,這個檔案就有描述該包的版本資訊。
比如 Specification-Version: 2.2 可以知道該包的版本了。
上傳成功後,在 nexus 界面點選 3rd party 倉庫可以看到這包。