Maven私服(遠程倉庫)搭建

1、下載maven私服工具

可以百度搜索 或 從百度網盤中下載

這裏已經將工具上傳到百度網盤,下載鏈接:

鏈接:https://pan.baidu.com/s/1eSh_TegWUFsEBlv92PWYiw 
提取碼:cobz 

下載 windows系統安裝包,即 nexus-2.12.0-01-bundle.zip 文件

2、安裝私服

首先,先將  nexus-2.12.0-01-bundle.zip 文件解壓到某個目錄(目錄不含空格)

解壓後發現有兩個目錄,分別是:nexus-2.12.0-01 和 sonatype-work

nexus-2.12.0-01 目錄:私服安裝及運行時需要用到相關文件

私服倉庫:在 sonatype-work\nexus 目錄下面,由於還沒有創建倉庫,所以該目錄下面沒有任何目錄或文件。

安裝私服:將 私服安裝到 windows 服務項中

使用 windows 命令行(以管理員身份運行),進入到 nexus-2.12.0-01\bin 目錄,執行 nexus.bat install 安裝私服

此時windows服務項中,可以找到服務名 爲 nexus 的服務。說明私服安裝成功。

3、啓動私服服務器

注意私服服務器端口占用問題:服務器默認使用的是 8081 端口。(在 nexus-2.12.0-01\conf\nexus.properties 文件中可以看到相關信息 )

在 windows 服務項中,找到服務名 爲 nexus 的服務,並啓動該服務。

點擊 “啓動” 服務。

或者服務項中,右鍵啓動。

私服服務器啓動成功:

4、訪問私服服務器(默認端口爲 8081)

打開電腦中的瀏覽器:

輸入地址 http://localhost:8081/nexus/  或者 http://192.168.1.106:8081/nexus/

瀏覽器自動跳轉到  http://localhost:8081/nexus/#welcome 或者 http://192.168.1.106:8081/nexus/#welcome

這裏以localhost地址爲例:

登錄服務器:默認賬號及密碼爲   admin / admin123

登錄成功:右上角會顯示登錄賬號信息、左側菜單可看到 Views/Repositories 選項。

5、查看倉庫

左側菜單可看到 Views/Repositories 選項, 點擊 Repositories 查看倉庫。

在私服安裝目錄 sonatype-work\nexus\storage 目錄下,可以找到對應的倉庫目錄

6、上傳 jar 包到私服

  • 配置maven環境,修改 settings 文件,配置連接私服的用戶名和密碼
<server>
 <id>releases</id>
 <username>admin</username>
 <password>admin123</password>
 </server>
<server>
 <id>snapshots</id>
 <username>admin</username>
 <password>admin123</password>
 </server>

releases 連接發佈版本項目倉庫
snapshots 連接測試版本項目倉庫

  • 配置 (工程或模塊的)pom.xml 文件

配置私服倉庫的地址,本公司的自己的 jar 包會上傳到私服的宿主倉庫,如果工程的版本爲 release 則上傳到私服的 release 倉庫,如果版本爲 snapshot 則上傳到私服的 snapshot 倉庫,這裏演示配置成localhost即可,如果是是公司中安裝的私服地址應該是localhost替換成私服ip地址

在工程或模塊的 pom.xml 文件中,添加如下代碼:

<distributionManagement>
 <repository>
 <id>releases</id>
 <url>http://localhost:8081/nexus/content/repositories/releases/</url>
 </repository>
 <snapshotRepository>
 <id>snapshots</id>
 <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
 </snapshotRepository>
</distributionManagement>
注意:pom.xml 這裏<id> 和 settings.xml 配置 <id> 對應!
  • 測試
以mall項目爲例(包含了父工程 mall_parent、子模塊有mall_dao、mall_service、mall_web), 將mall_dao 模塊打成 jar 包發佈到私服:
 
 
確定私服服務器已經啓動,然後對子模塊 mall_dao 執行 deploy 命令
 
執行 deploy 命令後,mall_dao將安裝 jar 包到本地倉庫,以及上傳 jar  包到私服(遠程倉庫),查看 nexus 的 snapshot倉庫,如果 version定義爲 release則項目將發佈到 nexus的 release 倉庫,本項目將發佈到 snapshot 倉庫:
 
 
 
在網頁中瀏覽器私服(遠程倉庫):
 
 
 
7、從私服(遠程倉庫)下載 jar 包
 
