Nexus(Maven私服)详解

我们看到仓库的类型主要有proxy、hosted、group类型。

proxy是代理的远程仓库。

hosted是指本地或者内部项目仓库。

group只是一个仓库组,它包含其他的几个仓库。

Releases是指发行版本(本地或者内部项目)。

Snapshots是指正在构建的版本(本地或者内部项目)。

Central是指中央仓库(远程中央仓库下载的构件放入此处)。

 

我们可以再项目的pom.xml文件中配置:

  <repositories>
     <repository>
        <id>nexus</id>
        <name>nexus-repo</name>
        <url>http://localhost:8081/nexus/content/repositories/central/</url>
     </repository>
  </repositories>

这样我们的项目就可以从私服的中央仓库中下载构件,但是如果我们需要用到releases或者snapshots仓库,就需要增加额外的配置,此时就需要用到我们的group仓库,我们先查看一下group仓库

 

group仓库默认包含四个仓库,因此我们只需要配置group仓库就可以将其他几个仓库都配置上去

   <repositories>
     <repository>
        <id>nexus</id>
        <name>nexus-repo</name>
        <url>http://localhost:8081/nexus/content/groups/public/</url>
     </repository>

  </repositories>

 

在项目的pom.xml文件配置,只能对当前的项目起作用,因此它是一个局部的配置,我们可以再maven的settings.xml 中配置,那么它就起到一个全局的作用,在profiles节点下配置:

 <profile>
      <id>nexusProfile</id>
        <repositories>
<repository>
<id>nexus</id>
<name>nexus-repo</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</repository>
       </repositories>
    </profile>

      

此时的profile并未激活,我们还需要配置:

    <activeProfiles>
    <activeProfile>nexusProfile</activeProfile>  //其值指向profile的id
  </activeProfiles> 

在我们的配置文件中可能有很多个profile,每一个profile只有激活之后才生效。

 

这个时候当我们在我们的项目中加入依赖,项目通过我们的私服去访问远程中央仓库,将下载到的构件放入到我们的私服中,但是当我们通过nexus stop 命令关闭私服时,我们再次在项目中加入依赖我们会发现,项目会先访问私服,但是发现私服关闭了,就会从远程中央仓库去下载我们需要的构件,这样的情况并不是我们想要的 ,因为我们希望通过私服去间接的访问远程中央仓库,那么这个时候的解决办法就是配置镜像,每一个仓库都有一个唯一的id,远程中央仓库的id是central,上面配置的仓库的id为nexus,当我们的项目要访问central或nexus仓库时,我们让它镜像到其他的仓库上,不管你访问哪一个仓库,但是只能访问指定的镜像仓库,我们可以再settings.xml中的mirrors节点下配置:

       <mirror>
      <id>nexusId</id>
      <mirrorOf>central,nexus</mirrorOf>  //在这里我们通常使用*代替central,nexus,表示任何的访问都会镜像到这里
      <name>Human Readable Name for this Mirror.</name>
      <url>http://localhost:8081/nexus/content/groups/public/</url>
    </mirror>

 

那么这样就实现了,只能通过私服去访问远程中央仓库。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值