目錄
- 一.參數配置
- 二.倉庫配置
- 建立 Blob Strores
- 建立 Proxy Repository 代理倉庫
- 建立 hosted repository 宿主倉庫
- 建立 group repository 倉庫組
- 三.讓 Maven 項目使用 Nexus 私服
- 下載下傳Jar:設定 Nexus 為鏡像位址
- 部署上傳Jar:配置distributionManagement
如果你想配置 nexus 的應用在本地啟動的 JVM參數,可以在 nexus.vmoptions:

如果你想改變 nexus 的 端口号,可以在 nexus-default.properties:
在建立 repository之前,還是需要先設定一個指定的檔案存儲目錄,便于統一管理。在建立 repository之前,還是需要先設定一個指定的檔案存儲目錄,便于統一管理。
前篇文章也提到,nexus3 使用了Blob Stores來儲存倉庫檔案。是以建立倉庫前,可以先建立 Blob Stores。當然你也可以不建立,那麼将會使用預設的路徑:./sonatype-work/nexus3/blobs
配置 Proxy Repository代理倉庫。建立頁面出來之後,頁面上有三個框是必填的:
注意:可以多建幾個proxy repository,以便于可以加入更多的遠端maven倉庫到我們的 group 倉庫中。唯一一點,就是看加入的proxy資源庫使用的是國内的還是國外的遠端倉庫。加入group資源庫的順序應是:hosted倉庫 > 國内proxy倉庫 > 國外proxy倉庫。
這裡推薦幾個遠端倉庫:
- jboss的maven中央倉庫位址:http://repository.jboss.com/maven2/
- 阿裡雲的maven中央倉庫位址:http://maven.aliyun.com/nexus/content/groups/public/
- apache的maven中央倉庫位址:http://repo.maven.apache.org/maven2/
Hosted有三種方式:Releases、Snapshot、Mixed
- Releases: 一般是已經釋出的Jar包
- Snapshot: 未釋出的版本
- Mixed:混合的
為什麼要注意順序??
官方文檔中建議:
It is recommended practice to place hosted repositories higher in the list than proxy repositories. For proxy repositories, the repository manager needs to check the remote repository which will incur more overhead than a hosted repository lookup.
希望将hosted repositories【宿主倉庫】的順序放在proxy repositories【代理倉庫】之前,因為一個group【倉庫組】中可以涵括這些宿主倉庫和代理倉庫。而一整個的group是作為一個public,一個接口給别人使用的。
是以當查找架包的時候,如果代理資源庫在前面,那就是先從遠端去查找jar,而不是先從宿主倉庫(本地倉庫)去查找是否有jar。
Maven 下的setting.xml檔案和你自己項目中的pom.xml檔案的關系:
- settting.xml 檔案就是本地電腦上的全局變量;而pom.xml檔案就是局部變量。
- pom.xml檔案對于項目來說,是優先使用的。然而pom.xml檔案中如果沒有指定jar要從哪裡下載下傳的話,也就是說沒有配置鏡像位址的話,當然隻能去按照settting.xml 中定義的位址去找了
拷貝的這個位址,就是下面即将在setting.xml檔案中添加時使用的:
setting.xml:
<!--自定義maven本地倉庫位址-->
<localRepository>D:\apps\repository</localRepository>
<!--nexus伺服器-->
<servers>
<server>
<id>nexus</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
<!--倉庫組的url位址 id和name自定義,mirrorOf的值設定為central,寫死的-->
<mirrors>
<mirror>
<id>nexus</id>
<name>nexus repository</name>
<url>http://localhost:8081/repository/myself_group/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
配置 distributionManagement 節點,執行 deploy 指令時,會将jar釋出到指定位址。
注意:配置20180929-SNAPSHOT時,如果後面添加了SNAPSHOT,則會自動釋出到指定的snapshots倉庫,否則釋出到releases倉庫。
pom.xml:
<project>
...
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
...
</project>
settings.xml:
<settings>
...
<servers>
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
...
</settings>
兩個檔案的 id 需要保持一緻!
本文版權歸作者所有,歡迎轉載,請務必添加原文連結。