搭建私服好处

  1. 方便团队协作,A开发完成的代码,提交的私服上,B添加pom.xml就可以使用A的代码了
  2. 公司内部员工,不需要连接外网就可以从内网拉取依赖了。

安装步骤

1. 查看镜像

docker search nexus3

查询内容

[dingguo@dingguodeMacBook-Pro:~]$ docker search nexus3
NAME                                  DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
sonatype/nexus3                       Sonatype Nexus Repository Manager 3             733                                     
shifudao/nexus3                       clone from nexus3 image but based from openj…   4                                       [OK]
home1oss/nexus3                       An auto configured nexus3                       2                                       [OK]
madmuffin/nexus3                      Sonatype Nexus3 Milestone7 docker image         2                                       [OK]

2. 拉取第一个镜像

docker pull sonatype/nexus3

3.查看镜像

[dingguo@dingguodeMacBook-Pro:~]$ docker images
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
sonatype/nexus3                    latest              87d9a097d22c        2 weeks ago         634MB

4. 修改镜像名称

docker tag sonatype/nexus3 nexus3

查看镜像

[dingguo@dingguodeMacBook-Pro:~]$ docker images
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
nexus3                             latest              87d9a097d22c        2 weeks ago         634MB

5. 创建容器

docker run -id --privileged=true --name=nexus3 --restart=always -p 8081:8081 -v /Users/dingguo/nexus3/nexus-data:/var/nexus-data 87d

查看docker日志

docker logs -f 9b

会返回一大串的内容,等几分钟,直到出现下面表示启动成功

*****省略*****
*****省略*****
*****省略*****
2020-01-10 03:36:12,690+0000 INFO  [jetty-main-1] *SYSTEM org.eclipse.jetty.server.AbstractConnector - Started ServerConnector@76e00d3{HTTP/1.1,[http/1.1]}{0.0.0.0:8081}
2020-01-10 03:36:12,698+0000 INFO  [jetty-main-1] *SYSTEM org.eclipse.jetty.server.Server - Started @97140ms
2020-01-10 03:36:12,700+0000 INFO  [jetty-main-1] *SYSTEM org.sonatype.nexus.bootstrap.jetty.JettyServer - 
-------------------------------------------------

Started Sonatype Nexus OSS 3.20.1-01

-------------------------------------------------

6. 在页面输入地址  http://localhost:8081/

nexus3 docker部署 log_私服

点击右上角登录,默认账号/密码,admin/admin123

如果出现报错:如下

nexus3 docker部署 log_私服_02

解决方案:

进入容器内部,查看 /nexus-data/admin.password

docker exec -it b8 bash
bash-4.4$ dir
bin   dev  help.1  lib	  licenses    media  nexus-data  proc  run   srv  tmp		     uid_template.sh  var
boot  etc  home    lib64  lost+found  mnt    opt	 root  sbin  sys  uid_entrypoint.sh  usr
bash-4.4$ more nexus-data/admin.password 
e7a42a55-6188-4dea-bad7-3989998f03b5
bash-4.4$

在页面输入密码:e7a42a55-6188-4dea-bad7-3989998f03b5

登录成功会弹出框,点击next,输入新的密码就可以了。

Nexus私服配置

如果我们要把写好的代码传到私服上,怎么配置呢?

1. 查看我们Nexus私服的地址

点击 maven-public 一行的copy,将地址复制保存

nexus3 docker部署 log_Maven_03

2. 修改maven的settings.xml文件

找到 settings.xml 文件,打开,需要添加两个部分

第一个部分

配置nexus私服地址,<url>标签的地址是 maven-public

<!-- Nexus私服配置 -->
<profile>
    <id>dev</id>
    <repositories>
        <repository>
            <id>nexus</id>
            <url>http://localhost:8081/repository/maven-public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
</profile>

第二个部分

配置 Nexus私服发布的用户名密码,和私服登录密码一致

<!-- Nexus私服发布的用户名密码 -->
</servers>
  <server>  
    <id>maven-releases</id>  
    <username>admin</username>  
    <password>123456</password>  
  </server>  
  <server>  
    <id>maven-snapshots</id>  
    <username>admin</username>  
    <password>123456</password>  
  </server> 
</servers>

ok,settings.xml的配置就完成了。

