使用nexus搭建maven私库

什么是nexus?

nexus是一个maven仓库管理器,使用nexus可以快速便捷的搭建自己的maven私有仓库。

docker安装nexus

拉取镜像
docker pull sonatype/nexus3
后台执行镜像
docker run -d -p 8081:8081 --name nexus-dev
查看nexus容器是否启动

查看nexus

访问本地的nexus

在浏览器url地址中输入localhost:8081,如果此时未能成功加载,等待几秒后再尝试刷新浏览器。

nexus启动页面

成功访问后,点击右上角sigin,按照提示从指定目录下获取admin账号的密码。

登陆界面

添加常用代理源

选择maven2代理方式

选择代理方式

添加阿里云代理源

阿里云代理源

设置常用代理到maven-public

maven-public是一个聚合仓库,当从这个仓库中获取依赖时,它会从成员列表中依次往下遍历,从对应的成员仓库中获取依赖。

maven-public

设置release仓库可重复发布

配置release仓库可重复发布之后,可以重复发布同一个版本号的依赖。这里大家可以根据实际情况勾选是否启用。

release仓库

本地maven配置

配置好私有仓库之后,我们需要修改本地的maven配置和项目中的pom文件才能够跟私有仓库进行互动操作。

修改settings.xml

这里需要注意settings.xml文件的优先级(用户级别>全局设置>自定义路径),具体的配置看下面的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>D:\Program Files\Maven\repository</localRepository>

  <pluginGroups>
  </pluginGroups>

  <proxies>
  </proxies>

  <servers>
      <server>
          <id>nexus</id>
          <username>admin</username>
          <password>chenhao.123</password>
      </server>
  </servers>

  <mirrors>
      <mirror>
          <id>nexus</id>
          <mirrorOf>*</mirrorOf>
          <url>http://localhost:8081/repository/maven-public/</url>
      </mirror>
  <profiles>
      <profile>
          <id>jdk-1.8</id>
          <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>
      </profile>
      <profile>
          <id>nexus</id>
          <repositories>
              <repository>
                  <id>maven-public</id>
                  <url>http://localhost:8081/repository/maven-public/</url>
                  <releases>
                      <enabled>true</enabled>
                  </releases>
                  <snapshots>
                      <enabled>false</enabled>
                  </snapshots>
              </repository>
              <repository>
                  <id>maven-snapshots</id>
                  <url>http://localhost:8081/repository/maven-snapshots/</url>
                  <releases>
                      <enabled>false</enabled>
                  </releases>
                  <snapshots>
                      <enabled>true</enabled>
                  </snapshots>
              </repository>
              <repository>
                  <id>maven-releases</id>
                  <url>http://localhost:8081/repository/maven-releases/</url>
                  <releases>
                      <enabled>true</enabled>
                  </releases>
                  <snapshots>
                      <enabled>false</enabled>
                  </snapshots>
              </repository>
          </repositories>
          <pluginRepositories>
              <pluginRepository>
                  <id>maven-public</id>
                  <url>http://localhost:8081/repository/maven-public/</url>
                  <releases>
                      <enabled>true</enabled>
                  </releases>
                  <snapshots>
                      <enabled>false</enabled>
                  </snapshots>
              </pluginRepository>
              <pluginRepository>
                  <id>maven-snapshots</id>
                  <url>http://localhost:8081/repository/maven-snapshots/</url>
                  <releases>
                      <enabled>false</enabled>
                  </releases>
                  <snapshots>
                      <enabled>true</enabled>
                  </snapshots>
              </pluginRepository>
              <pluginRepository>
                  <id>maven-releases</id>
                  <url>http://localhost:8081/repository/maven-releases/</url>
                  <releases>
                      <enabled>true</enabled>
                  </releases>
                  <snapshots>
                      <enabled>false</enabled>
                  </snapshots>
              </pluginRepository>
          </pluginRepositories>
      </profile>

  </profiles>
    <activeProfiles>
        <activeProfile>jdk-1.8</activeProfile>
        <activeProfile>nexus</activeProfile>
    </activeProfiles>
</settings>

创建一个maven项目A作为依赖提供者

这里在idea创建一个maven模板的项目即可,没有什么特别的操作。

修改A项目的pom.xml
    <distributionManagement>
        <repository>
            <id>nexus</id>
            <name>releases Repository</name>
            <url>http://127.0.0.1:8081/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus</id>
            <name>snapshots Repository</name>
            <url>http://127.0.0.1:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
在A项目中创建一个Math作为测试
package com.chenhao.util;

public class Math {
    public static int add(int a, int b){
        return a+b;
    }
}
将项目A的jar包发布到私有仓库

在idea项目右边的maven工具来中,点击deploy按钮,查看控制台输出

deploy

查看nexus上是否存在这个依赖

nexus depencies

创建一个maven项目B作为依赖使用者

这里创建一个项目B来作为依赖的使用者,并且需要注意的是,在项目A中使用了deploy操作,此时已经将依赖上传到本地仓库。所以此时应该将本地仓库中的依赖删除。

package com.chenhao;
import com.chenhao.util.Math;
public class Main {
    public static void main(String[] args) {
        System.out.println(Math.add(1, 1));
    }
}

修改项目B的pom.xml
 <dependencies>
        <dependency>
            <groupId>com.chenhao</groupId>
            <artifactId>example</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
运行项目B

run project b

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
搭建Maven服可以使用Sonatype Nexus,它是一个功能强大的仓管理器,可以帮助我们创建和管理Maven服。下面是基于Nexus搭建Maven服的详细步骤: 1. 准备环境:首先,确保你已经安装了Java环境,并且可以正常运行MavenNexus软件。 2. 下载和解压缩Nexus软件:从Sonatype官方网站下载最新版本的Nexus软件,并解压缩到合适的目录中。 3. 启动Nexus使用命令行窗口进入Nexus软件的目录,然后执行 `./bin/nexus start` 命令来启动Nexus服务器。 4. 访问Nexus控制台:在浏览器中输入 `http://localhost:8081` 访问Nexus控制台,默认用户名和密码都是 `admin`,登录成功后,可以看到Nexus的管理界面。 5. 创建Maven:在Nexus控制台中,点击左侧菜单的 `Repositories`,然后点击 `Create repository` 来创建新的Maven。根据需要选择 `Hosted repository` 或者 `Proxy repository`,然后填写相应的配置信息,如仓名称、URL、布局等。 6. 配置Maven项目:在你的Maven项目的pom.xml文件中添加Nexus服的配置信息,包括ID、URL等,用来指定将Maven构建输出部署到Nexus中。 7. 部署和使用使用 `mvn deploy` 命令将项目打包并部署到Nexus服中,当其他开发人员或者CI/CD服务器需要下载依赖时,只需要修改项目的settings.xml文件,指定Nexus服的URL和凭据即可。 8. 其他设置:你还可以在Nexus控制台里进行其他设置,比如用户管理、访问控制、仓代理等。 总结一下,使用Nexus搭建Maven服非常简单,只需要几个基本的步骤即可完成。通过搭建Maven服,我们可以有效地管理和共享项目的依赖包,提升团队的开发效率和项目的可维护性。这对于生产环境中的项目非常重要。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值