前言
在小型公司發展歷程中,開發對倉庫的依賴不斷提高,java web需要maven倉庫、android需要gradle倉庫、運維需要docker倉庫…… 是時候搞一套倉庫私服了。
初識nexus
nexus是目前市場上,支援倉庫種類最多,使用者群體最大的一個倉庫平臺,上述所有的倉庫,它均支援。
安裝nexus
這裡省略安裝步驟,建議使用3.x及以上版本的nexus
配置maven私服
這裡倉庫主要指2種,一種是代理的倉庫,使得內網下載倉庫不必要去外網上下載,一種是私有的倉庫,存公司內部jar。
代理倉庫
使用管理員帳號(預設使用者名稱admin 密碼admin123 預設埠8080)
點選建立倉庫
選擇maven2(proxy) 代理模式,這裡填寫阿里雲的maven倉庫代理,為了得到更快的下載速度。
maven.aliyun.com/nexus/conte…
私有倉庫
上傳本地jar
首先配置一下maven配置檔案的使用者名稱密碼,我這裡是/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/conf/settings.xml
<server>
<id>nexus</id>
<username>admin</username>
<password>admin123</password>
</server>複製程式碼
其次本地開發工具生成好jar,最後敲命令
mvn deploy:deploy-file
-DgroupId=com.***
-DartifactId=***
-Dversion=1.2.3
-Dpackaging=jar
-Dfile=D:\***-1.2.3.jar
-Durl=http://127.0.0.1:8081/repository/java/
-DrepositoryId=nexus-releases複製程式碼
maven使用
修改pom檔案,增加以下語句。
<repositories>
<repository>
<id>nexus</id>
<name>Maven Repository</name>
<url>http://10.10.100.*:8081/repository/aliyun/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>複製程式碼
docker 代理
其中代理地址,我使用了阿里雲的免費倉庫代理地址。
私有倉庫和上面java類似同理,可以自己操作下
docker使用
終端輸入(這裡我使用域名及埠對映出的docker倉庫)
docker login -u admin -p admin123 rep.***.com:10055複製程式碼
當返回登入成功時,說明配置正常。
然後,我們嘗試pull一個映象
docker pull rep.*.com:10055/nginx複製程式碼
這裡有個坑,我們預設出去的可能是http,而很多docker映象預設拉取的是https,所以會報錯,這裡
vi /etc/docker/daemon.json
手動輸入 { "insecure-registries":["rep.*.com:10055","10.10.100.11:8082"] }
systemctl daemon-reload
systemctl restart docker複製程式碼
上傳映象
docker push rep.*.com:10055/<repo-name>:<tag>複製程式碼
npm倉庫
建立npm倉庫
映象地址填寫阿里的npm.taobao.org/mirrors/npm…
使用
將npm登錄檔寫入本地檔案
npm config set registry http://localhost:8081/repository/npm/複製程式碼
或設定.npmrc 檔案
registry = http://localhost:8081/repository/npm-all/複製程式碼
釋出npm
npm publish --registry http://localhost:8081/repository/npm-internal/複製程式碼
android使用
gradle.properties配置基本資訊
MAVEN_URL= http://localhost:8080/nexus/content/**
MAVEN_SNAPSHOT_URL = http://localhost:8080/nexus/**
#maven groupId
GROUP=*
#賬號
NEXUS_USERNAME=admin
#密碼
NEXUS_PASSWORD=admin123
# groupid(最終你引用時的名字)
GROUP_ID = com.*.tools
# type
TYPE = aar
# description
DESCRIPTION = dependences lib複製程式碼
在Module的build.gradle配置Task
uploadArchives {
configuration = configurations.archives
repositories {
mavenDeployer {
snapshotRepository(url: MAVEN_SNAPSHOT_URL) {
authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
}
repository(url: MAVEN_URL) {
authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
}
pom.project {
#版本,有更新時修改版本號,在上傳
version '1.*.*'
#名字
artifactId '**'
groupId **
packaging TYPE
description '***'
}
}
}
}
artifacts {
#編譯的原始碼型別
archives file('***.aar')
}複製程式碼
引用的話,只需要在 project 的 build.gradle中配置
allprojects {
repositories {
jcenter()
maven { url MAVEN_URL }
}
}複製程式碼
然後正常使用
compile 'com.**.tools:**:1.0.0'複製程式碼
結尾
有的以上倉庫,可以滿足絕大部分場景的需求,當然nexus倉庫功能足夠強大,大家可以對著自己場景研究。