1. 前言
Maven
倉庫管理也叫 Maven
私服或者代理倉庫。使用 Maven
私服有兩個目的:
- 私服是一個介於開發者和遠程倉庫之間的代理;
- 私服可以用來部署公司自己的
jar
;
2. Nexus
介紹
Nexus
是一個強大的 Maven
倉庫管理工具,使用 Nexus
可以方便的管理內部倉庫同時簡化外部倉庫的訪問。
2.1 Nexus
安裝
- 下載
下載地址:www.sonatype.com/download-os…
- 解壓
將下載下來的壓縮包,拷貝到一個沒有中文的路徑下,然后解壓。
- 啟動
解壓之后,打開 cmd
窗口(以管理員身份打開 cmd 窗口),然后定位了 nexus 解壓目錄,執行 nexus.exe/run
命令啟動服務。這個啟動稍微有點慢,大概有 1 兩分鍾的樣子。
啟動成功后,瀏覽器輸入 http://lcoalhost:8081
打開管理頁面。
打開管理頁面后,點擊右上角上的登錄按鈕進行登錄,默認的用戶名/密碼是 admin/admin123
。當然,用戶也可以點擊設置按鈕,手動配置其他用戶。
點擊 Repositories 可以查看倉庫詳細信息:
3. 倉庫類型
名稱 | 說明 |
---|---|
proxy | 表示這個倉庫是一個遠程倉庫的代理,最典型的就是代理 Maven 中央倉庫 |
hosted | 宿主倉庫,公司自己開發的一些 jar 存放在宿主倉庫中,以及一些在 Maven 中央倉庫上沒有的 jar |
group | 倉庫組,包含代理倉庫和宿主倉庫 |
virtual | 虛擬倉庫 |
4. 上傳 jar
上傳 jar,配置兩個地方:
Maven 的 conf/settings.xml
文件配置:
<server> <id>releases</id> <username>admin</username> <password>admin123</password> </server> <server> <id>snapshots</id> <username>admin</username> <password>admin123</password> </server>
在要上傳 jar 的項目的 pom.xml
文件中,配置上傳路徑:
<distributionManagement> <repository> <id>releases</id> <url>http://localhost:8081/repository/maven-releases/</url> </repository> <snapshotRepository> <id>snapshots</id> <url>http://localhost:8081/repository/maven-snapshots/</url> </snapshotRepository> </distributionManagement>
配置完成后,點擊 deploy
按鈕,或者執行 mvn deploy
命令就可以將 jar
上傳到私服上。
5. 跳過某個module
I don’t want to deploy one of the artifacts in my multi-module build. Can I skip deployment?
Yes, you can skip deployment of individual modules by configuring the Deploy Plugin as follows:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> <configuration> <skip>true</skip> </configuration> </plugin>
參考:http://maven.apache.org/plugins/maven-deploy-plugin/faq.html
6. 下載私服上的 jar
直接在項目中添加依賴,添加完成后,額外增加私服地址即可:
<repositories> <repository> <id>local-repository</id> <url>http://localhost:8081/repository/maven-public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories>