系列文章
1、SSM基礎專項復習1——SSM項目整合-CSDN博客
2、SSM基礎專項復習2——Spring 框架(1)-CSDN博客
3、SSM基礎專項復習3——Spring框架(2)-CSDN博客
4、SSM基礎專項復習4——Maven項目管理工具(1)-CSDN博客
文章目錄
系列文章
1、maven 私服介紹
1.1 私服介紹
1.2. Nexus 介紹
2、maven 私服實戰
2.1 nexus 安裝
2.2 nexus 倉庫類型
2.3 將項目發布到私服
2.4. 從私服下載 jar 包
1、maven 私服介紹
1.1 私服介紹
????????正式開發,不同的項目組開發不同的工程。maven-dao 工程開發完畢,發布到私服maven-service 從私服下載 dao。
????????公司在自己的局域網內搭建自己的遠程倉庫服務器,稱為私服,私服服務器即是公司內部的 maven 遠程倉庫, 每個員工的電腦上安裝 maven 軟件并且連接私服服務器,員工將自己開發的項目打成 jar 并發布到私服服務器,其它項目組從私服服務器下載所依賴的構件(jar)。
私服還充當一個代理服務器,當私服上沒有 jar 包會從互聯網中央倉庫自動下載。
1.2. Nexus 介紹
Nexus 是 Maven 倉庫管理器,通過 nexus 可以搭建 maven 倉庫,同時nexus 還提供強大的倉庫管理功能,構件搜索功能等。?
2、maven 私服實戰
2.1 nexus 安裝
這個是nexus官網
Sonatype Nexus Repository Manager Community Edition | Downloadhttps://www.sonatype.com/download-nexus-repo-oss?submissionGuid=e225567e-4323-4049-870c-478afc2a7bfe
解壓 nexus-2.12.0-01-bundle.zip 文件,存放到一個不含中文的目錄下。查看 conf 文件下的 nexus.properties 配置文件,可以修改對應的配置:
nexus 的 安 裝 命 令 : 使 用 管 理 員 運 行 cmd 命令窗口,切換目錄nexus\nexus-2.12.0-01\bin 目錄下,執行 nexus.bat install 進行安裝。執行nexus.bat start 啟動服務 執行 nexus.bat stop 停止服務。?
nexus 的 卸 載 命 令 : 使 用 管 理 員 運 行 cmd 命 令窗口,切換目錄nexus\nexus-2.12.0-01\bin 目錄下,執行 nexus.bat uninstall 進行卸載
訪問圖形化界面:打開瀏覽器,輸入http://localhost:端口號/nexus 訪問
點擊 log in,進行登錄。用戶名:admin 密碼:admin123
2.2 nexus 倉庫類型
nexus 提供了不同的倉庫類型
- hosted,宿主倉庫, 部署自己的 jar 到這個類型的倉庫,包括releases 和snapshot 兩部分, Releases 公司內部發布版本倉庫、 Snapshots 公司內部測試版本倉庫
- proxy,代理倉庫, 用于代理遠程的公共倉庫,如 maven 中央倉庫,用戶連接私服,私服自動去中央倉庫下載 jar 包或者插件。
- group,倉庫組,用來合并多個 hosted/proxy 倉庫,通常我們配置自己的maven連接倉庫組。Group 倉庫組也是可以自己進行定制的。
- virtual(虛擬),兼容 Maven1 版本的 jar 或者插件
2.3 將項目發布到私服
????????需要在客戶端即部署要部署的工程電腦上配置 maven 環境,并修改settings.xml 文件, 配置連接私服的用戶和密碼 。
????????此用戶名和密碼用于私服校驗,因為私服需要知道上傳的賬號和密碼是否和私服中的賬號和密碼一致。
????????在 servers 節點下進行配置
<!-- 定義穩定版本的 id 名稱,用戶名密碼 -->
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<!-- 定義開發版本的 id 名稱,用戶名密碼 -->
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
????????配置項目 pom.xml,配置私服倉庫的地址,本公司的自己的 jar 包會上傳到私服的宿主倉庫,根據工程的版本號決定上傳到哪個宿主倉庫,如果版本為release 則上傳到私服的 release 倉庫,如果版本為 snapshot 則上傳到私服的 snapshot 倉庫。
<distributionManagement>
<repository>
<id>releases</id>
<url>http://localhost:8079/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://localhost:8079/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
注意:這里的 id 標簽的值要和 settings.xml 配置文件中的id 值保持一致。
在該工程中執行 deploy 命令,發布項目到私服上。
查看私服結果
?
可以發布 RELEASES 穩定版本的 jar 包到私服。
2.4. 從私服下載 jar 包
在 settings.xml 配置文件配置私服的鏡像文件
<mirror>
<!-- id 名稱 -->
<id>nexusmaven</id>
<!-- 表示攔截所有的請求,都重定向到私服,從私服下載jar 包,私服沒有再去中央倉庫下載 -->
<mirrorOf>*</mirrorOf>
<name>nexus maven</name>
<!-- 私服的組地址 -->
<url>http://localhost:8079/nexus/content/groups/public/</url>
</mirror>
進行測試
先把自己的某個項目發布到私服中,然后刪除掉本地倉庫中的jar 包,再使用其他項目去依賴該 jar 包,查看是否從私服中下載。
在其他項目中引入該坐標依賴。
<!--依賴 demo1 這個項目-->
<dependencies>
<dependency>
<groupId>cn.tx.maven</groupId>
<artifactId>txmaven_demo10413</artifactId>
<version>1.0-RELEASES</version>
</dependency>
</dependencies>
還有一種方式
可以配置倉庫的方式,可以修改自己項目的 pom 配置文件,添加倉庫的配置。
<repositories>
<repository>
<id>nexus</id>
<name>nexusmaven</name>
<url>http://localhost:8079/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>public</id>
<url>http://localhost:8079/nexus/content/groups/public/</url>
<name>pluginRepositories</name>
</pluginRepository>
</pluginRepositories>
上面的方式不是特別的理想,需要在每一個項目的 pom 文件中都添加相同的配置,比較麻煩。
可以在 settings.xml 配置文件中添加配置,完成統一的設置。
<!-- 下載 jar 包配置 -->
<profile>
<!--profile 的 id -->
<id>dev</id>
<repositories>
<repository> <!--倉庫 id,repositories 可以配置多個倉庫,保證 id 不重復 -->
<id>nexus</id> <!--倉庫地址,即 nexus 倉庫組的地址--><url>http://localhost:8079/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:8079/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
激活配置
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
進行測試