用nexus3.x 官方镜像搭建maven私有镜像仓库


1.获取nexus3镜像

   docker pull sonatype/nexus3 

2.创建启动nexus3容器

  docker run -dit -p 8081:8081 --name nexus -v /home/nexus-data:/var/nexus-data --restart=always sonatype/nexus3

参数说明

    • -dit:在容器中打开一个伪终端进行交互操作,并在后台运行
    • -v:把宿主机的/root/nexus-data目录挂载到容器/var/nexus-data目录,来实现数据的持久化
    • -p:映射端口,访问宿主机的8081端口就访问到nexus容器的服务了
    • --restart=always:这是重启的策略,假如这个容器异常退出会自动重启容器
    • --name nexus:创建容器命名为nexus,你可以随便命名
    • sonatype/nexus3:pull下来的镜像

3.访问nexus

  在浏览器输入:http://ip:8081即可看到以下页面:(ip为远程主机的ip地址)

用初始密码admin/admin123登录,发现报出:Your admin user password is located in /nexus-data/admin.password on the server.

意思是admin用户密码在/nexus-data/admin.password 文件中,那么接下来我就需要找到这个文件问题就解决了。

进入nexus容器

docker exec -it nexus3 /bin/bash

bash-4.4$ cd nexus-data/

bash-4.4$ ls
admin.password blobs cache db elasticsearch etc generated-bundles instances javaprefs karaf.pid keystores lock log orient port restore-from-backup tmp
bash-4.4$ cat admin.password

拿着上图这个初始密码,点击右上方的Sign in进行登录

 

nexus私服简介

可以看到默认情况下Nexus会帮我们创建了几个仓库,仔细观察红色框住的地方,里面有几种仓库的类型,解释如下:

  • proxy 远程仓库的代理,比如说nexus配置了一个central repository的proxy,当用户向这个proxy请求一个artifact的时候,会现在本地查找,如果找不到,则会从远程仓库下载,然后返回给用户。
  • hosted 宿主仓库,用户可以把自己的一些仓库deploy到这个仓库中
  • group 仓库组,是nexus特有的概念,目的是将多个仓库整合,对用户暴露统一的地址,这样就不需要配置多个仓库地址。

下面我们仔细看一下里面的一些仓库,点击maven-central仓库:

可以看到是一个proxy类型的仓库,默认代理的远程仓库地址是https://repo1.maven.org/maven2/,这里把它改为阿里云http://maven.aliyun.com/nexus...

进入maven-public查看

 可以看到这是一个group类型的仓库,里面包含了maven-releases/maven-snapshots/maven-central仓库,意思是我们只需要在本地添加这个仓库,则可以依赖到上述3个仓库中的库了。

实现本地上传代码库

1.创建一个新的仓库(也可以选用已经存在的仓库)
创建仓库,点击Create repository,然后选择maven2(hosted)然后输入仓库名称(test-release)。在version policy中选择这个仓库的类型,这里选择release,在Deployment policy中选择Allow redeploy(这个很重要)

 2.修改本地D:Program Files (x86)apache-maven-3.2.3conf目录下的settings.xml


    
    
    
    
     
     
        releases
        admin
        123456
       
      
  

3.用IDEA搭建springboot项目,pom.xml文件内容为

<?xml version="1.0" encoding="UTF-8"?>

    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.9.RELEASE
         
    
    com.example
    docker
    docker
    Demo project for Spring Boot
    
    1.0-RELEASE

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
    
        
            
            releases
            http://139.9.40.41:8081/repository/test-release/
        
    

    
        
            
            
                org.apache.maven.plugins
                maven-deploy-plugin
                2.7
            
            
            
                org.apache.maven.plugins
                maven-source-plugin
                2.2.1
                
                    
                        package
                        
                            jar
                        
                    
                
            
        
    

打开终端,输入mvn deploy,若报出mvn不是内部命令,可参考
https://blog.csdn.net/sz15732...,最后用管理职身份再次运行IDEA。

上传成功,回到Nexus的网页中查看结果

引用依赖

用IDEA新建一个springboot工程,pom.xml使用依赖

<?xml version="1.0" encoding="UTF-8"?>

    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.9.RELEASE
         
    
    com.example
    test
    0.0.1-SNAPSHOT
    test
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            com.example
            docker
            1.0-RELEASE
        

    

    
        
            releases
            http://139.9.40.41:8081/repository/test-release/
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

最后发现jar依赖成功

 参考文献:https://segmentfault.com/a/1190000020708330