使用Docker基于Nexus3 Manger快速搭建Maven私有仓库

官方文档

nexus manager:https://guides.sonatype.com/repo3/quick-start-guides/proxying-maven-and-npm/

docker 安装 nexus:https://hub.docker.com/r/sonatype/nexus3/

Maven的获取依赖示意图

本地maven私服指定的是一个私有仓库的情况

image-20210111102720359

本地maven私服指定的是一个私有仓库组的情况

image-20210111102839586

使用docker 安装nexus

$ sudo mkdir -p /opt/data/nexus/nexus-data && sudo chown -R 200 /opt/data/nexus/nexus-data && \
docker run -d -p 8081:8081 \
-e INSTALL4J_ADD_VM_PARAMS="-Xms1g -Xmx1g -XX:MaxDirectMemorySize=1g" \
-e NEXUS_CONTEXT=nexus \
-v /opt/data/nexus/nexus-data:/nexus-data \
--restart always
--name nexus sonatype/nexus3


# 测试  
$ curl http://192.168.1.100:8081/nexus/

# 启动后,查看日志信息
$ docker logs -f nexus

访问并登录到nexus

浏览器访问nexus 的地址

image-20210110200335091

登录nexus 服务

image-20210110200631483

查看密码

image-20210110200721188

登录成功,进入引导页面

image-20210110200823255

image-20210110200911919

image-20210110201118320

image-20210110201136423

创建用户(部署代码需要用户信息)

添加一个用户

image-20210110211526999

image-20210110211837530

配置私有仓库

创建一个proxy仓库、两个hosted仓库

创建私有仓库

image-20210110203505116

image-20210110203549799

设置私有仓库的具体信息

image-20210110204349701

image-20210111103354794

image-20210110205056576

修改仓库的信息,设置远程仓库地址为阿里云的:http://maven.aliyun.com/nexus/content/groups/public

image-20210110205310391

再创建两个仓库(release和snapshots),先创建releases的仓库

image-20210111155845546

image-20210111160235866

在创建一个存储临时版本的maven (hosted)仓库

image-20210111160528482

image-20210111160843652

settings、pom.xml 示例demo

设置maven的settings文件

流程:找不到才会往下接着找
mvn compile -> 从本地仓库找依赖(localRepository),找不到-> 从远程仓库找(repositories),找不到 -> 下载依赖到代理仓库,同时从代理仓库里面获取依赖。下载的镜像源是看看你的mirrors,如果没有配置,会使用远程仓库的mirror进行下载
<!-- filename settings.xml.haitao.bak -->
<settings>
  <!-- 改成你想设置的本地仓库路径 -->
  <localRepository>/opt/module/maven-3.6.3/maven_repository_haitaoss</localRepository>

  <!-- 为仓库列表配置的下载镜像列表。 -->
  <!-- 如果这里不配置,那么会使用私服里面配置的中央仓库地址下载依赖  -->
  <mirrors>
    <!-- 这里最好配置的是中央仓库,或者你能确定你配置的私服仓库里面的依赖满足项目的需求 也可以配置私服仓库地址 -->
    <!-- <mirror>
       就是本地在下载依赖的时候,通过这个镜像去下载(去这个镜像里面找文件)
      <id>nexus</id>
      <url>http://192.168.1.100:8081/nexus/repository/haitao-maven-repository/</url>
      如果url不是中央查明库地址,这里可以写 *
      <mirrorOf>*</mirrorOf>
    </mirror> -->

    <!-- 建议写这个 -->
    <mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
       <!-- 如果url是中央仓库的地址,mirrorOf 必须写central -->
      <mirrorOf>central</mirrorOf>
    </mirror>
  </mirrors>
  <!-- 配置好用户名和密码 -->
  <servers>
    <server>
      <id>nexus</id>
      <username>haitaoss</username>
      <password>haitaoss</password>
    </server>
  </servers>
  <profiles>
    <!-- 定义配置文件 -->
    <profile>
      <id>nexus</id>
      <!-- 设置编译器信息 -->
      <activation>
        <activeByDefault>true</activeByDefault>
        <jdk>1.8</jdk>
      </activation>
      <properties>
        <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>
      <!-- 远程仓库列表 -->
      <repositories>
        <repository>
          <!-- id 是在server标签里面配置的 -->
          <id>nexus</id>
          <!-- url d对应的是远程仓库的地址 -->
          <url>http://192.168.1.100:8081/nexus/repository/haitao-maven-repository/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>
      <!-- 插件仓库列表 -->
      <pluginRepositories>
        <pluginRepository>
          <!-- id 是在server标签里面配置的 -->
          <id>nexus</id>
          <!-- url d对应的是远程仓库的地址 -->
          <url>http://192.168.1.100:8081/nexus/repository/haitao-maven-repository/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>
  <!-- 应用那个配置文件 -->
  <activeProfiles>
    <!--make the profile active all the time -->
    <activeProfile>nexus</activeProfile>
  </activeProfiles>
</settings>

创建一个pom.xml

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.haitaoss</groupId>
  <artifactId>nexus-proxy</artifactId>
   <!-- <version>1.0-SNAPSHOT</version> -->
  <version>1.0.2</version>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
    </dependency>
  </dependencies>

  <!-- 设置插件 -->
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>${java.version}</source>
          <target>${java.version}</target>
          <encoding>${project.build.sourceEncoding}</encoding>
        </configuration>
      </plugin>
      <!--发布代码Jar插件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.8.2</version>
      </plugin>
      <!--发布源码插件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <phase>install</phase>
            <goals>
              <goal>jar-no-fork</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <!-- 加了这个配置,才能将本地jar 部署到私有仓库中(将jar deploy 到那个仓库) -->
  <distributionManagement>
    <repository>
      <!-- 这里的id与settings.xml.haitao.bak中的servers节点配置的用户ID一致,这样才能使用配置的用户去上传到maven私有仓库 -->
      <!-- 此仓库对应的为RELEASE版本的jar -->
      <id>nexus</id>
      <name>Nexus Release Repository</name>
      <!-- 这里的仓库地址的 type必须是hosted, version policy 必须是 Release-->
      <url>http://192.168.1.100:8081/nexus/repository/haitao-maven-repository-releases/</url>
    </repository>
    <snapshotRepository>
      <!-- 这里的id与settings.xml.haitao.bak中的servers节点配置的用户ID一致,这样才能使用配置的用户去上传到maven私有仓库 -->
      <!-- 此仓库对应的为SNAPSHOT版本的jar -->
      <id>nexus</id>
      <name>Nexus Snapshot Repository</name>
      <!-- 这里的仓库地址的 type必须是hosted, version policy 必须是 Snapshot-->
      <url>http://192.168.1.100:8081/nexus/repository/haitao-maven-repository-snapshots/</url>
    </snapshotRepository>
  </distributionManagement>
</project>

测试从远程仓库下载jar

mvn compile --s ~/.m2/settings.xml.haitao.bak

测试部署到远程仓库

mvn deploy --s ~/.m2/settings.xml.haitao.bak

测试拉取我们刚才部署到私服的jar

# 修改pom文件
<dependencies>
   <dependency>
      <groupId>cn.haitaoss</groupId>
      <artifactId>nexus-proxy</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
  
# 执行命令
mvn compile --s ~/.m2/settings.xml.haitao.bak

# 出现下面的错误

image-20210111170548419

出现问题的原因:

  • 通过中央仓库下载的依赖保存在 http://192.168.1.100:8081/nexus/repository/haitao-maven-repository/
  • 我们部署的<version>1.0-SNAPSHOT</version> 保存在http://192.168.1.100:8081/nexus/repository/haitao-maven-repository-snapshots/
  • 我们部署的<version>1.0.0</version> 保存在http://192.168.1.100:8081/nexus/repository/haitao-maven-repository-releases/
  • 而我们的dependence 里面使用的是 保存在http://192.168.1.100:8081/nexus/repository/haitao-maven-repository-snapshots/ 里面的jar,而我们的settings.xml 配置的仓库地址是http://192.168.1.100:8081/nexus/repository/haitao-maven-repository/,所以提示找不到依赖文件。

解决办法:

  1. 修改settings 里面的远程仓库地址成对应的仓库地址

image-20210111170756539

  1. 使用私有仓库组
  2. 可能还有其他解决方式,执行研究吧

配置私有仓库组

创建一个maven组

image-20210111171004571

image-20210111171022968

image-20210111171606990

settings、pom.xml、示例demo

