victorlzw 2023-04-14 21:27 采纳率: 50%
浏览 22

maven的setting配置问题

maven的setting配置问题
最近在学习maven的settings.xml配置时遇到了一些不理解的问题
  1. 仓库的搜索优先级。所了解到的是 本地仓库 -> 私服仓库 -> 线上的中央仓库,实际尝试的过程中,我将mirrors镜像注释掉,在profile中配置了几个阿里云仓库,实际下载的时候发现还是走的repo.maven.apache.org/maven2,也就是中央仓库,为什么没有先从私服中查找?

  2. 中央仓库的覆盖。相关博客说,仓库的<id>central</id> 就会覆盖默认的中央仓库,如上所述,可知并没有被覆盖。
    这两个问题我尝试了好多次都没有弄明白,另外假如中央仓库可以覆盖的话,直接覆盖不就好了,何必还要配置镜像呢?求解惑! 具体配置文件如下

    <?xml version="1.0" encoding="UTF-8"?>
    <settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 http://maven.apache.org/xsd/settings-1.2.0.xsd">
    <localRepository>F:/MavenRepository</localRepository>
    <pluginGroups>
    </pluginGroups>
    <proxies>
    </proxies>
    <servers>
    </servers>
    <mirrors>
     <!-- <mirror>
       <id>nexus-aliyun</id>
       <mirrorOf>central</mirrorOf>
       <name>Nexus aliyun</name>
       <url>http://maven.aliyun.com/nexus/content/groups/public</url>
     </mirror>
      -->    
    </mirrors>
    <profiles>
    <profile>
         <id>aLiCentral</id>
         <repositories>
           <repository>
             <id>central</id>
           <url>http://maven.aliyun.com/nexus/content/groups/public</url>
             <!-- <url>https://maven.aliyun.com/repository/central</url> -->
             <releases>
               <enabled>true</enabled>
             </releases>
             <snapshots>
               <enabled>true</enabled>
               <updatePolicy>always</updatePolicy>
             </snapshots>
           </repository>
         </repositories>
       </profile>
       <profile>
         <id>spring</id>
         <repositories>
           <repository>
             <id>spring</id>
             <url>https://maven.aliyun.com/repository/spring</url>
             <releases>
               <enabled>true</enabled>
             </releases>
             <snapshots>
               <enabled>true</enabled>
               <updatePolicy>always</updatePolicy>
             </snapshots>
           </repository>
         </repositories>
       </profile>
       <profile>
         <id>spring-plugin</id>
         <repositories>
           <repository>
             <id>spring-plugin</id>
             <url>https://maven.aliyun.com/repository/spring-plugin</url>
             <releases>
               <enabled>true</enabled>
             </releases>
             <snapshots>
               <enabled>true</enabled>
               <updatePolicy>always</updatePolicy>
             </snapshots>
           </repository>
         </repositories>
       </profile>
    
     <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>aLiCentral</activeProfile>
    <activeProfile>spring</activeProfile>
    <activeProfile>spring-plugin</activeProfile>
    </activeProfiles>
    </settings>
    
  • 写回答

2条回答 默认 最新

  • _Axing 2023-04-15 21:11
    关注

    可能是以下原因导致的:

    1.阿里云仓库未及时同步中央仓库
    由于 Maven 中央仓库是所有 Maven 项目都能够访问和使用的公共仓库,因此它的更新速度比较快。而像阿里云这样的私服仓库,需要与中央仓库同步才能保证最新版本的依赖库被下载。因此,如果您的 profile 中的阿里云仓库还没有及时同步最新的中央仓库,就可能无法找到最新的依赖。

    2.依赖库已在本地缓存中
    Maven 会将您已经下载的依赖库缓存在本地仓库中,以避免每次都重新下载相同的依赖。如果您曾经使用中央仓库下载过某个依赖库,那么该库就会被缓存到本地仓库中。因此,即使您现在使用阿里云仓库,Maven 也会优先从本地缓存中查找依赖库文件。如果本地缓存中已经存在最新版本的依赖库,那么就不需要再从阿里云仓库下载了。

    3.没有启用 profile
    在 Maven 中,只有在 pom.xml 或 settings.xml 文件中显式地声明了 profile,才会启用该 profile 配置的仓库。因此,在执行 Maven 命令时,请确保已经正确指定了要使用的 profile。您可以使用以下命令来指定 profile:

    mvn -P profile-name command
    

    其中,profile-name 是指要使用的 profile 名称,command 是指要执行的 Maven 命令。

    评论

报告相同问题?

问题事件

  • 修改了问题 4月14日
  • 创建了问题 4月14日

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)