沒有使用私服之前,如果本地倉庫沒有,去中央倉庫下載,通常在企業中會在局域網
內部署一臺私服服務器,有了私服本地項目首先去本地倉庫找 jar,如果沒有找到則連接私
服從私服下載 jar 包,如果私服沒有 jar 包私服同時作爲代理服務器從中央倉庫下載 jar 包,
這樣做的好處是一方面由私服對公司項目的依賴 jar 包統一管理,一方面提高下載速度,項
目連接私服下載 jar 包的速度要比項目連接中央倉庫的速度快的多。
 
例子: 測試從私服下載 mall_dao 工程 jar 包。
  • 配置maven環境,修改 settings 文件,配置配置私服的倉庫
在客戶端的 setting.xml 中配置私服的倉庫,由於 setting.xml 中沒有 repositories 的配置
標籤需要使用 profile 定義倉庫。
<profile> 
 <!--profile 的 id-->
 <id>dev</id> 
 <repositories> 
     <repository> 
     <!--倉庫 id,repositories 可以配置多個倉庫,保證 id 不重複-->
     <id>nexus</id> 
     <!--倉庫地址,即 nexus 倉庫組的地址-->
     <url>http://localhost:8081/nexus/content/groups/public/</url> 
     <!--是否下載 releases 構件-->
     <releases> 
         <enabled>true</enabled> 
     </releases> 
     <!--是否下載 snapshots 構件-->
     <snapshots> 
         <enabled>true</enabled> 
     </snapshots> 
     </repository> 
 </repositories> 
 <pluginRepositories> 
     <!-- 插件倉庫,maven 的運行依賴插件,也需要從私服下載插件 -->
     <pluginRepository> 
         <!-- 插件倉庫的 id 不允許重複,如果重複後邊配置會覆蓋前邊 -->
         <id>public</id> 
         <name>Public Repositories</name> 
         <url>http://localhost:8081/nexus/content/groups/public/</url> 
     </pluginRepository> 
 </pluginRepositories> 
 </profile>
使用 profile 定義倉庫需要激活纔可生效。
 
<activeProfiles>
    <activeProfile>dev</activeProfile>
</activeProfiles>

配置成功後通過 idea 查看有效 pom (effective pom),有效 pom 是 maven 軟件最終使用的 pom 內容,程
序員不直接編輯有效 pom,打開有效 pom
 
有效 pom 內容如下:
下邊的 pom 內容中有兩個倉庫地址,maven 會先從前邊的倉庫的找,如果找不到 jar 包再從
下邊的找,從而就實現了從私服下載 jar 包。
 
 
<repositories>
 <repository>
 <releases>
 <enabled>true</enabled>
 </releases>
 <snapshots>
 <enabled>true</enabled>
 </snapshots>
 <id>public</id>
 <name>Public Repositories</name>
 <url>http://localhost:8081/nexus/content/groups/public/</url>
 </repository>
 <repository>
 <snapshots>
 <enabled>false</enabled>
 </snapshots>
 <id>central</id>
 <name>Central Repository</name>
 <url>https://repo.maven.apache.org/maven2</url>
 </repository>
 </repositories>
 <pluginRepositories>
 <pluginRepository>
 <id>public</id>
 <name>Public Repositories</name>
 <url>http://localhost:8081/nexus/content/groups/public/</url>
 </pluginRepository>
 <pluginRepository>
 <releases>
 <updatePolicy>never</updatePolicy>
 </releases>
 <snapshots>
 <enabled>false</enabled>
 </snapshots>
 <id>central</id>
 <name>Central Repository</name>
 <url>https://repo.maven.apache.org/maven2</url>
 </pluginRepository>
 </pluginRepositories>
  • 測試從私服下載 jar 包
測試 1:局域網環境或本地網絡即可
在 mall_service 工程中添加 mall_dao 工程的依賴以及配置私服下載信息,刪除本地倉庫中 mall_dao jar包,同時在 idea 中關閉 mall_dao 工程。
 
 
 
 mall_service 模塊重新編譯,執行 compile 命令
 
 
項目先從本地倉庫找 mall_dao,找不到從私服找,由於之前執行 deploy 將 mall_dao 部署到
私服中,所以成功從私服下載 mall_dao 並在本地倉庫保存一份。
 
 
 
 
 
 
 
 
 
 
 
 
 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章