<settings>
  <!-- 改成你想设置的本地仓库路径 -->
  <localRepository>/opt/module/maven-3.6.3/maven_repository_haitaoss</localRepository>

  <!-- 为仓库列表配置的下载镜像列表。 -->
  <!-- 如果这里不配置,那么会使用私服里面配置的中央仓库地址下载依赖  -->
  <mirrors>
    <!-- 这里最好配置的是中央仓库,或者你能确定你配置的私服仓库里面的依赖满足项目的需求 也可以配置私服仓库地址 -->
    <mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
       <!-- 如果url是中央仓库的地址,mirrorOf 必须写central -->
      <mirrorOf>central</mirrorOf>
    </mirror>
  </mirrors>
  <!-- 配置好用户名和密码 -->
  <servers>
    <server>
      <id>nexus</id>
      <username>haitaoss</username>
      <password>haitaoss</password>
    </server>
  </servers>
  <profiles>
    <!-- 定义配置文件 -->
    <profile>
      <id>nexus</id>
      <!-- 设置编译器信息 -->
      <activation>
        <activeByDefault>true</activeByDefault>
        <jdk>1.8</jdk>
      </activation>
      <properties>
        <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>
      <!-- 远程仓库列表 -->
      <repositories>
        <repository>
          <!-- id 是在server标签里面配置的 -->
          <id>nexus</id>
          <!-- url 对应的是远程仓库的地址 -->
          <url>http://192.168.1.100:8081/nexus/repository/haiao-maven-public/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>
      <!-- 插件仓库列表 -->
      <pluginRepositories>
        <pluginRepository>
          <!-- id 是在server标签里面配置的 -->
          <id>nexus</id>
          <!-- url d对应的是远程仓库的地址 -->
          <url>http://192.168.1.100:8081/nexus/repository/haiao-maven-public/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>
  <!-- 应用那个配置文件 -->
  <activeProfiles>
    <!--make the profile active all the time -->
    <activeProfile>nexus</activeProfile>
  </activeProfiles>
</settings>
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.haitaoss</groupId>
  <artifactId>nexus-proxy2</artifactId>
   <!-- <version>1.0-SNAPSHOT</version> -->
  <version>1.0.2</version>


  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
    </dependency>
  </dependencies>

  <!-- 设置插件 -->
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>${java.version}</source>
          <target>${java.version}</target>
          <encoding>${project.build.sourceEncoding}</encoding>
        </configuration>
      </plugin>
      <!--发布代码Jar插件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.8.2</version>
      </plugin>
      <!--发布源码插件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <phase>install</phase>
            <goals>
              <goal>jar-no-fork</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <!-- 加了这个配置,才能将本地jar 部署到私有仓库中(将jar deploy 到那个仓库) -->
  <distributionManagement>
    <repository>
      <!-- 这里的id与settings.xml.haitao.bak中的servers节点配置的用户ID一致,这样才能使用配置的用户去上传到maven私有仓库 -->
      <!-- 此仓库对应的为RELEASE版本的jar -->
      <id>nexus</id>
      <name>Nexus Release Repository</name>
      <!-- 这里的仓库地址的 type必须是hosted, version policy 必须是 Release-->
      <url>http://192.168.1.100:8081/nexus/repository/haitao-maven-repository-releases/</url>
    </repository>
    <snapshotRepository>
      <!-- 这里的id与settings.xml.haitao.bak中的servers节点配置的用户ID一致,这样才能使用配置的用户去上传到maven私有仓库 -->
      <!-- 此仓库对应的为SNAPSHOT版本的jar -->
      <id>nexus</id>
      <name>Nexus Snapshot Repository</name>
      <!-- 这里的仓库地址的 type必须是hosted, version policy 必须是 Snapshot-->
      <url>http://192.168.1.100:8081/nexus/repository/haitao-maven-repository-snapshots/</url>
    </snapshotRepository>
  </distributionManagement>
</project>

测试从远程仓库下载jar

mvn compile --s ~/.m2/settings.xml.haitao.bak

测试部署到远程仓库

mvn deploy --s ~/.m2/settings.xml.haitao.bak

测试拉取我们刚才部署到私服的jar

# 修改pom文件
 <dependencies>
    <dependency>
      <groupId>cn.haitaoss</groupId>
      <artifactId>nexus-proxy</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
  
# 执行命令
mvn compile --s ~/.m2/settings.xml.haitao.bak

image-20210111173044265

通过nexus 查看私有仓库里面的jar

1. Click Browse from the main toolbar.
2. Click Components.
3. Of your components, choose maven-proxy or npm-proxy. You'llsee the test component you proxied for the respective format during the previous build steps.
4. Click on a component name to review its details
5. The Components screen is a sub-section to the Browseinterface.In order to view other components, click Components directly from the current screen and select another repository name from step 3 in this part.

image-20210111165600489

image-20210111165740964

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值