iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Maven配置单仓库与多仓库的实现(Nexus)
  • 307
分享到

Maven配置单仓库与多仓库的实现(Nexus)

Maven配置单仓库Maven配置多仓库Maven 单仓库 多仓库 2023-01-16 12:01:36 307人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录单仓库多仓库多仓库配置单仓库 当只配置一个仓库时,操作比较简单,直接在Maven的settings.xml文件中进行全局配置即可,以阿里云的镜像为例: <mirrors&g

单仓库

当只配置一个仓库时,操作比较简单,直接在Maven的settings.xml文件中进行全局配置即可,以阿里云的镜像为例:

<mirrors>
    <mirror>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>Http://maven.aliyun.com/nexus/content/groups/public/</url>
        <mirrorOf>central</mirrorOf>
    </mirror>
</mirrors>

只用新增一个mirror配置即可。要做到单一仓库,设置mirrorOf到*。

mirrorOf中配置的星号,表示匹配所有的artifacts,也就是everything使用这里的代理地址。上面的mirrorOf配置了具体的名字,指的是repository的名字。

镜像配置说明:

1、id: 镜像的唯一标识;

2、name: 名称描述;

3、url: 地址;

4、mirrorOf: 指定镜像规则,什么情况下从镜像仓库拉取。其中, *: 匹配所有,所有内容都从镜像拉取;

external:*: 除了本地缓存的所有从镜像仓库拉取;

repo,repo1: repo或者repo1,这里的repo指的仓库ID;

*,!repo1: 除了repo1的所有仓库;

多仓库

先看看我的settings配置文件

<?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:\idea\Maven\apache-maven-3.8.4\maven-repo</localRepository>
    <pluginGroups>
    </pluginGroups>
    <proxies>
    </proxies>
    <!-- 私服信息   -->
    <servers>
    <server>
      <id>kd-nexus</id>
      <username>账号</username>
      <passWord>密码</password>
    </server>
  <server>
      <id>nexus</id>
      <username>账号</username>
      <password>密码</password>
    </server>
    </servers>
    <!--  仓库信息  -->
    <mirrors>
    <mirror>
        <!--  与上面的id对应  -->
      <id>kd-nexus</id>
      <mirrorOf>*</mirrorOf>
      <name>kd-nexus maven</name>
      <url>http://ip:8081/repository/maven-public/</url>
    </mirror>
    <mirror>
        <!--  与上面的id对应  -->
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <name>nexus maven</name>
      <url>http://ip:8081/repository/maven-public/</url>
    </mirror>
    <mirror>
      <id>alimaven</id>
      <mirrorOf>central</mirrorOf>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
    </mirror>
  </mirrors>
    <!--  项目配置文件信息  -->
    <profiles>
        <profile>
            <id>jdk-1.8</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>
        </profile>
    </profiles>
    <!--  启动那个配置  -->
    <activeProfiles>
        <activeProfile>jdk-1.8</activeProfile>
    </activeProfiles>
</settings>

刚开始我以为这样就可以了,如果在kd-nexus找不到jar包,会自动去nexus里面找jar包,可现实是maven只会在kd-nexus中找,找不到就报错,根本不会在之后的仓库中取寻找 因为:

虽然mirrors可以配置多个子节点,但是它只会使用其中的一个节点,即**默认情况下配置多个mirror的情况下,只有第一个生效,**只有当前一个mirror

无法连接的时候,才会去找后一个;而我们想要的效果是:当a.jar在第一个mirror中不存在的时候,maven会去第二个mirror中查询下载,但是maven不会这样做!

多仓库配置

那么针对多仓库的配置是否再多配置几个mirror就可以了?如此配置并不会生效。

正确的操作是在profiles节点下配置多个profile,而且配置之后要激活。

<profiles>
    <profile>
      <id>boundlessgeo</id> 
      <repositories>
        <repository>
          <id>boundlessgeo</id> 
          <url>https://repo.boundlessgeo.com/main/</url> 
          <releases>
            <enabled>true</enabled>
          </releases> 
          <snapshots>
            <enabled>true</enabled> 
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </repository>
      </repositories>
    </profile>
    <profile>
      <id>aliyun</id> 
      <repositories>
        <repository>
          <id>aliyun</id> 
          <url>http://maven.aliyun.com/nexus/content/groups/public/</url> 
          <releases>
            <enabled>true</enabled>
          </releases> 
          <snapshots>
            <enabled>true</enabled> 
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </repository>
      </repositories>
    </profile> 
    <profile>
      <id>maven-central</id> 
      <repositories>
        <repository>
          <id>maven-central</id> 
          <url>http://central.maven.org/maven2/</url> 
          <releases>
            <enabled>true</enabled>
          </releases> 
          <snapshots>
            <enabled>true</enabled> 
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </repository>
      </repositories>
    </profile>
<profiles>

通过配置activeProfiles子节点激活:

<activeProfiles>
    <activeProfile>boundlessgeo</activeProfile>
    <activeProfile>aliyun</activeProfile>
    <activeProfile>maven-central</activeProfile>
</activeProfiles>

打包时,勾选所使用的profile即可。如果使用Maven命令打包执行命令格式如下:

mvn -Paliyun ...

1.如果aliyun仓库的id设置为central,则会覆盖maven里默认的远程仓库。

2.aliyun的仓库也可以不用配置,直接在mirrors标签内配置一个镜像仓库,mirrors镜像仓库mirrorOf的值设置为central,则也可以实现覆盖默认的仓库。

好了你既然看到这里了,那么上诉可能基本没效果,问就是我导包也没成功,现提供两个解决方案

前提

idea每次修改完settings之后,需要重新点击一下,更新一下文件配置

一、挨个导包

首先kd-nexus先导一下包

    <mirrors>
    <mirror>
      <id>kd-nexus</id>
      <mirrorOf>*</mirrorOf>
      <name>kd-nexus maven</name>
      <url>http://ip:8081/repository/maven-public/</url>
    </mirror>
    <mirror>
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <name>nexus maven</name>
      <url>http://ip:8081/repository/maven-public/</url>
    </mirror>
  </mirrors>

之后修改maven配置,导一下nexus的包

    <mirrors>
    <mirror>
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <name>nexus maven</name>
      <url>http://ip:8081/repository/maven-public/</url>
    </mirror>
    <mirror>
      <id>kd-nexus</id>
      <mirrorOf>*</mirrorOf>
      <name>kd-nexus maven</name>
      <url>http://ip:8081/repository/maven-public/</url>
    </mirror>
  </mirrors>

二、手动导包(解决一切导包问题,就是过程很麻烦)

本地maven仓库:如果连这个都不清楚,看nexus完全没意义

就是localRepository标签中的文件地址
<localRepository>D:\idea\Maven\apache-maven-3.8.4\maven-repo</localRepository>

不知道Nexus怎么搭建的家人们,可以看一下这篇文章:使用Nexus搭建Maven私服教程(附:nexus上传、下载教程)

到此这篇关于Maven配置单仓库与多仓库的实现(Nexus)的文章就介绍到这了,更多相关Maven

--结束END--

本文标题: Maven配置单仓库与多仓库的实现(Nexus)

本文链接: https://www.lsjlt.com/news/177869.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作