公司在自己的區域網路内搭建自己的遠端倉庫伺服器,稱為私服(私有伺服器),私服伺服器即是公司内部的maven遠端倉庫,每個員工的電腦上安裝maven軟體并且連接配接私服伺服器,員工将自己開發的項目打成jar并釋出到私服伺服器,其它項目組從私服伺服器下載下傳所依賴的構件(jar)。
Maven私服作用:
1、我們不能聯網的情況下,它幫我們從網際網路下載下傳jar(私服中jar的來源1)
2、自己開發的jar上傳到私服供(共享給)團隊使用(私服中jar的來源2)
3、最終私服就是讓我們下載下傳jar的
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHL0cmeNBzaU1kMNpHW4Z0MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL5QTMyQDM0cTM2IjNwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
私服還充當一個代理伺服器,當私服上沒有jar包會從網際網路中央倉庫自動下載下傳。
Maven伺服器:某一個機器上安裝了可以提供Maven私服功能的軟體,那麼這個機器就叫做Maven私有伺服器。
(一)搭建私服環境
nexus是Maven倉庫管理器(maven私服軟體),通過nexus可以搭建maven私服倉庫,同時nexus還提供強大的倉庫管理功能等。
l 安裝nexus
n 解壓nexus-2.12.0-01-bundle.zip(無中文和空格路徑下),進入bin目錄
n 管理者身份運作cmd,進入bin目錄,執行nexus.bat install(若安裝後解除安裝可執行nexus.bat uninstall)
n services.msc進入服務管理,檢視nexus服務
n 若nexus服務已經安裝,可以使用兩種方式啟動nexus
u bin目錄下執行nexus.bat start
u 直接啟動nexus服務
n 檢視nexus的配置檔案conf/nexus.properties
n 通路http://localhost:8081/nexus/ 内置登入名:admin/admin123
n 倉庫類型
nexus的倉庫有4種類型:
- hosted,宿主倉庫,部署自己的jar到這個類型的倉庫,包括releases和snapshot兩部分,Releases公司内部釋出版本倉庫、 Snapshots 公司内部測試版本倉庫
- proxy,代理倉庫,用于代理遠端的公共倉庫,如maven中央倉庫,使用者連接配接私服,私服自動去中央倉庫下載下傳jar包或者插件。
- group,倉庫組,用來合并多個hosted/proxy倉庫,通常我們配置自己的maven連接配接倉庫組。
- virtual(虛拟):相容Maven1 版本的jar或者插件
接下來三件事:
1、 把我們的項目釋出到私服;
2、 從私服上下載下傳jar;
3、 如何向私服上上傳第三方的jar
(二)項目釋出到nexus私服
對遠端的一個操作:url、使用者名、密碼都得有,後續上傳就是圍繞這三個資訊進行;
第一步:maven軟體settings.xml中配置連接配接私服的使用者名和密碼(放在servers标簽内)
<!--配置使用者名密碼開始-->
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
<!--配置使用者名密碼結束-->
第二步:配置項目的pom.xml
<!--添加到要上傳的項目pom中确定上傳路徑開始-->
<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中确定上傳路徑結束-->
注意:pom.xml這裡 和 settings.xml 配置 對應
第三步:執行mvn deploy指令
(三)從nexus私服下載下傳jar
在maven的settings.xml檔案中配置下載下傳模闆(放在profiles标簽内)
<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>
激活下載下傳模闆配置
<!--激活模闆開始-->
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
<!--激活模闆結束-->
測試:删除本地的dao工程,終端進入service工程下執行mvn compile
(四)把第三方jar放入本地倉庫或者私服
第三方jar放入本地倉庫
mvn install:install-file -Dfile=ojdbc14-10.2.0.4.0.jar -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.4.0 -Dpackaging=jar
第三方jar放入私服
maven的settings配置檔案中配置第三方倉庫的server資訊
<server>
<id>thirdparty</id>
<username>admin</username>
<password>admin123</password>
</server>
執行指令
mvn deploy:deploy-file -Dfile=ojdbc14-10.2.0.4.0.jar -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.4.0 -Dpackaging=jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty