軟件和安裝環境
- nexus安裝包
下載地址:https://www.sonatype.com/oss-thank-you-tar.gz
我這里使用nexus-3.13.0-01(寫本文時的最新版) - JDK 1.8+
1. 安裝nexus
- 設置當前用戶可以打開的文件總數為65536
[hadoop@jed etc]$ sudo vim /etc/security/limits.conf
#在文件中添加以下內容,其中hadoop是用戶名
hadoop - nofile 65536
- 解壓安裝包
目錄說明:
bin: 包含nexus的啟動腳本以及啟動相關的配置文件,例如通過bin/nexus.vmoptions文件,你可以配置一些JVM參數和日志存放位置等配置
etc: 包含應用級別的配置文件
lib: 包含 Apache Karaf 相關的jar包
public: 包含應用相關的公共資源
system: 包含應用相關的構件和插件
- 進入bin目錄下,啟動nexus
[hadoop@jed bin]$ ./nexus start
Starting nexus
# 使用 nexus run 也會啟動 nexus,區別在于:start以守護線程方式啟動,run以非守護線程方式啟動
- 查看nexus狀態
[hadoop@jed bin]$ ./nexus status
nexus is running.
- 訪問Web UI
http://ip:8081/
看到以上頁面說明 nexus 啟動正常。
其他命令說明:
# 重啟
nexus restart
# 強制重新刷新倉庫
nexus force-reload
2. 配置 nexus 以服務的形式啟動,并且開機自啟動
(0) 準備工作
- 關閉之前手動開啟的nexus進程
[hadoop@jed bin]$ ./nexus stop
Shutting down nexus
Stopped.
- 配置環境變量,添加NEXUS_HOME
[hadoop@jed nexus-3.13.0-01]$ vim ~/.bash_profile
export NEXUS_HOME=/opt/apps/nexus-3.13.0-01
export PATH=$PATH:$NEXUS_HOME/bin
[hadoop@jed nexus-3.13.0-01]$ source ~/.bash_profile
- 修改$NEXUS_HOME/bin/nexus.rc
# 后面改為你自己的用戶名
run_as_user="hadoop"
- 修改$NEXUS_HOME/bin/nexus
# 這一行是注釋的,釋放掉,后面寫JAVA_HOME的路徑
INSTALL4J_JAVA_HOME_OVERRIDE=/opt/apps/jdk1.8.0_172
- 做一個$NEXUS_HOME/bin/nexus到/etc/init.d/nexus的軟連接
[hadoop@jed bin]$ sudo ln -s $NEXUS_HOME/bin/nexus /etc/init.d/nexus
(1) 方法一:使用chkconfig
cd /etc/init.d
## 添加nexus服務
sudo chkconfig --add nexus
## 設置在3、4、5這3個系統運行級別的時候自動開啟nexus服務
sudo chkconfig --levels 345 nexus on
## 啟動nexus服務
sudo service nexus start
關于系統運行級別以及chkconfig命令的用法參考Linux的運行級別和chkconfig用法
(2) 方法二:使用update-rc.d
cd /etc/init.d
sudo update-rc.d nexus defaults
sudo service nexus start
(3) 方法三:使用systemd(CentOS-7推薦使用)
# 在/etc/systemd/system/下新建文件nexus.service
[hadoop@jed nexus-3.13.0-01]$ touch /etc/systemd/system/nexus.service
# 編輯該文件,內容如下(你可能需要適當修改)
[hadoop@jed nexus-3.13.0-01]$ sudo vim /etc/systemd/system/nexus.service
[Unit]
Description=nexus service
After=network.target
[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/opt/apps/nexus-3.13.0-01/bin/nexus start
ExecStop=/opt/apps/nexus-3.13.0-01/bin/nexus stop
User=hadoop
Restart=on-abort
[Install]
WantedBy=multi-user.target
[hadoop@jed nexus-3.13.0-01]$ sudo systemctl daemon-reload
[hadoop@jed nexus-3.13.0-01]$ sudo systemctl enable nexus.service
[hadoop@jed nexus-3.13.0-01]$ sudo systemctl start nexus.service
3. Nexus倉庫的分類
Maven可以直接從宿主倉庫下載構件,也可以從代理倉庫下載構件,代理倉庫會間接的從遠程倉庫下載并緩存構件,為了方便,maven也可以從倉庫組下載構件,而倉庫組沒有實際內容,它會轉向其包含的宿主倉庫或者代理倉庫獲得實際構件的內容。
登錄Nexus Web UI,管理員默認賬戶密碼為admin/admin123
查看內置的倉庫
nexus 3.13 自帶的部分倉庫的說明:
- maven-central:代理倉庫,該倉庫代理Maven中央倉庫,策略為release,因此只會下載和緩存中央倉庫中的發布版本的構件。
- maven-releases: 宿主倉庫,策略為release,用來部署組織內部的發布版本的構件。
- maven-snapshots:宿主倉庫,策略為snapshots,用來部署組織內部的快照版本的構件。
- maven-public:倉庫組,包含了以上3個倉庫
4. Nexus 操作
(1) 創建用戶
退出系統,用新創建的用戶登錄(賬戶hadoop/密碼hadoop)
(2) 創建宿主倉庫
(2) 創建代理倉庫
(3) 創建倉庫組
(4) 配置maven從Nexus下載構件
pom如下:
<repositories>
<repository>
<id>nexus</id>
<name>nexus</name>
<url>http://jed:8081/repository/hadoop-test-repository-group/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>nexus</name>
<url>http://jed:8081/repository/hadoop-test-repository-group/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
你可以從nexus頁面上獲得倉庫的url
在pom中的id、name不需要與倉庫中的對應,但url一定要一樣,在pom中,多個倉庫的id一定是不同的,例如<repositories>下配置了多個倉庫,那么這些倉庫的id一定要不同,但是<repositories>和<pluginRepositories>下可以共用一個倉庫。
以上配置只在當前的項目中生效,如果想讓你本地的所有的maven項目都去自定義的私服下載構件,需要在settings.xml中配置如下:
<settings>
<profiles>
<profile>
<repositories>
<repository>
<id>nexus</id>
<name>nexus</name>
<url>http://jed:8081/repository/hadoop-test-repository-group/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>nexus</name>
<url>http://jed:8081/repository/hadoop-test-repository-group/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
在profile中配置的私服確實可以作用于本地所有的maven項目,但是maven除了會去私服中下載構件,也會去maven中央倉庫中下載,如果我們想要配置maven的下載請求僅僅通過nexus,以全面發揮私服的作用,這就需要在<mirror>級別添加配置了(在profile配置的基礎上再在mirror上添加配置),settings.xml中的內容如下:
<mirrors>
<mirror>
<id>nexus</id>
<url>http://jed:8081/repository/hadoop-test-repository-group/</url>
<!-- * 代表這個私服可以作為所有遠程倉庫的鏡像 -->
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
(5) 部署構件到nexus
項目中的pom配置如下:
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>nexus-releases</name>
<url>http://jed:8081/repository/hadoop-hosted-test-repository/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshot</id>
<name>nexus-snapshot</name>
<url>http://jed:8081/repository/hadoop-hosted-test-repository-snapshots/</url>
</snapshotRepository>
</distributionManagement>
這里設置了兩個倉庫,一個用于部署發布版構件,一個用于部署快照版構件,用于部署快照版構件的倉庫我們在之前演示創建倉庫的時候沒有創建,你需要自己創建一個,另外無論是部署快照版構件還是部署發布版構件,都是需要部署到宿主類型的倉庫中,而我們之前配置的下載構件的倉庫是一個倉庫組,這里需要注意一下。
另外,nexus倉庫對于匿名用戶是只讀的,所以還需要在settings.xml中配置認證信息,如下:
<servers>
<server>
<id>nexus-releases</id>
<username>hadoop</username>
<password>hadoop</password>
</server>
<server>
<id>nexus-snapshot</id>
<username>hadoop</username>
<password>hadoop</password>
</server>
</servers>
然后在項目根目錄下執行maven命令mvn deploy
即可,如果想在任意路徑下部署某個已經打好的jar包,完整的maven命令如下:
mvn deploy:deploy-file \
-DgroupId=your.group.id \
-DartifactId=your.artifact.id \
-Dversion=1.0.0 \
-Dpackaging=jar \
-Dfile=/path/to/your-jar-1.0.0.jar \
-Durl=http://ip:port/your/repository/url\
-DrepositoryId=yourRepositoryId
除了使用 maven 命令,還可以使用nexus WEB 界面來手動上傳第三方jar包:
(6) 為項目分配獨立的倉庫
- 假設項目名稱為bonc,新建兩個宿主倉庫bonc-releases和bonc-snapshots分別用于部署bonc項目的發布版構件和快照版構件,過程不再贅述
- 創建用于管理這兩個倉庫的權限(這里只演示為bonc-releases倉庫創建權限)
- 創建一個角色bonc-role,添加這兩個權限
- 創建一個用戶,賦予bonc-role角色
- 在pom中配置部署構件的倉庫
<distributionManagement>
<repository>
<id>bonc-releases</id>
<name>bonc-releases</name>
<url>http://jed:8081/repository/bonc-releases/</url>
</repository>
<snapshotRepository>
<id>bonc-snapshots</id>
<name>bonc-snapshots</name>
<url>http://jed:8081/repository/bonc-snapshots/</url>
</snapshotRepository>
</distributionManagement>
- 在 settings.xml中配置認證信息
<servers>
<server>
<id>bonc-releases</id>
<username>bonc</username>
<password>bonc</password>
</server>
<server>
<id>bonc-snapshots</id>
<username>bonc</username>
<password>bonc</password>
</server>
</servers>
這樣就能把bonc項目的構件發布到專屬的兩個倉庫中,而其他用戶(沒有設置管理這兩個倉庫權限或角色的用戶)是不能部署構件到這兩個倉庫中的,當然了系統級別的用戶(admin和上文創建的hadoop用戶是可以的)
(7) nexus的調度任務
你可以在nexus界面上配置一些周期性執行的后臺任務來維護nexus,以下為nexus 3.13 支持的后臺任務的部分說明:
接下來以發布倉庫的索引文件為例來演示怎么調度task
可以看到,新創建的task在等待執行:
到達設置的時間后,task開始執行,狀態為running:
當任務運行完成后,剛才那條task就會消失(因為剛才的task設置只執行一次),需要注意的是,這里生成的索引文件,并不是被代理的倉庫中的所有構件的索引,也就是說,這個任務并沒有生成maven中央倉庫中所有構件的索引,而是nexus倉庫中已經存在的構件的索引
最后,關于更多nexus 3.x 的使用和配置的細節可以去Nexus 3 的官方文檔中學習