maven私有鏡像倉庫nexus部署使用
1、Nexus部署
#查找鏡像 docker search sonatype/nexus3 #拉取鏡像 docker pull sonatype/nexus3 #持久化目錄 mkdir -p /data/nexus/data chmod 777 -R /data/nexus/data #啟動服務(wù) docker run -d --name nexus3 -p 8081:8081 --restart always -v /data/nexus/data:/nexus-data sonatype/nexus3 #查看日志 docker logs -f nexus3
稍等一下,出現(xiàn) Started Sonatype Nexus OSS 表示啟動好了。
2、Nexus訪問
安裝完成后可訪問管理平臺:http://ip:8081,打開瀏覽器,訪問 http://ip:8081/
admin賬戶登錄密碼查看
cat /data/nexus/data/admin.password
3、Nexus配置
默認倉庫說明
maven-central:maven中央庫,默認從https://repo1.maven.org/maven2/拉取jar
maven-releases:私庫發(fā)行版jar,初次安裝請將Deployment policy設(shè)置為Allow redeploy
maven-snapshots:私庫快照(調(diào)試版本)jar
maven-public:倉庫分組,把上面三個倉庫組合在一起對外提供服務(wù),在本地maven基礎(chǔ)配置settings.xml或項目pom.xml中使用
Nexus倉庫類型介紹
hosted:本地倉庫,通常我們會部署自己的構(gòu)件到這一類型的倉庫。比如公司的第二方庫。
proxy:代理倉庫,它們被用來代理遠程的公共倉庫,如maven中央倉庫。
group:倉庫組,用來合并多個hosted/proxy倉庫,當(dāng)你的項目希望在多個repository使用資源時就不需要多次引用了,只需要引用一個group即可。
4、創(chuàng)建Blob Stores
在創(chuàng)建repository之前,還需要先指定文件存儲目錄,便于統(tǒng)一管理。就需要創(chuàng)建Blob Stores,不創(chuàng)建則使用的是default
可以看到blob stores有兩個,一個是系統(tǒng)默認的,一個是剛創(chuàng)建的。如果不想自己創(chuàng)建,使用系統(tǒng)默認的文件存儲目錄也是可以的。到時候創(chuàng)建repository時,存儲目錄選擇default就可以了。新創(chuàng)建的目錄,可以在/data/blobs/目錄下面可以看到。
5、Nexus倉庫
如圖所示,代理倉庫負責(zé)代理遠程中央倉庫,托管倉庫負責(zé)本地資源,組資源庫 = 代理資源庫 + 托管資源庫
6、創(chuàng)建proxy repository代理倉庫
選擇maven2(proxy),代理倉庫
設(shè)置代理倉庫
其他的可以采用默認,以后需要修改的可以再修改。
這里推薦幾個遠程倉庫:
jboss的maven中央倉庫地址:http://repository.jboss.com/maven2/
阿里云的maven中央倉庫地址:http://maven.aliyun.com/nexus/content/groups/public/
apache的maven中央倉庫地址:http://repo.maven.apache.org/maven2/
7、創(chuàng)建hosted repository倉庫
jboss的maven中央倉庫地址:http://repository.jboss.com/maven2/
阿里云的maven中央倉庫地址:http://maven.aliyun.com/nexus/content/groups/public/
apache的maven中央倉庫地址:http://repo.maven.apache.org/maven2/
上圖的Hosted設(shè)置選項,選項中有三個值:
Allow redeploy:允許同一個版本號下重復(fù)提交代碼, nexus以時間區(qū)分
Disable redeploy:不允許同一個版本號下重復(fù)提交代碼
Read-Only:不允許提交任何版本原生的maven-releases庫是Disable redeploy設(shè)置, maven-snapshots是Allow redeploy。
8、創(chuàng)建group repository組倉庫
9、maven部署配置
#獲取軟件包 wget https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.9.6/binaries/apache-maven-3.9.6-bin.tar.gz --no-check-certificate tar -zxvf apache-maven-3.9.6-bin.tar.gz echo "" >>/etc/profile source /etc/profile mvn -V
Maven下的setting.xml文件和項目中的pom.xml文件的關(guān)系是:settting.xml文件是全局設(shè)置,而pom.xml文件是局部設(shè)置。pom.xml文件對于項目來說,是優(yōu)先使用的。而pom.xml文件中如果沒有配置鏡像地址的話,就按照settting.xml中定義的地址去查找。
如上圖方式獲取組倉庫smart_group的倉庫地址,修改setting.xml文件如下:
<!--nexus服務(wù)器,id為組倉庫name--> <servers> <server> <id>custom_group</id> <username>admin</username> <password>admin123</password> </server> </servers> <!--倉庫組的url地址,id和name可以寫組倉庫name,mirrorOf的值設(shè)置為central--> <mirrors> <mirror> <id>custom_group</id> <name>custom_group</name> <url>http://192.168.124.189:8081/repository/custom_group/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors>
修改后可以重新編譯項目,必須添加參數(shù)-U,(-U,–update-snapshots,強制更新releases、snapshots類型的插件或依賴庫,否則maven一天只會更新一次snapshot依賴)。代理倉庫會從遠程中央倉庫下載jar包
mvn clean compile -U
新建SpringBoot項目,可以查看本地倉庫,遠程倉庫的下載的Jar包
這個時候可以看到代理倉庫已經(jīng)從中央倉庫下載了項目編譯需要的jar包。同樣地,在組倉庫中也能看到所有的jar包,包括代理倉庫和宿主倉庫的。
10、管理平臺上傳三方j(luò)ar包
有些jar是第三方提供的,在中央倉庫中是沒有的,我們可以上傳這些本地三方j(luò)ar包到hosted repository宿主倉庫中。
上傳成功后,就可以看到hosted repository和group repository中已經(jīng)有了剛上傳的三方j(luò)ar包
然后項目中通過編譯打包就可以下載jar包到本地repository中了,記住maven clean、compile、package首次執(zhí)行時加參數(shù)-U。
11、命令上傳三方j(luò)ar包
在setting.xml配置文件中添加hosted repository server
<!--id自定義,但是在使用命令上傳的時候會用到--> <server> <id>custom_hosted</id> <username>admin</username> <password>admin123</password> </server>
使用如下命令上傳jar包:
mvn deploy:deploy-file -DgroupId=byd.ghy -DartifactId=portal-auth -Dversion=1.0.0 -Dpackaging=jar -Dfile=D:\workspace_byd\workspace_ghy\workspace_portal-backend\portal-backend-jar-v2\target\auth-server.jar -Durl=http://192.168.124.189:8081/repository/custom_hosted/ -DrepositoryId=custom_hosted 命令解釋: -DgroupId=byd.ghy 自定義 -DartifactId=portal-auth 自定義 -Dversion=1.0.0 自定義,三個自定義,構(gòu)成pom.xml文件中的標識 -Dpackaging=jar 傳的類型是jar類型 -Dfile=D:\auth-server.jar jar包的本地磁盤位置 -Durl=http://ip:8081/repository/custom_hosted/ hosted資源庫的地址 -DrepositoryId=custom_hosted 需要和setting.xml文件中配置的ID一致 例如: mvn deploy:deploy-file -Dmaven.test.skip=true -Dfile=./cloudx-common-basics-2.0.2-SNAPSHOT.jar -DgroupId=com.risit.chk -DartifactId=risit-chk-common -Dversion=1.0.0-SNAPSHOT -Dpackaging=jar -DrepositoryId=nexus-snapshots -Durl=http://10.10.111.222:8081/repository/nexus-snapshots/ mvn deploy:deploy-file -Dfile=cloudx-center-2.0.2-SNAPSHOT.pom -DgroupId=com.cloudx -DartifactId=cloudx-center -Dversion=2.0.2-SNAPSHOT -Dpackaging=pom -DrepositoryId=nexus-snapshots -Durl=http://10.10.111.222:8081/repository/nexus-snapshots/
上傳成功后,hosted repository中已經(jīng)可以看到了
12、deploy部署jar包到私服
release和snapshots jar包區(qū)別
SNAPSHOT版本代表不穩(wěn)定(快照版本),還在處于開發(fā)階段,隨時都會有變化。當(dāng)上傳同樣的版本號jar包的時候,SNAPSHOT會在版本號的后面自動追加一串新的數(shù)字,即日志標簽,nexus會根據(jù)日志標簽區(qū)分出不同的版本,在maven引用時,如果使用的是snapshot版本,重新導(dǎo)入maven的時候,會去私庫拉取最新上傳的代碼。
RELEASE則代表穩(wěn)定的版本(發(fā)布版本),一般上線后都會改用RELEASE版本。
也就是說1.0,2.0這樣的版本只能有一個,也就是說當(dāng)前版本號下,不可能出現(xiàn)不同的jar。
創(chuàng)建snapshot倉庫
可以在nexus上添加一個snapshot倉庫,專門用于存放snapshot版本的jar包。snapshot倉庫也是hosted類型的,創(chuàng)建方式和hosted repository類型。創(chuàng)建好后,加入到組倉庫里面就可以了。
一定要選擇Deployment Policy為allow redeploy
同樣的操作,創(chuàng)建Release類型的倉庫,并加入到組中
13、項目pom.xml文件配置
可以在項目的pom文件中設(shè)置具體的relaeses庫和snapshots庫。如果當(dāng)前項目的版本號的后綴名中帶著-SNAPSHOT,類似***-SNAPSHOT,就會將項目jar包提交到snapshots庫中,沒有帶-SNAPSHOT的話會提交releases庫中。
在pom.xml文件中配置 distributionManagement 節(jié)點如下,在項目中執(zhí)行deploy命令后,jar包將會被上傳到nexus中。
setting.xml中配置
<server> <id>custom_snapshots</id> <username>admin</username> <password>admin123</password> </server> <server> <id>custom_releases</id> <username>admin</username> <password>admin123</password> </server>
pom.xml配置
<repositories> <repository> <id>custom_group</id> <name>Nexus Repository</name> <url>http://192.168.124.189:8081/repository/custom_group/</url> <snapshots> <enabled>true</enabled> </snapshots> <releases> <enabled>true</enabled> </releases> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>custom_group</id> <name>Nexus Plugin Repository</name> <url>http://192.168.124.189:8081/repository/custom_group/</url> <snapshots> <enabled>true</enabled> </snapshots> <releases> <enabled>true</enabled> </releases> </pluginRepository> </pluginRepositories> <!--項目分發(fā)信息,在執(zhí)行mvn deploy后表示要發(fā)布的位置。有了這些信息就可以把網(wǎng)站部署到遠程服務(wù)器或者把構(gòu)件jar等部署到遠程倉庫。 --> <distributionManagement> <repository><!--部署項目產(chǎn)生的構(gòu)件到遠程倉庫需要的信息 --> <id>custom_releases</id><!-- 此處id和settings.xml的id保持一致 --> <name>Nexus Release Repository</name> <url>http://192.168.124.189:8081/repository/custom_releases/</url> </repository> <snapshotRepository><!--構(gòu)件的快照部署到哪里?如果沒有配置該元素,默認部署到repository元素配置的倉庫,參見distributionManagement/repository元素 --> <id>custom_snapshots</id><!-- 此處id和settings.xml的id保持一致 --> <name>Nexus Snapshot Repository</name> <url>http://192.168.124.189:8081/repository/custom_snapshots/</url> </snapshotRepository> </distributionManagement>
默認地,maven編譯打包不會下載SNAPSHOT版本的jar包,所以還需要在pom.xml文件中配置支持下載snapshot版本jar包。
mvn clean deploy -Dmaven.test.skip=true
至此,nexus搭建完畢,支持本地部署依賴jar包!??!
14、問題排查
1、nexus.log日志中報錯內(nèi)容如下:
UNKNOWN com.sonatype.nexus.plugins.outreach.internal.outreach.SonatypeOutreach - Could not download page bundle
java.net.SocketException: Network is unreachable (connect failed)
解決方法:
a) 訪問Nexus管理站點,我這里是 http://192.168.124.184:8081,進入后,用管理員帳號登錄。
b)點擊 下圖中上面的按鈕,然后選擇左側(cè)Capabilities,然后點擊右側(cè)的 Outreach:Management
c)點擊【disable】按鈕,關(guān)閉Outreach服務(wù)
或者給Outreach設(shè)定新的URL,可以在標簽頁 Settings 中的 Override Outreach Content URL項中設(shè)定。Nexus支持的URL有如下三個:
- http://links.sonatype.com and https://links.sonatype.com
- http://download.sonatype.com and https://download.sonatype.com
- http://sonatype-download.global.ssl.fastly.net and https://sonatype-download.global.ssl.fastly.net
到此這篇關(guān)于maven私有鏡像倉庫nexus部署使用的文章就介紹到這了,更多相關(guān)maven nexus部署內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Java打印數(shù)字組成的魔方陣及字符組成的鉆石圖形
這篇文章主要介紹了使用Java打印數(shù)字組成的魔方陣及字符組成的鉆石圖形,可作為一些CLI程序界面的基礎(chǔ)部分,需要的朋友可以參考下2016-03-03Rabbitmq延遲隊列實現(xiàn)定時任務(wù)的方法
這篇文章主要介紹了Rabbitmq延遲隊列實現(xiàn)定時任務(wù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05Java基礎(chǔ)之多線程方法狀態(tài)和創(chuàng)建方法
Java中可以通過Thread類和Runnable接口來創(chuàng)建多個線程,下面這篇文章主要給大家介紹了關(guān)于Java基礎(chǔ)之多線程方法狀態(tài)和創(chuàng)建方法的相關(guān)資料,需要的朋友可以參考下2021-09-09