maven配置让 settings文件中配置的仓库和pom.xml配置的仓库都生效

本文讲述了在Maven项目中遇到的依赖包下载问题,解决方法在于正确配置settings文件中仓库镜像,确保POM中定义的仓库与settings中的镜像规则匹配。关键在于理解`mirrorOf`标签对仓库选择的影响。
摘要由CSDN通过智能技术生成

问题描述

在使用 maven 项目下载依赖包的时候,总提示jar包在仓库中找不到,后发现是由于project 工程中 pom 配置的公司自己的 maven仓库地址未被识别到,只读取了 settings 配置文件中的地址。

问题排查:

maven的setting文件配置信息如下:

<mirrors>   
    <mirror> 
    <id>Central</id> 
    <url>https://repo1.maven.org/maven2</url> 
    <mirrorOf>central</mirrorOf> 
    </mirror>     
            
    <mirror>
      <id>aliNexus</id>
      <mirrorOf>*</mirrorOf> 
      <name>aliNexus</name>
      <url>https://maven.aliyun.com/nexus/content/groups/public/</url>
    </mirror>             

  </mirrors>

pom.xml中的配置:

    <repositories>
        <repository>
            <id>local-nexus</id>
            <url>${maven.host}/repository/maven-public/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

为什么明明在POM中配置了仓库地址,但是下载包的时候却不识别呢,原因就在于 settings 文件中 mirror 的配置问题,如下所示:

 

   <mirror>
      <id>aliNexus</id>
      <mirrorOf>*</mirrorOf> 
      <name>aliNexus</name>
      <url>https://maven.aliyun.com/nexus/content/groups/public/</url>
    </mirror>  

    mirrorOf 标签,这个标签如果设置为 *,就说明整个工程只能使用settings中配置的地址,这就是问题的根源。

 

解决办法

  在settings文件的mirrorof 标签中内容改成 !local-nexus,* ,如下:

   <mirror>
      <id>aliNexus</id>
      <mirrorOf>!local-nexus,*</mirrorOf> 
      <name>aliNexus</name>
      <url>https://maven.aliyun.com/nexus/content/groups/public/</url>
    </mirror>  

!local-nexus 意思是把 local-nexus 这个仓库排除在外,依然把公司的仓库作为唯一仓库,这样就可以使 settings文件中配置的仓库和pom.xml配置的仓库都生效。

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Maven 的 `settings.xml` 文件是用来配置 Maven 的全局设置和本地仓库等信息的,它位于 Maven 安装目录下的 `conf` 目录。 下面是一个简单的 `settings.xml` 文件示例: ```xml <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>/path/to/local/repo</localRepository> <mirrors> <mirror> <id>aliyun</id> <mirrorOf>*</mirrorOf> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </mirror> </mirrors> <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <env>dev</env> </properties> <repositories> <repository> <id>central</id> <url>https://repo.maven.apache.org/maven2</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </profile> <profile> <id>prod</id> <properties> <env>prod</env> </properties> <repositories> <repository> <id>central</id> <url>https://repo.maven.apache.org/maven2</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </profile> </profiles> <activeProfiles> <activeProfile>dev</activeProfile> </activeProfiles> </settings> ``` 上述示例,`<localRepository>` 指定了本地 Maven 仓库的路径,`<mirrors>` 配置了阿里云的镜像,`<profiles>` 配置了两个环境的配置信息,`<activeProfiles>` 指定了当前使用的环境。 需要注意的是,`settings.xml` 文件配置是全局生效的,如果有多个项目使用同一份配置文件,那么它们都会受到该文件配置影响。如果需要针对某个项目单独配置,可以在项目的 `pom.xml` 文件添加相应的配置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值