记录:用Nexus3搭建Maven私有仓库,用于install & deploy操作

1. Maven私有仓库安装

1.1 本机配置

  • Windows 10 x64

1.2 官网下载地址

Download

image-20210428214640894

image-20210428214824572

我这里下载的是Windows的版本

1.3 解压

解压得到两个文件夹

  • nexus-3.30.1-01
  • sonatype-work

image-20210428220456213

1.4 启动

进到 nexus-3.30.1-01/bin 目录下,使用cmd窗口执行命令:nexus.exe /run

看到下面的日志时,启动完成:

image-20210428220706887

1.5 登陆界面

默认登陆页面地址:http://localhost:8081

image-20210428221145969

1.5.1 登陆

点击右上角的 Sign in,会提示你对应的账号密码是什么,如下图

image-20210428221227235

  1. 第一次登录后会提醒你换admin账号的密码,我这里设置成admin/admin
  2. 记得勾选Allow anonymous users to access the server

1.5.2 更改端口号

此端口可在 sonatype-work\nexus3\etc\nexus.propertis 文件里更改

image-20210428221108222

至此,nexus3就安装完成了

2. 搭建

2.1 主要Type介绍

  • group:
    • Public Repositories,仓库组
  • hosted:
    • 3rd party:无法从公共仓库获得的第三方发布版本的构件仓库,比如oracle jar包等(版权保护无法从官网免费下载)
    • Snapshot:用于存放快照版本(Snapshots)的仓库
    • Releases:用于存放稳定版本的jar和war或者插件的仓库
  • proxy:
    • Central:用来代理maven中央仓库中发布版本构件的仓库
    • Apache Snapshots:用了代理ApacheMaven仓库快照版本的构件仓库

后面创建测试组件的时候,会进一步了解到它们的作用

2.2 创建物理存储库

  1. 进到管理界面,可以看到默认已经有一个名为default的存储库

    image-20210428222243777

  2. 填写必要条件,点击Create blob store即可

    image-20210428222435070

    image-20210428222457224

2.3 创建Proxy

现在我们的私有仓库是空的,设置我们的proxy连向阿里云仓库地址。当在私有仓库中拿不到对应的jar包,则会去proxy对应的仓库拉取jar包。

image-20210428222700532

image-20210428222720193

image-20210428223023951

image-20210428223103646

image-20210428223121501

2.4 创建hosted

用于:当我们要将本地jar包通过maven命令上传到私有库,我们这里会创建两个hosted:Release + Snapshot。

2.4.1 release hosted

在这里插入图片描述
在这里插入图片描述

2.4.2 snapshot hosted

在这里插入图片描述

2.5 创建group

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

2.6 创建用户

在这里插入图片描述
在这里插入图片描述

2.7 小结

至此,目前我们需要的都创建完了,结果如图:

在这里插入图片描述

3. 使用

3.1 项目目录介绍

我们有两个模块:

  • ModuleA:
    • 用于演示拉取阿里云仓库的依赖;
    • 用于演示上传ModuleA的jar包到我们的私有库中;
  • ModuleB
    • 用于演示从私有库中拉取ModuleA的jar包下来;

3.2 拉取阿里云仓库的依赖

3.2.1 修改settings.xml

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

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<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">
    <pluginGroups></pluginGroups>
    <proxies></proxies>
    <!--http://www.cnblogs.com/sxdcgaq8080/p/7580964.html 有详细教程-->
    <!--就是配置maven本地仓库的地址为自定义的地址-->
    <localRepository>D:\mvnRepository\repository\repository\</localRepository>
    <!--nexus服务器-->
    <servers>
        <server>
            <id>release</id>
            <username>TEST</username>
            <password>TEST</password>
        </server>

        <server>
            <id>snapshot</id>
            <username>TEST</username>
            <password>TEST</password>
        </server>
    </servers>
    <!--组资源库的url地址  id和name自定义,mirrorOf的值设置为central,写死的-->
    <mirrors>
        <mirror>
            <id>nexus</id>
            <name>nexus repository</name>
            <url>http://localhost:8081/repository/TEST_GROUP/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
    </mirrors>
</settings>

mirror的地址在这里找:

在这里插入图片描述

3.2.2 查看当前生效的settings.xml

命令:mvn help:effective-settings

在这里插入图片描述

3.2.3 往ModuleA引入本地仓库没有的jar包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-batch</artifactId>
    <version>2.4.5</version>
</dependency>

执行命令:mvn clean install

在这里插入图片描述

3.2.4 查看私有库的情况

回到nexus3 UI上,进到TEST_GROUP中,可以看到对应的jar包也已经存在库里了

在这里插入图片描述

3.3 上传ModuleA至私有库

3.3.1 更改pom.xml配置

添加下面配置,里面的id与settings.xml中server的id一致

<distributionManagement>
    <snapshotRepository>
        <id>snapshot</id>
        <name>Nexus Snapshot Repository</name>
        <url>http://localhost:8081/repository/TEST_SNAPSHOT_HOSTED/</url>
    </snapshotRepository>
    <repository>
        <id>release</id>
        <name>Nexus Release Repository</name>
        <url>http://localhost:8081/repository/TEST_RELEASE_HOSTED/</url>
    </repository>
</distributionManagement>

完整的pom.xml

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

    <artifactId>ModuleA</artifactId>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
            <version>2.4.5</version>
        </dependency>
    </dependencies>

    <distributionManagement>
        <snapshotRepository>
            <id>snapshot</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://localhost:8081/repository/TEST_SNAPSHOT_HOSTED/</url>
        </snapshotRepository>
        <repository>
            <id>release</id>
            <name>Nexus Release Repository</name>
            <url>http://localhost:8081/repository/TEST_RELEASE_HOSTED/</url>
        </repository>
    </distributionManagement>
</project>

3.3.2 ModuleA中添加常量

public class StringConstant {
    public static final String TEST_STRING = "TEST";
}

3.3.3 deploy snapshot版本

执行命令:mvn deploy

在这里插入图片描述

3.3.3.1 查看snapshot hosted

在这里插入图片描述

3.3.4 deploy release版本

将ModuleA中的version从1.0-SNAPSHOT改成1.0,再deploy

3.3.4.1 查看release hosted

在这里插入图片描述

3.4 ModuleB引入ModuleA依赖

3.4.1 pom.xml

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

    <artifactId>ModuleB</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>ModuleA</artifactId>
            <version>3.0</version> <!-- 我后面重新deploy了一个3.0的,所以这里写的是3.0,不是1.0 -->
        </dependency>
    </dependencies>
</project>

3.4.2 执行install命令

在这里插入图片描述

3.5 小结

目前使用到的功能就这些,后面有用到其他的功能,再补充。

参考

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值