Nexus安裝、使用說明、問題總結
1 . 私服簡介
私服是架設在區域網的一種特殊的遠端倉庫,目的是代理遠端倉庫及部署第三方構件。有了私服之後,當 Maven 需要下載構件時,直接請求私服,私服上存在則下載到本地倉庫;否則,私服請求外部的遠端倉庫,將構件下載到私服,再提供給本地倉庫下載。
我們可以使用專門的 Maven 倉庫管理軟體來搭建私服,比如:Apache Archiva,Artifactory,Sonatype Nexus。這裡我們使用 Sonatype Nexus。
2 . 安裝Nexus
2 . 1 . 下載Nexus
Nexus 專業版是需要付費的,這裡我們下載開源版 Nexus OSS。Nexus 提供兩種安裝包,一種是包含 Jetty 容器的 bundle 包,另一種是不包含容器的 war 包。下載地址:http://www.sonatype.org/nexus/go。
2 . 2 . 使用bundle安裝包安裝Nexus
解壓安裝包nexus-2.8.1-bundle.zip,開啟命令提示符,進入/nexus-2.8.1-01目錄,鍵入nexus命令(為方便啟動和退出Nexus,可將bin目錄新增到環境變數):
執行 nexus install 將Nexus安裝為Windows服務。可將服務啟動方式設為手動,以後通過 nexus start 即可啟動Nexus ,通過 nexus stop 退出Nexus:
開啟瀏覽器,訪問:http://localhost:8081/nexus/:
點選右上角 Log In,使用使用者名稱:admin ,密碼:admin123 登入,可使用更多功能:
3 . Nexus預置的倉庫
點選左側 Repositories 連結,檢視 Nexus 內建的倉庫:
Nexus 的倉庫分為這麼幾類:
- hosted 宿主倉庫:主要用於部署無法從公共倉庫獲取的構件(如 oracle 的 JDBC 驅動)以及自己或第三方的專案構件;
- proxy 代理倉庫:代理公共的遠端倉庫;
- virtual 虛擬倉庫:用於適配 Maven 1;
- group 倉庫組:Nexus 通過倉庫組的概念統一管理多個倉庫,這樣我們在專案中直接請求倉庫組即可請求到倉庫組管理的多個倉庫。
4 . 新增代理倉庫
以 Sonatype 為例,新增一個代理倉庫,用於代理 Sonatype 的公共遠端倉庫。點選選單 Add - Proxy Repository :
填寫Repository ID - sonatype;Repository Name - Sonatype Repository;
Remote Storage Location - http://repository.sonatype.org/content/groups/public/ ,save 儲存:
將新增的 Sonatype 代理倉庫加入 Public Repositories 倉庫組。選中 Public Repositories,在 Configuration 選項卡中,將 Sonatype Repository 從右側 Available Repositories 移到左側 Ordered Group Repositories,save 儲存:
5 . 搜尋構件
為了更好的使用 Nexus 的搜尋,我們可以設定所有 proxy 倉庫的 Download Remote Indexes 為 true,即允許下載遠端倉庫索引。
索引下載成功之後,在 Browse Index 選項卡下,可以瀏覽到所有已被索引的構件資訊,包括座標、格式、Maven 依賴的 xml 程式碼:
有了索引,我們就可以搜尋了:
6 . 配置Maven使用私服
私服搭建成功,我們就可以配置 Maven 使用私服,以後下載構件、部署構件,都通過私服來管理。
在 settings.xml 檔案中,為所有倉庫配置一個映象倉庫,映象倉庫的地址即私服的地址(這兒我們使用私服公共倉庫組 Public Repositories 的地址):
<mirrors> <mirror> <id>central</id> <mirrorOf>*</mirrorOf> <!-- * 表示讓所有倉庫使用該映象--> <name>central-mirror</name> <url>http://localhost:8081/nexus/content/groups/public/</url> </mirror> </mirrors>
PS1:私有maven倉庫Nexus使用http與https協議
Http協議:沒有問題
Https協議:
Could not GET 'https://some_server.com/some/path/some.pom'.
> peer not authenticated
解決:
If you get any other error like this:
Could not GET 'https://some_server.com/some/path/some.pom'.
> peer not authenticated
Then you need to import a certificate:
- open the 'https://some_server.com/some/path/some.pom' in your favorite browser
- export the cert using the Steps to export cert from a web site
- copy the cer into
JDK_HOME/jre/lib/security
folder - open a shell and go to
JDK_HOME/jre/lib/security
folder - then import the cer into java using the
keytool -import -alias <the short name of the server> -file <cert_file_name_you_exported.cer> -keystore cacerts -storepass changeit
It will prompt you to import the certificate, type yes and press enter.
Then restart your eclipse and try building the project.