介紹
Nexus 是Maven倉庫管理器,如果你使用Maven,你可以從Maven中央倉庫 下載所需要的構件(artifact),但這通常不是一個好的做法,你應該在本地架設一個Maven倉庫服務器,在代理遠程倉庫的同時維護本地倉庫,以節省帶寬和時間,Nexus就可以滿足這樣的需要。此外,他還提供了強大的倉庫管理功能,構件搜索功能,它基於REST,友好的UI是一個extjs的REST客戶端,它佔用較少的內存,基於簡單文件系統而非數據庫。這些優點使其日趨成爲最流行的Maven倉庫管理器。
下載
開源版本–免費,專業版本–收費。下面爲開源版本2.14.3-02
https://sonatype-download.global.ssl.fastly.net/repository/downloads-prod-group/oss/nexus-2.14.3-02-bundle.tar.gz
安裝
要注意nexus啓動需要2核2G以上的配置,否則啓動不了。
解壓
# 進入安裝目錄
cd /home/nexus
# 下載安裝包
wget https://sonatype-download.global.ssl.fastly.net/repository/downloads-prod-group/oss/nexus-2.14.3-02-bundle.tar.gz
# 解壓到當前文件夾
tar -zxvf nexus-2.14.3-02-bundle.tar.gz
解壓之後可以看到如下兩個目錄:
第一個文件夾nexus-2.14.3-02
是核心文件,第二個文件夾sonatype-work
是用來存儲下載下來的jar的。
修改配置
核心配置文件是/home/nexus/nexus-2.14.3-02/conf/nexus.properties
。
修改完成之後即可啓動。
啓動
cd /home/nexus/nexus-2.14.3-02
./bin/nexus start
這時候有可能會報錯,
那麼這時候修改運行文件nexus
,vim nexus
,添加一行代碼RUN_AS_USER=root
。然後重新啓動即可。
打開web頁面
打開瀏覽器,訪問地址:http://ip:port/nexus
。
點擊右上角登錄按鈕,默認用戶名:admin
,默認密碼:admin123
。
使用
功能說明
用途
指定私服的中央地址、將自己的Maven項目指定到私服地址、從私服下載中央庫的項目索引、從私服倉庫下載依賴組件、將第三方項目jar上傳到私服供其他項目組使用
倉庫
- hosted 類型的倉庫,內部項目的發佈倉庫
- releases 內部的模塊中release模塊的發佈倉庫
- snapshots 發佈內部的SNAPSHOT模塊的倉庫
- 3rd party 第三方依賴的倉庫,這個數據通常是由內部人員自行下載之後發佈上去
- proxy 類型的倉庫,從遠程中央倉庫中尋找數據的倉庫
- group 類型的倉庫,組倉庫用來方便我們開發人員進行設置的倉庫
配置
nexus配置大部分使用默認配置即可,主要是配置一個項目索引
選擇Central
倉庫,設置Download Remote Indexes:True
maven配置nexus
項目使用nexus私服
- 使用nexus私服的jar包,在項目的pom.xml文件中指定私服倉庫
<repositories>
<repository>
<id>nexus</id>
<name>nexus</name>
<url>http://10.1.24.226:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
- 使用nexus私服的插件,在項目的pom.xml文件中指定插件倉庫
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>nexus</name>
<url>http://10.1.24.226:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
本機所有maven項目都使用nexus私服的組件
可以在maven的設置文件settings.xml中添加屬性,並激活
<profiles>
<profile>
<id>nexusProfile</id>
<repositories>
<repository>
<id>nexus</id>
<name>nexus</name>
<url>http://10.1.24.226:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<!-- 激活 -->
<activeProfiles>
<activeProfile>nexusProfile</activeProfile>
</activeProfiles>
項目發佈到私服
maven項目使用命令:mvn clean deploy;需要在pom文件中配置一下代碼;
<distributionManagement>
<repository>
<id>user-release</id>
<name>User Project Release</name>
<url>http://10.1.24.226:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>user-snapshots</id>
<name>User Project SNAPSHOTS</name>
<url>http://10.1.24.226:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
注意還需要配置mvn發佈的權限,否則會報401錯誤,在settings.xml中配置權限,其中id要與pom文件中的id一致
<server>
<id>user-release</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>user-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
完成之後即可在倉庫中看到你上傳的jar包
這時候在別的項目中就可以直接引用了
<dependency>
<groupId>person.bo</groupId>
<artifactId>just-test</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>