项目中如何使用私服 Nexus

1. 概述

让 Maven 项目使用 Nexus 作为远程仓库有两种方式。

  • 单个项目:在项目的 pom.xml 中进行更改,让单个项目使用 Nexus 仓库;

  • 所有项目:通过修改 Maven的 配置文件 settings.xm l进行更改,让所有项目都使用 Nexus仓库;

2. 单个项目配置

在项目的 pom.xml 文件的 dependencies 标签之前添加

<repositories>
        <repository>
            <id>nexus3</id>
            <name>nexus3</name>
            <!-- 注意:这里是 Nexus 服务上的仓库地址 -->
            <url>http://127.0.0.1:8081/repository/maven-public/</url>
            <!-- Release版本则代表稳定的版本 -->
            <releases>
                <enabled>true</enabled>
            </releases>
            <!-- Snapshot版本代表不稳定、尚处于开发中的版本,默认关闭,需要手动启动 -->
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

    <!-- 指定插件仓库 -->
    <pluginRepositories>
        <pluginRepository>
            <id>nexus</id>
            <name>nexus</name>
            <url>http://127.0.0.1:8081/repository/maven-public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

仓库地址的获取方式如下

http://127.0.0.1:8081/repository/maven-public/

506ec25c13ff87fa04b05241cbff33fd.png

点开上图的仓库

2a95bc5a7db52b2138c015014d240a93.png

比如在项目中添加 junit 依赖,可以看到下载的连接是本地的 Nexus 服务的地址

5f52c6b5c06cae54ba57f3f552d9a0f0.png

同样,可以在 Nexus 服务看到 Junit 已经下载下来。

14cbf5a73024e7625264e297c941c6ab.png

3. 所有项目

在 Maven 安装目录找到如下文件

C:\devtools\maven-3.5.0\conf\settings.xml

编辑settings.xml文件

  • profiles节点添加如下内容

<profile>    
    <!--profile 的 id--> 
    <id>dev</id>    
   <repositories>    
      <repository>   
           <!--仓库 id,repositories 可以配置多个仓库,保证 id 不重复--> 
          <id>nexus</id>    
          <!--仓库地址,即 nexus 仓库组的地址--> 
          <url>http://127.0.0.1:8081/repository/maven-public/</url>    
          <!--是否下载 releases 构件--> 
          <releases>    
                <enabled>true</enabled>    
          </releases>    
          <!--是否下载 snapshots 构件--> 
          <snapshots>    
               <enabled>true</enabled>    
          </snapshots>    
      </repository>    
  </repositories>   
     <pluginRepositories>   
         <!-- 插件仓库,maven 的运行依赖插件,也需要从私服下载插件 --> 
         <pluginRepository>   
              <!-- 插件仓库的 id 不允许重复,如果重复后边配置会覆盖前边 --> 
             <id>public</id>   
             <name>Public Repositories</name>   
             <url>http://127.0.0.1:8081/repository/maven-public/</url>   
        </pluginRepository>   
   </pluginRepositories>   
</profile>
  • 打开activeProfiles节点注释,添加如下内容

<activeProfiles> 
    <activeProfile>dev</activeProfile> 
</activeProfiles>

比如在项目中添加 log4j 依赖,可以看到下载的连接是本地的 Nexus 服务的地址

94a5081740bb5ce221bea87f10bd5f56.png

同样,可以在 Nexus 服务看到 log4j 已经下载下来。

852dcc02192581d0e38342512182233c.png

2fd1816d7438dce8e746a3d3d7127874.png

分享&在看

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BUG弄潮儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值