一、私有倉庫的價值
開發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
即刻生效
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/download-oss-sonatype
下載(2018年3月)最新版nexus-3.9.0-01-unix.tar.gz ()
3.2、部署
先規划存儲私有倉庫的目錄,作者本機的/opt目錄空間較多,所以以/opt為例
cd /opt/scm tar -xf ~/download/nexus-3.9.0-01-unix.tar.gz -C .
生成兩個目錄
nexus-3.9.0-01 sonatype-work
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 ExecStop=/opt/scm/nexus-3.9.0-01/bin/nexus stop User=ansible 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
逐個增加常用代理
1. aliyun http://maven.aliyun.com/nexus/content/groups/public 2. apache_snapshot https://repository.apache.org/content/repositories/snapshots/ 3. apache_release https://repository.apache.org/content/repositories/releases/ 4. atlassian https://maven.atlassian.com/content/repositories/atlassian-public/ 5. central.maven.org http://central.maven.org/maven2/ 6. datanucleus http://www.datanucleus.org/downloads/maven2 7. maven-central (安裝后自帶,僅需設置Cache有效期即可) https://repo1.maven.org/maven2/ 8. nexus.axiomalaska.com http://nexus.axiomalaska.com/nexus/content/repositories/public 9. oss.sonatype.org https://oss.sonatype.org/content/repositories/snapshots 10.pentaho https://public.nexus.pentaho.org/content/groups/omni/
再次強調,在
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>