Nexus搭建開發組的私有倉庫
原文地址: https://www.cnblogs.com/fanzhenyong/p/7709434.html
一、私有倉庫的價值
開發Java應用系統,用到Maven、sbt和 Gradle等構建工具,在構建過程中一般需要從網際網路下載依賴庫,構建私有倉庫就是為了在開發組或者部門內共用,從而節省整體的下載成本和構建成本。下面先以Maven為例說明。
Maven是一個強大的構建工具,一般用於Java專案。Maven專案基於物件模型(POM),可以通過一小段描述資訊來管理專案的構建,報告和文件的軟體專案管理工具。Maven 除了以程式構建能力為特色之外,還提供高階專案管理工具。由於 Maven 的預設構建規則有較高的可重用性,所以常常用兩三行 Maven 構建指令碼就可以構建簡單的專案。
Maven的Java專案一般需要下載第三方元件,下載後構成本地倉庫,為了減少網路對構建專案的影響,一般會構建私服倉庫,代理第三方庫。Nexus就是構建私服倉庫的優秀軟體。
圖 1 三層倉庫架構
二、準備工作
2.1、安裝Java編譯環境
Java編譯環境包括核心的JDK和編譯工具,因為Java的編譯工具有很多種,而開源專案作者的隨意性很高,常用的工具有maven,gradle,sbt,ant等等,本文關注Maven。
因為Oracle不再維護Java1.7,所以採用Java 1.8作為編譯核心.
2.1.1、安裝
作業系統採用Centos7.4
yum install java-1.8.0-openjdk-devel java-1.8.0-openjdk java-1.8.0-openjdk-headless -y
2.1.2、驗證
檢視jdk版本號
java -version
Picked up _JAVA_OPTIONS: -Xmx2048m -XX:MaxMetaspaceSize=512m -Djava.awt.headless=true
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)
2.1.3、設定環境變數
vi ~/.bashrc
增加
export JAVA_HOME=/usr/lib/jvm/java
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:$JRE_HOME/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH
即刻生效
[[email protected] ~]#source ~/.bashrc
2.2、虛擬機器訪問網際網路
vi /etc/sysconfig/network-scripts/ifcfg-ens32
注:環境不同網絡卡名會不同
增加天津聯通的DNS(注:作者在天津,請按個人本地的執行商做相應修改)
DNS1=202.99.96.68
DNS2=202.99.104.68
在實際測試中感覺天津電信的DNS更加靠譜,訪問一些特殊網站返回的IP能夠順利訪問,大家根據實踐選擇吧
DNS1=219.150.32.132
DNS2=219.146.0.130
重新啟動網路
systemctl restart network
測試
ping www.163.com
PING 163.xdwscache.ourglb0.com (42.81.9.47) 56(84) bytes of data.
64 bytes from 42.81.9.47 (42.81.9.47): icmp_seq=1 ttl=128 time=9.66 ms
64 bytes from 42.81.9.47 (42.81.9.47): icmp_seq=2 ttl=128 time=10.4 ms
…
三、安裝Nexus
3.1、下載nexus
從官方網站現在最新3.X版
https://www.sonatype.com/nexus-repository-oss
下載: Nexus Repository OSS
下載(2018年3月)最新版nexus-3.14.0-04-unix.tar.gz
3.2、部署
#方法一:
先規劃儲存私有倉庫的目錄,作者本機的/opt目錄空間較多,所以以/opt為例
cd /opt/scm
tar -xf ~/download/nexus-3.9.0-01-unix.tar.gz -C .
生成兩個目錄
nexus-3.14.0-04
sonatype-work
#方法二:
[[email protected] ~]#cd /app/nexus
[[email protected] ~]#wget https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.12.0-01-bundle.tar.gz
[[email protected] ~]#tar -zvxf nexus-2.12.0-01-bundle.tar.gz
[[email protected] ~]#mv nexus-2.12.0-01 nexus
3.3、系統服務
3.3.1、編輯系統服務檔案
vi /etc/systemd/system/nexus.service
[Unit]
Description=nexus service
After=network.target
[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/opt/scm/nexus-3.9.0-01/bin/nexus start # 安裝目錄, 注意檔案許可權許可權755
ExecStop=/opt/scm/nexus-3.9.0-01/bin/nexus stop # 安裝目錄, 注意檔案許可權許可權755
User=root
Restart=on-abort
[Install]
WantedBy=multi-user.target
3.3.2、設定為自啟動服務
sudo systemctl daemon-reload
sudo systemctl start nexus.service
sudo systemctl status nexus.service
sudo systemctl enable nexus.service
四、設定Nexus
4.1、瀏覽器登入
使用者名稱的密碼為:admin admin123
4.2、進入管理介面
4.3、增加新的代理源
設定名稱和URL:
Cache統一設定為200天 288000:
逐個增加常用代理:
再次強調,在
How long (in minutes) to cache metadata before rechecking the remote repository.處
統一設定為
288000 即200天,當然可以設定為更長的時間
設定maven-public
將這些代理加入Group
4.3、設定私用倉庫可重複釋出
Nexus安裝後自帶maven-releases,maven-snapshots兩個倉庫,用於將生成的jar包釋出在這兩個倉庫中,在實際開發中需要將maven-releases設定為可以重複釋出。
maven-releases
注:maven-snapshots預設是可以重新部署的。
五、安裝maven並設定私用倉庫
5.1、下載安裝
從maven官網下載3.5.0 http://maven.apache.org/download.cgi
cd tools
tar -xf ../download/apache-maven-3.5.0-bin.tar.gz –C .
5.2、設定環境變數
vi ~/.bashrc
增加
export PATH=/home/ansible/tools/apache-maven-3.5.0/bin:$PATH
即刻生效
source ~/.bashrc
5.3、測試
mvn -v
Picked up _JAVA_OPTIONS: -Xmx2048m -XX:MaxMetaspaceSize=512m -Djava.awt.headless=true
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T03:39:06+08:00)
Maven home: /home/ansible/tools/apache-maven-3.5.0
Java version: 1.8.0_102, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.102-4.b14.el7.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-514.el7.x86_64", arch: "amd64", family: "unix"
5.4、配置
vi ~/tools/apache-maven-3.5.0/conf/settings.xml(本地伺服器可以使用localhost,開發組其他伺服器則修改為對應Nexus伺服器的域名或者IP地址)
<settings>
<pluginGroups>
<pluginGroup>org.sonatype.plugins</pluginGroup>
</pluginGroups>
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8081/repository/maven-public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<!--Enable snapshots for the built in central repo to direct -->
<!--all requests to nexus via the mirror -->
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<!--make the profile active all the time -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
<servers>
<server>
<id>nexus</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
</settings>