前言想要使用maven搭建項目,但是國內的網絡環境可以想象,還有公司自己開發的jar包等問題,所以需要搭建一個maven的私服,這樣便於管理。 找了一些教程,順便記下來,當做筆記。 本文以Windows系統為例。
1. 下載官網: https://www.sonatype.com/ 下載地址: https://www.sonatype.com/nexus-repository-oss 官方文檔: https://help.sonatype.com/repomanager3 下載的時候選擇Nexus Repository OSS,帶Pro的大家都知道,是收費版。 根據自己的系統選擇對應版本,支持OS X,Windows,Unix三大系統。 本文選擇的是最新版本 nexus-3.12.0-01-win64.zip,地址https://www.sonatype.com/oss-thank-you-win64.zip
最新的3.x版本除了支持maven,還支持Bower,Docker,Git,npm,NuGet等等。
2. 安裝安裝很簡單,直接解壓到指定目錄就可以,綠色版的,但是Windows用戶需注意目錄路徑不能含有中文,空格等字符。
3. 配置Nexus需要配置的地方不多,但是這個自帶jetty,默認端口8081,可能會有端口沖突,找到配置文件修改即可。 進入到nexus安裝目錄。
# 打開.\nexus-3.12.0-01-win64\nexus-3.12.0-01\etc\nexus-default.properties# Jetty 訪問端口,默認是8081application-port=8083# 本機地址application-host=0.0.0.01234564. 啟動啟動有兩種方法,一是直接啟動,二是安裝成服務。 進入安裝目錄
C:\Windows\system32>D:
D:\>cd D:\Soft\nexus-3.12.0-01-win64\nexus-3.12.0-01\bin1234直接啟動D:\Soft\nexus-3.12.0-01-win64\nexus-3.12.0-01\bin>nexus.exe /run1第一次啟動比較慢,等到打印出來下面的內容就啟動成功了。
-------------------------------------------------
Started Sonatype Nexus OSS 3.12.0-01
-------------------------------------------------12345這個啟動成功之后,直接按enter就可以停止服務。
安裝成服務D:\Soft\nexus-3.12.0-01-win64\nexus-3.12.0-01\bin>nexus.exe /install nexusInstalled service 'nexus'.123可以直接在服務里面啟動,也可以使用下面的命令啟動
D:\Soft\nexus-3.12.0-01-win64\nexus-3.12.0-01\bin>nexus.exe /start nexusStarting service 'nexus'.125. 使用打開 http://localhost:8083/ 點擊右上角Sign in,輸入賬號密碼,默認是 admin, admin123。
點擊齒輪狀按鈕,可進入配置頁面,進入Repository-Repositories:
Repository的type屬性有:proxy,hosted,group三種。
proxy:即你可以設置代理,設置了代理之后,在你的nexus中找不到的依賴就會去配置的代理的地址中找; hosted:你可以上傳你自己的項目到這里面; group:它可以包含前面兩個,是一個聚合體。一般用來給客戶一個訪問nexus的統一地址。
簡單的說,就是你可以上傳私有的項目到hosted,以及配置proxy以獲取第三方的依賴(比如可以配置中央倉庫的地址)。前面兩個都弄好了之后,在通過group聚合給客戶提供統一的訪問地址。
至於format,因為本文講的的 Maven Repository ,所以請選擇maven2;
你還可以添加一個國內的私有maven庫作為代理,避免中央庫無法連接,譬如阿里雲: http://maven.aliyun.com/nexus/content/groups/public/
系統默認就有以上幾個Repository。點擊maven-public 確保已經將 maven-central,maven-releases以及maven-snapshots都包含在里面,如果你自己有自定義庫,請確保也在里面。
maven-releases : 默認配置只能上傳 release版本的項目 maven-snapshots: 默認配置只能上傳 snapshots版本的項目 nuget開頭的庫: .net使用的庫,java不用管
如有特殊要求,可以自己創建一個Version policy 為Mixed的Repository。
以上配置就能滿足一般需求了。
修改用戶密碼和添加用戶 沒能找到用戶密碼的配置,但是在網頁是可以修改的,找到 Security > Users , 點擊 Create local user 添加新用戶,選擇用戶,點擊 More 可以選擇修改密碼,需要先輸入原密碼,驗證通過熟人新密碼。
6. 使用 mvn deploy 向 Nexus服務器 上傳項目首先: maven setting.xml配置<?xml version="1.0" encoding="UTF-8"?><settings> <localRepository>D:\Soft\apache\maven-repository</localRepository> <mirrors> <mirror> <!--This sends everything else to /public --> <id>nexus</id> <mirrorOf>*</mirrorOf> <url>http://localhost:8083/repository/maven-public/</url> </mirror> </mirrors> <servers> <server> <id>nexus</id> <username>admin</username> <password>zxm10@</password> </server> </servers></settings>12345678910111213141516171819localRepository:本地庫的地址
mirror:nexus地址
servers:nexus服務器登錄名和密碼,server可以配置多個,賦予不同權限,例如Release和Snapshot不同
1. 使用cmd上傳mvn deploy:deploy-file -DgroupId=com.cxx -DartifactId=fu -Dversion=1.0.0 -Dpackaging=jar -Dfile=%待上傳jar地址% -Durl=http://localhost:8081/repository/maven-releases/ -DrepositoryId=nexus -s %MAVEN_HOME%\conf\settings.xml1參數說明:
-D: 傳入指定參數 分別對應pom中的 groupId,artifactId,version,packaging file: 本地jar的路徑 url: Repository Url (請選擇對應release,snapshots或mixed的url) repositoryId: 對應setting.xml中server id -s: setting.xml的路徑(如果使用默認conf中的setting,則無需配置)
2. 使用IDE上傳項目中的pom文件添加
<distributionManagement> <repository> <id>nexus</id> <name>Nexus Release Repository</name> <url>http://localhost:8083/repository/maven-releases/</url> </repository> <snapshotRepository> <id>nexus</id> <name>Nexus Snapshot Repository</name> <url>http://localhost:8083/repository/maven-snapshots/</url> </snapshotRepository></distributionManagement>123456789101112id: 對應setting.xml中server id name: nexus Repository name url: nexus Repository url
需要說明的是,我們在Maven項目的pom.xml文件中 ,如果版本號中含有SNAPSHOT,則會自動發布到Snapshots倉庫中,例如:
<groupId>cn.xiweiai</groupId><artifactId>azkaban-app-deployer</artifactId><version>0.0.1-SNAPSHOT</version>123如果將version的值改為不帶SNAPSHOT后綴,則會發布到releases倉庫中,根據實際需要進行選擇。
然后使用IDE自帶的Maven deploy就可以了。
3. 使用網頁上傳選擇 方塊 > Upload > maven-releases 選擇文件,輸入對應信息,點擊上傳即可,上傳成功即可查看。
7. 默認存儲位置3.x的默認存儲變了,原以為下載到本地私服里面的jar包是以.jar格式存儲的,就和本地倉儲一樣,但是用everything搜了半天,沒有搜到,查了之后才發現,是以.bytes格式存儲的。 存儲位置在: 安裝目錄\sonatype-work\nexus3\blobs
.bytes格式是jar包,.properties格式是jar包信息。 里面存儲有下載時間,從哪個中心庫下載,下載用戶,用戶地址,jar包名稱,大小等信息。
在頁面查看此包:
8. Linux配置下載Nexus Repository Manager軟件包:
wget https://sonatype-download.global.ssl.fastly.net/repository/repositoryManager/3/nexus-3.12.0-01-unix.tar.gztar xvzf nexus-3.12.0-01-unix.tar.gz12解壓縮后可以看到,生成nexus-3.12.0-01和sonatype-work兩個目錄:
[root@ali-bj01-tst-cluster-004 nexus]# lsnexus-3.7.0-04 sonatype-work12是這兩個目錄在同一個目錄下,例如我這里是在nexus目錄下面。 可以在etc/nexus-default.properties配置文件中,修改對應的配置項,滿足實際需要,這里我直接使用默認的,其它可以使用的配置可以參考官網說明。 如果使用自定義的JDK,可以增加如下配置:
export JAVA_HOME=/usr/local/java/jdk1.8.0_144/1啟動Nexus Repository Manager,執行如下命令:
bin/nexus start————————————————版權聲明:本文為CSDN博主「飛飛1934」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。原文鏈接:https://blog.csdn.net/chenfei2341/article/details/80431700