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.