開發Java應用系統,用到Maven、sbt和 Gradle等構建工具,在構建過程當中通常須要從互聯網下載依賴庫,構建私有倉庫就是爲了在開發組或者部門內共用,從而節省總體的下載成本和構建成本。下面先以Maven爲例說明。 java
Maven是一個強大的構建工具,通常用於Java項目。Maven項目基於對象模型(POM),能夠經過一小段描述信息來管理項目的構建,報告和文檔的軟件項目管理工具。Maven 除了以程序構建能力爲特點以外,還提供高級項目管理工具。因爲 Maven 的缺省構建規則有較高的可重用性,因此經常用兩三行 Maven 構建腳本就能夠構建簡單的項目。linux
Maven的Java項目通常須要下載第三方組件,下載後構成本地倉庫,爲了減小網絡對構建項目的影響,通常會構建私服倉庫,代理第三方庫。Nexus就是構建私服倉庫的優秀軟件。apache
圖 1 三層倉庫架構瀏覽器
Java編譯環境包括核心的JDK和編譯工具,由於Java的編譯工具備不少種,而開源項目做者的隨意性很高,經常使用的工具備maven,gradle,sbt,ant等等,本文關注Maven。bash
由於Oracle再也不維護Java1.7,因此採用Java 1.8做爲編譯核心.服務器
操做系統採用Centos7.4網絡
yum install java-1.8.0-openjdk-devel java-1.8.0-openjdk java-1.8.0-openjdk-headless -y
查看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)
vi ~/.bashrc
增長less
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
即刻生效jvm
source ~/.bashrc
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
…
從官方網站如今最新3.X版
https://www.sonatype.com/download-oss-sonatype
下載(2018年3月)最新版nexus-3.9.0-01-unix.tar.gz ()
先規劃存儲私有倉庫的目錄,做者本機的/opt目錄空間較多,因此以/opt爲例
cd /opt/scm tar -xf ~/download/nexus-3.9.0-01-unix.tar.gz -C .
生成兩個目錄
nexus-3.9.0-01 sonatype-work
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
sudo systemctl daemon-reload sudo systemctl start nexus.service sudo systemctl status nexus.service sudo systemctl enable nexus.service
用戶名的密碼爲:admin admin123
設置名稱和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
Nexus安裝後自帶maven-releases,maven-snapshots兩個倉庫,用於將生成的jar包發佈在這兩個倉庫中,在實際開發中須要將maven-releases設置爲能夠重複發佈。
maven-releases
注:maven-snapshots缺省是能夠從新部署的。
從maven官網下載3.5.0 http://maven.apache.org/download.cgi
cd tools tar -xf ../download/apache-maven-3.5.0-bin.tar.gz –C .
vi ~/.bashrc
增長
export PATH=/home/ansible/tools/apache-maven-3.5.0/bin:$PATH
即刻生效
source ~/.bashrc
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"
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>