2. 配置pom.xml文件

同理复制下图地址

nexus3 docker部署 log_Nexus_04

pom.xml新增内容

<!--私服仓库-->
<distributionManagement>
    <repository>
        <id>maven-releases</id>
        <name>Nexus Release Repository</name>
        <url>http://localhost:8081/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
        <id>maven-snapshots</id>
        <name>Nexus Snapshot Repository</name>
        <url>http://localhost:8081/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

3. 编写代码测试

新增一个类 HelloNexus 

package com.wjx;

/**
 * @Description:
 * @Author: dingguo
 * @Date: 2020/1/10 下午12:40
 */
public class HelloNexus {
    
    public void hello(){
        System.out.println("this is test");
    }
}

接下来就是将代码推到私服,代码版本

nexus3 docker部署 log_nexus3 docker部署 log_05

如图,点击 deploy

nexus3 docker部署 log_nexus3 docker部署 log_06

当出现下面这个表示已经上传成功

nexus3 docker部署 log_nexus3 docker部署 log_07

进入 maven-release 如下图,表示代码已经上传成功

nexus3 docker部署 log_Docker_08

右下角会有maven的依赖,如果在其他项目使用,把依赖拷贝过去就可以使用了。

可能遇到的问题

如果,相同版本再点击deploy就会报错

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.024 s
[INFO] Finished at: 2020-01-10T14:22:32+08:00
[INFO] Final Memory: 13M/167M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project nexus-demo: Failed to deploy artifacts: Could not transfer artifact com.wjx:nexus-demo:jar:1.1.0 from/to maven-releases (http://localhost:8081/repository/maven-releases/): Failed to transfer file: http://localhost:8081/repository/maven-releases/com/wjx/nexus-demo/1.1.0/nexus-demo-1.1.0.jar. Return code is: 400, ReasonPhrase: Repository does not allow updating assets: maven-releases. -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
错误信息:Repository does not allow updating assets: maven-releases. -> [Help 1]


解决方案:

nexus3 docker部署 log_Maven_09

nexus3 docker部署 log_Maven_10

nexus3 docker部署 log_Nexus_11

再次上传就不报错了。

附上 settingx.xml 的完全配置

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

    <!-- 本地仓库的位置 -->
    <localRepository>${user.home}/.m2/repository</localRepository>
  
    <!-- Apache Maven 配置 -->
    <pluginGroups/>
    <proxies/>

    <!-- Nexus私服发布的用户名密码 -->
    <servers>
      <server>  
        <id>maven-releases</id>  
        <username>admin</username>  
        <password>123456</password>  
      </server>  
      <server>  
        <id>maven-snapshots</id>  
        <username>admin</username>  
        <password>123456</password>  
      </server> 
    </servers>
    
    <!-- 阿里云镜像 -->
    <mirrors>
        <mirror>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <!-- https://maven.aliyun.com/repository/public/ -->
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
    </mirrors>

    <!-- 配置: java8, 先从阿里云下载, 没有再去私服下载  -->
    <!-- 20190929 hepengju 测试结果: 影响下载顺序的是profiles标签的配置顺序(后面配置的ali仓库先下载), 而不是activeProfiles的顺序 -->
    <profiles>
        <!-- 全局JDK1.8配置 -->
        <profile>
            <id>jdk1.8</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
            </activation>
            <properties>
                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
            </properties>
        </profile>

        
        <!-- Nexus私服配置 -->
        <profile>
            <id>dev</id>
            <repositories>
                <repository>
                    <id>nexus</id>
                    <url>http://localhost:8081/repository/maven-public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
        </profile>
        
        <!-- 阿里云配置: 提高国内的jar包下载速度 -->
        <profile>
            <id>ali</id>
            <repositories>
                <repository>
                    <id>alimaven</id>
                    <name>aliyun maven</name>
                    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>alimaven</id>
                    <name>aliyun maven</name>
                    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
                </pluginRepository>
            </pluginRepositories>
        </profile>

    </profiles>
    
    <!-- 激活配置 --> 
    <activeProfiles>
        <activeProfile>jdk1.8</activeProfile>
        <activeProfile>dev</activeProfile>
        <activeProfile>ali</activeProfile>
    </activeProfiles>
</settings>