Docker 搭建nexus私服

发布时间:2020-03-20 10:26:42编辑:admin阅读(2381)

    一、概述

    有三种专门的Maven仓库管理软件可以用来帮助大家建立私服:Apache基金会的Archiva、JFrog的Artifactory和Sonatype的Nexus。而Nexus是当前最流行的Maven仓库管理工具。
     私服是一种特殊的远程仓库,它是架设在局域网内的仓库服务,私服代理广域网上的远程仓库。供局域网内的Maven用户使用。当Maven需要下载构件的时候,它从私服请求,如果私服上不存在该构件,则从远程仓库下载,缓存在私服上以后,再为Maven的下载请求提供服务。此外,一些无法从外部仓库下载到的构件也能从本地上传到私服上供大家服务。

    1.png

     

    私服的工作原理容如下图所示:

    1.png

     

    二、安装nexus

    环境说明

    操作系统:centos 7.6

    java版本:1.8.0_211

    maven版本:3.6.3

    docker版本:19.03.5

    ip地址:192.168.31.183

     

    拉取镜像

    拉取最新版本,目前最新版本是3.20.1

    docker pull sonatype/nexus3

     

    持久化目录

    mkdir -p /data/nexus/data
    chmod 777 -R /data/nexus/data

     

    启动镜像

    docker run -d -p 8081:8081 --name nexus -v /data/nexus/data:/nexus-data sonatype/nexus3

     

    查看日志

    docker logs -f nexus

    输出:

    ...
    2019-12-26 08:06:12,134+0000 INFO  [jetty-main-1] *SYSTEM org.eclipse.jetty.server.AbstractConnector - Started ServerConnector@42da9de6{HTTP/1.1,[http/1.1]}{0.0.0.0:8081}
    2019-12-26 08:06:12,135+0000 INFO  [jetty-main-1] *SYSTEM org.eclipse.jetty.server.Server - Started @102936ms
    2019-12-26 08:06:12,137+0000 INFO  [jetty-main-1] *SYSTEM org.sonatype.nexus.bootstrap.jetty.JettyServer - 
    -------------------------------------------------
    Started Sonatype Nexus OSS 3.20.1-01
    -------------------------------------------------

    等待几分钟时间,出现 Started Sonatype Nexus OSS 表示启动好了。

     

    三、访问nexus

    打开浏览器,访问 http://192.168.31.183:8081/

    1.png

     

    点击右侧的登录

    1.png

    查看管理员admin密码

    # cat /data/nexus/data/admin.password

     

    登录

    1.png

     

    开始设置

    1.png

     

    修改密码

    1.png

     

    确认配置

    1.png

     

    四、添加阿里云maven代理

    点击settings->Repository->Repositories

    点击Create repositoty按钮

    1.png

     

    选择maven2 (proxy)

    1.png

     

    填写如下两个字段,分别是代理库的名称,所代理的上层库的url。阿里云url为:http://maven.aliyun.com/nexus/content/groups/public/

    1.png

     

    滚动到页面最下方,点击“Create repositoty”按钮。

    1.png

     

    可以看到刚刚新建的代理库已经存在了。

    1.png

     

    重新配置maven-public组,使其包含新建的aliyun-maven。在如上页面,点击maven-public,进入到配置页面。按下图进行修改。把aliyun-maven移至右侧,并向上移至第一位。然后点击保存。

     1.png

    点击左侧菜单Repositoty>Repositories,进入到仓库列表页面,点击maven-public一行的copy按钮,然后复制弹出的url,后面配置maven时需要使用。

    1.png

     

    四、配置maven

    修改配置文件

    cd /data/apache-maven-3.6.3/conf
    cp settings.xml settings.xml.bak
    vim settings.xml

     

    添加服务器认证信息,增加红色部分内容

    <!-- Another sample, using keys to authenticate.
        <server>
          <id>siteServer</id>
          <privateKey>/path/to/private/key</privateKey>
          <passphrase>optional; leave empty if not used.</passphrase>
        </server>
        --> 
        <server>
            <id>maven-releases</id>
            <username>admin</username>
            <password>abcd1234</password>
        </server>
        <server>
            <id>maven-public</id>
            <username>admin</username>
            <password>abcd1234</password>
        </server>
      </servers>

    注意:修改为自己设置的密码。

     

    增加mirrors

    <mirror>
          <id>mirrorId</id>
          <mirrorOf>repositoryId</mirrorOf>
          <name>Human Readable Name for this Mirror.</name>
          <url>http://my.repository.com/repo/path</url>
        </mirror>
         -->
        <mirror>
          <id>maven-public</id>
          <name>maven-public</name>
            <url>http://192.168.31.183:8081/repository/maven-public/</url>
          <mirrorOf>*</mirrorOf>
        </mirror>
      </mirrors>

    注意:修改ip地址为服务器ip

     

    五、测试nexus私服

    下载一个SpringBootDemo,进行打包测试。

    yum install -y git
    git clone https://github.com/solochen84/SpringBootDemo.git

     

    使用mvn命令打包

    # cd SpringBootDemo/
    # mvn clean install
    [INFO] Scanning for projects...
    Downloading from maven-public: http://192.168.31.183:8081/repository/maven-public/org/springframework/boot/spring-boot-starter-parent/1.5.4.RELEASE/spring-boot-starter-parent-1.5.4.RELEASE.pom
    Downloaded from maven-public: http://192.168.31.183:8081/repository/maven-public/org/springframework/boot/spring-boot-starter-parent/1.5.4.RELEASE/spring-boot-starter-parent-1.5.4.RELEASE.pom (7.5 kB at 7.6 kB/s)
    Downloading from maven-public: http://192.168.31.183:8081/repository/maven-public/org/springframework/boot/spring-boot-dependencies/1.5.4.RELEASE/spring-boot-dependencies-1.5.4.RELEASE.pom
    Downloaded from maven-public: http://192.168.31.183:8081/repository/maven-public/org/springframework/boot/spring-boot-dependencies/1.5.4.RELEASE/spring-boot-dependencies-1.5.4.RELEASE.pom (93 kB at 187 kB/s)
    Downloading from maven-public: http://192.168.31.183:8081/repository/maven-public/com/fasterxml/jackson/jackson-bom/2.8.8/jackson-bom-2.8.8.pom
    ...
    [INFO] Installing /root/SpringBootDemo/pom.xml to /root/.m2/repository/com/example/spring-boot-demo/0.0.1-SNAPSHOT/spring-boot-demo-0.0.1-SNAPSHOT.pom
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  04:13 min
    [INFO] Finished at: 2019-12-30T20:15:26+08:00
    [INFO] ------------------------------------------------------------------------

    如果有输出:http://192.168.31.183:8081/repository/maven-public,说明正在使用 nexus私服

    最后提示:BUILD SUCCESS,表示构建完成。

     

    构建完成后,它会自动创建一个target目录,这里面存放jar包

    # ls -l  target/
    总用量 14824
    drwxr-xr-x 3 root root       40 12月 30 20:13 classes
    drwxr-xr-x 3 root root       25 12月 30 20:13 generated-sources
    drwxr-xr-x 3 root root       30 12月 30 20:13 generated-test-sources
    drwxr-xr-x 2 root root       28 12月 30 20:14 maven-archiver
    drwxr-xr-x 3 root root       35 12月 30 20:13 maven-status
    -rw-r--r-- 1 root root 15170293 12月 30 20:15 spring-boot-demo-0.0.1-SNAPSHOT.jar
    -rw-r--r-- 1 root root     6517 12月 30 20:14 spring-boot-demo-0.0.1-SNAPSHOT.jar.original
    drwxr-xr-x 2 root root      129 12月 30 20:13 surefire-reports
    drwxr-xr-x 3 root root       17 12月 30 20:13 test-classes

     

    启动jar包

    # java -jar target/spring-boot-demo-0.0.1-SNAPSHOT.jar
    
      .   ____          _            __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v1.5.4.RELEASE)
    ...
    2019-12-30 20:18:39.923  INFO 23047 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
    2019-12-30 20:18:40.065  INFO 23047 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
    2019-12-30 20:18:40.077  INFO 23047 --- [           main] c.e.demo.SpringBootDemoApplication       : Started SpringBootDemoApplication in 8.484 seconds (JVM running for 9.609)

     

    访问页面

    http://192.168.31.183:8080/

    效果如下:

    1.png

     

    如果有第三方的sdk,阿里云maven没有时,可以上传到nexus私服。

    回到首页,点击Upload,上传到 maven-relaases,Extension会自动填充

    需要提供 Group ID,Atriface ID,Version。这个问java开发要一个。

     1.png

     

     

    但是,还是推荐将 第三方sdk嵌入到java项目里面,不使用nexus私服下载。如果一但nexus私服挂掉了,就很麻烦了。

    nexus私服还是去阿里云maven下载比较好。

     

    本文参考链接:

    https://www.cnblogs.com/wotoufahaiduo/p/11223834.html
    https://blog.csdn.net/lk142500/article/details/91357441
    https://www.cnblogs.com/sybblogs/p/9835977.html

    https://www.jianshu.com/p/e8e3ba719785


关键字