利用nexus构建maven、docker、npm、gradle私服仓库

7,355 阅读3分钟

前言

在小型公司发展历程中,开发对仓库的依赖不断提高,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…

私有仓库

选择host
选择host

创建私有仓库
创建私有仓库

上传本地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仓库功能足够强大,大家可以对着自己场景研究。