公司在自己的局域網內搭建自己的遠程倉庫服務器,稱爲私服(私有服務器),私服服務器即是公司內部的maven遠程倉庫,每個員工的電腦上安裝maven軟件並且連接私服服務器,員工將自己開發的項目打成jar併發布到私服服務器,其它項目組從私服服務器下載所依賴的構件(jar)。
Maven私服作用:
1、我們不能聯網的情況下,它幫我們從互聯網下載jar(私服中jar的來源1)
2、自己開發的jar上傳到私服供(共享給)團隊使用(私服中jar的來源2)
3、最終私服就是讓我們下載jar的
私服還充當一個代理服務器,當私服上沒有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