macOS下搭建Nexus 3.x服务器作为Maven的资源仓库服务器
文章目錄
- 下載 Nexus 服務器安裝包
- 安裝 Nexus 服務器
- 配置環(huán)境變量/啟動服務器/停止服務器
- Nexus 服務器管理后臺
- 登錄管理后臺
- Blob Stores
- Nexus 初始密碼無法登錄的問題
- 倉庫(Repository)
- Nexus 內置的倉庫
- Repository 的 Type 屬性
- 創(chuàng)建代理倉庫
- 創(chuàng)建本地倉庫(也叫宿主倉庫/私有倉庫)
- 配置 Nexus 服務器
- Nexus 命令介紹
- 遷移 Nexus 服務器中的數(shù)據(jù)
- 刪除 Nexus 服務器中的數(shù)據(jù)
- 升級 Nexus
- 部署工件到服務器
- 通過配置文件發(fā)布工件到 Nexus 服務器(第一種配置方式)
- 通過配置文件發(fā)布工件到 Nexus 服務器(第二種配置方式)
- 通過 Maven 命令發(fā)布工件到 Nexus 服務器
- 選擇本地工件上傳到 Nexus 宿主倉庫
- 從 Nexus 服務器自動下載依賴工件
- settings.xml 配置私服
- 配置 profile 元素
- 配置 mirror 元素(鏡像倉庫配置)
- pom.xml 配置私服
- Nexus 3 和 Nexus 2 的區(qū)別
下載 Nexus 服務器安裝包
目前的版本有 2.X 和 3.X ,2.X 對 Maven 的支持更友好一點,3.X 的支持范圍更廣,支持 ruby 和 docker。但是 3.X 要求 JDK 的版本是1.8,而且貌似還需要做特殊配制。有人下載下來解壓后,運行不起來,看 sonatype-work 里面的 logs 報的是缺某個 jar 包,所以如果沒有別的需要,只是搭建單純的 Maven 私服,建議直接使用版本 2.x。
官網地址:https://www.sonatype.com/。
我在下面也給出了直接下載的地址:
點擊下載 Nexus Repository Manager OSS 3.x /點擊下載 Nexus Repository Manager OSS 2.x /點擊下載 Nexus Repository 3.x。
通過官網下載 nexus 服務器安裝包可能因為網速的問題會導致下載失敗,我已經把安裝包上傳到百度云盤:
鏈接: https://pan.baidu.com/s/11E8lMR3-j71aUbYeK__Tyg
提取碼: e1fd
安裝 Nexus 服務器
下載安裝包后,將其解壓到你需要的目錄下(例如:用戶主目錄/Applications/),我就是解壓到用戶主目錄下的 Applications 目錄下,如下圖所示:
解壓后你可以看到目錄 nexus-3.20.1-01-mac,在這個目錄下有兩個子目錄 nexus-3.20.1-01 和 sonatype-work。nexus-3.20.1-01 是程序,sonatype-work 是工作目錄,如果以后要備份數(shù)據(jù),只要備份 sonatype-work 就行。
在與 nexus-3.20.1-01 目錄同級的目錄下創(chuàng)建一個指向 /Users/liaowenxiong/Applications/nexus-3.20.1-01-mac/nexus-3.20.1-01 的軟鏈接,以方便日后的升級:
[~/Applications/nexus-3.20.1-01-mac]$ ln -s /Users/liaowenxiong/Applications/nexus-3.20.1-01-mac/nexus-3.20.1-01 /Users/liaowenxiong/Applications/nexus-3.20.1-01-mac/nexus-latest [~/Applications/nexus-3.20.1-01-mac]$ ls nexus-3.20.1-01 nexus-latest sonatype-work配置環(huán)境變量/啟動服務器/停止服務器
在配置有關環(huán)境變量之前,要啟動 nexus 服務器,你需要打開命令終端,切換到 ~/Applications/nexus-3.20.1-01-mac/nexus-3.20.1-01/bin 目錄下,再執(zhí)行啟動命令,如下所示:
[~/Applications/nexus-3.20.1-01-mac/nexus-3.20.1-01/bin]$ ./nexus start Starting nexus [~/Applications/nexus-3.20.1-01-mac/nexus-3.20.1-01/bin]$ ./nexus status nexus is running.配置環(huán)境變量后則不必進入 nexus 的 bin 目錄下執(zhí)行啟動命令。下面介紹下如何配置 nexus 的環(huán)境變量。
編輯用戶主目錄下 .bash_profile 文件:
[~/Applications/nexus-3.20.1-01-mac]$ vim ~/.bash_profile在其中添加如下內容:
# 創(chuàng)建環(huán)境變量NEXUS_HOME export NEXUS_HOME=/Users/liaowenxiong/Applications/nexus-3.20.1-01-mac/nexus-latest # 在環(huán)境變量PATH的值后面追加nexus的bin目錄路徑 PATH=$PATH:$NEXUS_HOME/bin執(zhí)行下面的命令,使 .bash_profile 文件的配置立即生效:
[~/Applications/nexus-3.20.1-01-mac]$ source ~/.bash_profile因為前面已經配置 nexus 的環(huán)境變量,所以可以在任意目錄執(zhí)行 nexus 的命令,例如,我在用戶主目錄下執(zhí)行下面的停止服務器的命令:
[~]$ nexus stop Shutting down nexus Stopped. [~]$ nexus start Starting nexusNexus 服務器管理后臺
nexus 服務器啟動后,在瀏覽器地址欄輸入:http://localhost:8081,你可以打開 Nexus Repository 的管理后臺的首頁。
登錄管理后臺
通過右側的 Sign In 登錄管理后臺。對于未登錄的用戶,只能查詢和瀏覽相關資源。用戶登錄后就能看到管理相關的菜單了。
早期版本的 Nexus 的管理后臺默認登錄用戶名是 admin,默認登錄密碼 admin123,最新版的 Nexus 管理后臺默認登錄密碼不再是 admin123,具體參見下面的章節(jié)。
Blob Stores
Blob Stores 是用來配置資源的保存位置的,可以將不同的資源保存到服務器的不同位置上,類似于 Nexus 2 的資源保存路徑。
Nexus 初始密碼無法登錄的問題
之前版本的 nexus 系統(tǒng)管理后臺默認的登錄用戶名和密碼是:admin/admin123,新版的 nexus 的管理后臺默認登錄密碼不再是 admin123。初始密碼保存在 nexus 安裝目錄下的 sonatype-work/nexus3/admin.password 文件中,打開文件可以看到如下內容:
上面的內容即為密碼明文,而非加密后的結果,直接復制到密碼輸入框即可。
登錄成功之后會要求修改密碼,密碼修改成功后,admin.password 文件會自動被刪除。
倉庫(Repository)
Nexus 內置的倉庫
上圖中的 Format 列:maven2 是Java 倉庫;nuget 是 .net 倉庫。
Repository 的 Type 屬性
Repository 的 Type 有三種:proxy、hosted、group。
proxy:代理倉庫。即你可以設置代理遠程的倉庫,設置了代理之后,在你的 Nexus 倉庫中找不到需要的工件就會到代理的遠程倉庫中查找
hosted:本地倉庫。即本地的私有倉庫。通常用來存放和管理非中央倉庫或者你自己開發(fā)的工件包。這倉庫有 release 和 snapshots 兩種類型,自己在構建工件包的時候,就需要指定 release(正式版/發(fā)行版) 還是 snapshots(開發(fā)版)。
group:組合倉庫。這種類型的倉庫可以組合多個倉庫,然后用戶只要訪問這個組合倉庫,就可以同時訪問被組合的其它成員倉庫。一般用來給用戶一個訪問 Nexus 倉庫的統(tǒng)一地址。組合倉庫中可以看到成員倉庫中的工件資源,但是無權刪除。
實際的使用場景通常是這樣的,你可以上傳私有的工件到 hosted 倉庫中,代理倉庫中配置被代理的遠程倉庫的 URL 以獲取第三方的依賴(比如可以配置中央倉庫的地址)。本地私有倉和代理倉都弄好了之后,再通過 group 倉庫進行組合,這樣就可以將組合倉庫的 URL 對外提供使用了。
總結:
Maven 可以直接從宿主倉庫(本地私有倉庫)下載構件;Maven 也可以從代理倉庫下載構件,而代理倉庫中若沒有請求的構件,則會自動地從被代理的遠程倉庫下載構件并緩存構件;Maven 可以從組合倉庫下載構件,而組合倉庫沒有請求的構件,組合倉庫會轉向其包含的宿主倉庫或者代理倉庫獲取請求的構件。
組合倉庫的詳情界面如下圖所示:
特別注意,組倉庫會按從上到下的順序訪問成員倉庫。
創(chuàng)建代理倉庫
Repository Name 和 Remote storage 填寫好之后,就可以直接點擊下方的 Create repository 創(chuàng)建倉庫了。
代理倉庫創(chuàng)建好之后就可以復制倉庫的 URL,在 settings.xml 或者項目的 pom.xml 中進行私服倉庫的配置,具體請參見下面的章節(jié)。
創(chuàng)建本地倉庫(也叫宿主倉庫/私有倉庫)
配置 Nexus 服務器
打開 /Users/liaowenxiong/Applications/nexus-3.20.1-01-mac/nexus-3.20.1-01/etc 目錄下的屬性文件 nexus-default.properties,打開后默認的內容如下:
## DO NOT EDIT - CUSTOMIZATIONS BELONG IN $data-dir/etc/nexus.properties ## # Jetty section application-port=8081 application-host=0.0.0.0 nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-requestlog.xml nexus-context-path=/# Nexus section nexus-edition=nexus-pro-edition nexus-features=\ nexus-pro-featurenexus.hazelcast.discovery.isEnabled=trueapplication-host:是 nexus 服務器的 IP 地址,默認為 0.0.0.0,表示只要是本機上的 IP,就能連上 nexus 服務器。為了安全和屏蔽掉其他IP,建議改成 localhost 默認指向的 IP,即 127.0.0.1。
application-port:是 IP 的端口號,默認為 8081,如果端口號被占用,可以換成其他端口號。
nexus-context-path:上下文路徑,默認 /,類似 Java Web 項目的虛擬目錄,如果添加上下文路徑,需要在訪問地址后面加上下文路徑,即訪問地址變?yōu)?#xff1a;application-host:application-port/nexus-context-path。
配置后的內容如下:
## DO NOT EDIT - CUSTOMIZATIONS BELONG IN $data-dir/etc/nexus.properties ## # Jetty section application-port=8081 application-host=127.0.0.1 nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-requestlog.xml nexus-context-path=/nexus/content # http://127.0.0.1:8081/nexus/content# Nexus section nexus-edition=nexus-pro-edition nexus-features=\ nexus-pro-featurenexus.hazelcast.discovery.isEnabled=true配置好之后需要重啟 nexus:
[/etc]$ nexus restart Shutting down nexus Stopped. Restarting nexus然后在瀏覽器地址欄輸入:http://localhost:8081/nexus/content/
Nexus 命令介紹
nexus start
nexus stop
nexus status
nexus restart
nexus run
nexus run-redirect
nexus force-reload
遷移 Nexus 服務器中的數(shù)據(jù)
將 sonatype-work/nexus3 遷移到其它地方
刪除 Nexus 服務器中的數(shù)據(jù)
sonatype-work/nexus3 整個目錄刪除,以前緩存的數(shù)據(jù)就全部刪除了。
接著你可以重啟 Nexus,等幾秒鐘,再輸入之前的訪問地址,nexus3 目錄又會自動重新生成,之前的登錄密碼作廢了,你需要再重新打開 sonatype-work/nexus3/admin.password 文件,將里面的密碼粘貼到密碼輸入框即可完成登錄。
升級 Nexus
下載最新版的 nexus 包,解壓后僅保留 nexus-3.{new-version} 目錄,并移動到 /Users/liaowenxiong/Applications/nexus-3.20.1-01-mac
目錄下,然后重新創(chuàng)建軟鏈接 nexus-latest,指向新版本的 nexus 目錄即可:
以上升級方法適用于從 nexus-3.1.0+ 升級到 nexus3 的最新版本,官方文檔為:《Upgrading Nexus Repository Manager 3.1.0 and Newer》
部署工件到服務器
通過配置文件發(fā)布工件到 Nexus 服務器(第一種配置方式)
第一步:配置 settings.xml
在 settings.xml 配置 nexus 服務器的登錄用戶名和密碼,以及連接的倉庫名稱。
第二步:在項目的 pom.xml 文件中配置倉庫的名稱和訪問地址
直接在 project 根節(jié)點的 distributionManagement 子節(jié)點中配置如下:
<distributionManagement><!--配置倉庫類型為host(宿主倉庫)的儲存類型為release的倉庫--><repository><!-- id的名字可以任意取,但是在settings.xml文件中的<server>元素的ID子元素的內容與這里一致 --><id>release</id><!--部署(即上傳)工件的倉庫地址--><url>http://localhost:8081/nexus/content/repository/maven-releases/</url></repository><!--配置倉庫類型為host(宿主倉庫)的儲存類型為Snapshot的倉庫--><snapshotRepository><!-- id的名字可以任意取,但是在settings.xml文件中的<server>元素的ID子元素的內容與這里一致 --><id>snapshot</id><!--部署(即上傳)工件的倉庫地址--><url>http://localhost:8081/nexus/content/repository/maven-snapshots</url></snapshotRepository> </distributionManagement>問題:代理倉庫和組倉庫能否部署工件呢?沒有測試過!有時間再測試。
第三步:將項目打成指定格式的包,發(fā)布到倉庫中
項目的 pom.xml 文件中可以指定項目的打包格式,如下圖所示:
通過配置文件發(fā)布工件到 Nexus 服務器(第二種配置方式)
第一步:在 settings.xml 文件中配置
<settings>...<servers><!-- account and password for deploy to nexus --><server><id>lan</id><username>admin</username><password>admin123</password></server></servers><profiles><profile><id>lan</id><!--定義屬性(即變量)--><properties><lan-release-url>http://localhost:8081/repository/maven-releases</lan-release-url><lan-snapshot-url>http://localhost:8081/repository/maven-snapshots</lan-snapshot-url></properties></profile></profiles> </settings>第二步:在 pom.xml 中配置部署(即上傳,upload)工件的目標倉庫
<project><profiles><!-- configuration for deploy to nexus repository --><profile><!-- 這個id值要和settings.xml中定義屬性的profile的id值一致 --><id>lan</id><distributionManagement><!-- 這是配置版本類型為發(fā)布版的倉庫 --><repository><!-- 這個id值要和settings.xml中的server的id值一致 --><id>lan</id><url>${lan-release-url}</url></repository><!-- 這是配置版本類型為開發(fā)版的倉庫 --><snapshotRepository><!-- 這個id值要和settings.xml中的server的id值一致 --><id>lan</id><url>${lan-snapshot-url}</url></snapshotRepository></distributionManagement><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-source-plugin</artifactId></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-javadoc-plugin</artifactId></plugin></plugins></build></profile></profiles> </project>第三步:命令行執(zhí)行 mvn clean deploy -P lan 即可將項目打包發(fā)布到 nexus 私服,命令中的 lan 是 pom.xml 文件中的 profile 的 ID 值。
通過 Maven 命令發(fā)布工件到 Nexus 服務器
第一步:配置 settings.xml
在 settings.xml 配置 nexus 服務器的登錄用戶名和密碼,以及連接的倉庫名稱。
第二步:在 IDEA 的 Maven 命令輸入框執(zhí)行部署命令
將下面的命令語句填寫到 Maven 輸入框中:
deploy:deploy-file -DgroupId=com.example -DartifactId=demo02 -Dversion=1.0-SNAPSHOT -Dpackaging=jar -Dfile=/Users/liaowenxiong/Documents/IdeaProjects/demo02/target/demo02-1.0-SNAPSHOT.war -Durl=http://localhost:8081/nexus/content/repository/maven-snapshots/ -DrepositoryId=maven-snapshots
輸入完命令語句,按回車鍵會執(zhí)行命令語句。執(zhí)行完成后,你再到 Nexus 服務器的管理后臺查看是否發(fā)布成功,如下圖所示:
選項說明:
DgroupId 和 DartifactId 構成了該 jar 包在 pom.xml 的坐標,項目就是依靠這兩個屬性定位。可以在項目的 pom.xml 文件中查看,如下圖所示:
Dfile 表示需要上傳的工件的絕對路徑。
Durl 私服上倉庫的位置,可以在 Nexus 服務器的管理后臺查看倉庫的 URL,如下圖所示:
DrepositoryId 就是服務器的名稱。
Dversion 工件的版本信息。在項目的 pom.xml 中可以看到。
選擇本地工件上傳到 Nexus 宿主倉庫
如果通過包名和jar文件名來確定工件的坐標?
如上的 jar 文件,按 Maven 工件的命名規(guī)則,應該是 artifactId+version,解壓 jar 文件,看下圖:
所以上面的 c3p0-0.9.5.2.jar 的坐標是:
從 Nexus 服務器自動下載依賴工件
無論是自己安裝 Maven,還是 IDEA 自帶的 Maven 默認是沒有配置倉庫信息,這種情況下 Maven 默認去遠程的中心倉庫下載所依賴的工件(也叫構件)。如果希望從自己搭建的私服下載依賴的工件,就需要明確告訴 Maven 去哪里下載,可以在三個地方進行配置,分別是:
1.Maven 的安裝目錄下的 conf 目錄下的 settings.xml 文件,這個全局配置;
2.再一個是在 ~/.m2 目錄下的 settings.xml,如果不存在該文件,可以復制 conf 目錄下的 settings.xml,這個是針對當前用戶的;
3.還有一個是在 pom.xml 中指定,這個只對 pom.xml 所屬的項目有效。
以上三個文件的優(yōu)先級是 pom.xml > ~/.m2/settings.xml > conf/settings.xml。
配置好私服的倉庫信息后,當項目執(zhí)行 Maven 操作時,如果本地庫中沒有依賴的構件,Maven 會去指定的私服倉庫下載,如果指定的私服倉庫也沒有,私服倉庫會去指定的遠程倉庫下載(注:私服倉庫是 proxy 類型才會到指定的遠程倉庫下載),下載的構件會緩存在私服倉庫中,這樣當其它項目需要使用這些構件時,私服倉庫就可以直接提供下載了。
settings.xml 配置私服
如果在 settings.xml 中設置了其它的鏡像倉庫,需要將其注釋掉,我們自己配置的私服倉庫才會生效。
配置 profile 元素
在 profiles 標簽體中添加下面的元素:
<profile><!--id標簽體的內容隨意,但是要確保唯一性--><id>nexus</id><repositories><repository><!--id標簽體的內容隨意,但是要確保唯一性--><id>nexus</id><!--定義倉庫的名稱,但是在這里倉庫的名稱可以隨便命名,不必與真實的倉庫名稱相同,即name標簽體的內容隨意--><name>local private nexus</name><url>http://localhost:8081/nexus/content/repository/maven-public/</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></repository><repository><id>nexus-snapshots</id><name>local private nexus</name><url>http://localhost:8081/nexus/content/repository/maven-snapshots/</url><releases><enabled>false</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository></repositories><pluginRepositories><pluginRepository><id>nexus</id><name>local private nexus</name><url>http://localhost:8081/nexus/content/repository/maven-public/</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></pluginRepository><pluginRepository><id>nexus-snapshots</id><name>local private nexus</name><url>http://localhost:8081/nexus/content/repository/maven-snapshots/</url><releases><enabled>false</enabled></releases><snapshots><enabled>true</enabled></snapshots></pluginRepository></pluginRepositories></profile>上述的元素 id、name 可以根據(jù)自己的喜好來定義,保證 id 不重復即可,主要是倉庫的 url 的配置,不要寫錯了,倉庫的 url 可以在 nexus 服務器管理后臺的 repositories 列表直接復制,如下圖所示:
倉庫中主要存放兩種類型的構件,第一種構件被用作其它構件的依賴,這是中央倉庫中存儲的大部分構件的類型;另外一種構件類型是插件。如果不配置 pluginRepositories,那么 Maven 在下載項目所需的依賴構件時,還是會直接去遠程中央倉庫下載需要的插件構件,所以這里一定要加上這個。
另外還要在 activeProfiles 標簽體中添加下面的元素:
<activeProfile>nexus</activeProfile>activeProfile 標簽體中的內容就上面定義 profile 的 id。表示激活指定 id 的 profile。
配置 mirror 元素(鏡像倉庫配置)
還可以在 mirrors 標簽體中添加 mirror 元素來配置私服。
<!--自己搭建的Nexus服務器的倉庫地址--><mirror><!-- id表示鏡像的唯一標識,隨意填寫,但是要確保唯一性 --><id>private nexus</id><!--這里填寫被鏡像的倉庫的id,如果你沒有配置其它倉庫,只有默認的中央倉庫,而默認中央倉的id是central,所以你可以填寫central--><mirrorOf>central</mirrorOf><!-- 定義鏡像名稱,具體名稱隨意 --><name>private proxy nexus repository</name><!--私服倉庫的地址--><url>http://localhost:8081/nexus/content/repository/maven-aliyun/</url></mirror>關于 mirror 元素的配置另請參見:https://maven.apache.org/guides/mini/guide-mirror-settings.html
pom.xml 配置私服
<repositories><repository><!--id根據(jù)自己的需要命名--><id>nexus</id><!--name根據(jù)自己的需要命名--><name>local private nexus</name><url>http://192.168.1.68:8081/nexus/content/groups/public</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></repository><repository><id>nexus-snapshots</id><name>local private nexus</name><url>http://192.168.1.68:8081/nexus/content/groups/public-snapshots</url><releases><enabled>false</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository></repositories><pluginRepositories><pluginRepository><id>nexus</id><name>local private nexus</name><url>http://192.168.1.68:8081/nexus/content/groups/public</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></pluginRepository><pluginRepository><id>nexus-snapshots</id><name>local private nexus</name><url>http://192.168.1.68:8081/nexus/content/groups/public-snapshots</url><releases><enabled>false</enabled></releases><snapshots><enabled>true</enabled></snapshots></pluginRepository></pluginRepositories>pom.xml 文件中簡單配置如下:
<!--配置私服--><repositories><repository><!--id標簽體的內容隨意,只要確保唯一即可--><id>maven-aliyun</id><!--name標簽體的內容隨意--><name>local private nexus</name><!--私服倉庫的地址--><url>http://localhost:8081/nexus/content/repository/maven-aliyun/</url></repository></repositories>Nexus 3 和 Nexus 2 的區(qū)別
Nexus 2 能夠下載遠程倉庫資源的索引到 Nexus 服務器本地,供搜索查詢使用,但是 Nexus 3 暫時還不支持。在 Nexus 3 中查詢的都是已經從遠程倉庫下載并緩存到服務器本地的資源,對于遠程倉庫中沒有緩存到 Nexus 本地的資源是無法查詢到的。所以在 Nexus 3 搭建好后若沒有被訪問過,那么在 Nexus 的代理倉庫是查詢不到任何資源的。對于代理倉庫而言,需要程序先訪問 Nexus 代理倉庫,然后由倉庫向被代理的遠程倉庫發(fā)起請求,下載對應的資源并緩存到代理倉庫中,然后各個項目客戶端再從 Nexus 代理倉請求下載依賴的資源。
所以對于 Nexus 代理倉中沒有的資源,必須項目客戶端先發(fā)起請求,然后 Nexus 倉庫才會去代理的遠程中央倉庫下載資源緩存到代理倉庫中,以供其它用戶直接下載使用。
總結
以上是生活随笔為你收集整理的macOS下搭建Nexus 3.x服务器作为Maven的资源仓库服务器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 蓝宝石推出 RX 7600 显卡《猛兽派
- 下一篇: macOS安装Maven_IDEA集成M