場景
Ubuntu Server 上使用Docker Compose 部署Nexus(圖文教程):
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/101111611
在上面已經實作部署Nexus后的效果是

為什么要搭建私服
有時合作開發時,為了不泄露原始碼但是還能允許你呼叫,或者公司內部自己的依賴jar包,只能在本公司內用,并且再官方中央倉庫中沒有,類似情況下都需要搭建Maven私服,
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程式猿
獲取編程相關電子書、教程推送與免費下載,
實作
Deploy依賴到私服
配置認證資訊
找到Maven的安裝目錄

conf下的setting.xml中找到server節點,

配置認證節點,因為私服不是誰都能使用,所以需要配置用戶名和密碼,這里的密碼是上面搭建Nexus服務時所設定的密碼,
<server> <id>nexus-releases</id> <username>admin</username> <password>admin123</password> </server> <server> <id>nexus-snapshots</id> <username>admin</username> <password>admin123</password> </server>
修改之后,保存,

注:
nexues-releases:用于發布Release版本
nexus-snapshots:用于發布Snapshot版本(快照版),快照版會自動加一個時間作為標識,
配置自動化部署
在專案的pom.xml中加入如下代碼:
<distributionManagement> <repository> <id>nexus-releases</id> <name>Nexus Release Repository</name> <url>http://192.168.208.134:8081/repository/maven-releases/</url> </repository> <snapshotRepository> <id>nexus-snapshots</id> <name>Nexus Snapshot Repository</name> <url>http://192.168.208.134:8081/repository/maven-snapshots/</url> </snapshotRepository> </distributionManagement>
這里是使用IDEA新建的maven專案
注:
1.ID名稱要與settings.xml中Servers配置的ID保持一致,
2.專案版本號中有SNAPSHOT標識的,會發布到Nexus Snapshots Respository,否則發布到Nexus Release Repository,并根據ID去匹配授權賬號,
3.這里的url是Nexus服務上的url,

部署
打開IDEA下的Ternial,輸入:
mvn deploy

可以看到其部署效果

此時重繪Nexus服務的url,找到Browse下的maven-snapshots

部署成功,
然后打開IDEA--settings-maven,然后勾選上總是更新快照,

這樣就能用到最新的快照版本,
上傳第三方jar包
有時在官方倉庫沒有的jar包,需要上傳到私服上,供大家使用,
mvn deploy:deploy-file -DgroupId=com.google.code.kaptcha -DartifactId=kaptcha -Dversion=2.3.2 -Dpackaging=jar -Dfile=C:\Users\Administrator\Desktop\kaptcha-2.3.2.jar -Durl=http://192.168.208.134:8081/repository/maven-releases/ -DrepositoryId=nexus-releases
命令解釋:
-DgroupId= 自定義
-DartifactId= 自定義
-Dversion= 自定義 三個自定義,構成pom.xml檔案中的坐標
-Dpackaging=jar 上傳的型別是jar型別
-Dfile= jar的本地磁盤位置
-Durl= hosted資源庫的地址
-DrepositoryId=nexus-releases setting.xml檔案中配置的ID
上傳成功效果

此時再回到瀏覽器,重繪,

在專案中使用私服jar包
配置代理倉庫
在需要從私服中下載jar包的專案的pom.xml中加入如下配置:
<repositories> <repository> <id>nexus</id> <name>Nexus Repository</name> <url>http://192.168.208.134:8081/repository/maven-public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <!-- 私服倉庫配置:從私服下載--> <pluginRepositories> <pluginRepository> <id>nexus</id> <name>Nexus Plugin Repository</name> <url>http://192.168.208.134:8081/repository/maven-public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories>
為什么是從public進行下載,
因為公共倉庫是發行倉庫和快照倉庫的映射,把兩個倉庫結合起來,
下面這段代碼
<releases> <enabled>true</enabled></releases><snapshots> <enabled>true</enabled></snapshots>
作用是配置是否依賴發行版和是否依賴快照版,
怎樣使用私服jar包,
找到要下載的jar包的坐標配置,加入到pom中,那么就會先從私服去找對應的jar包,然后再去官服去找jar包,

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/48389.html
標籤:架構設計
