Maven项目中配置仓库地址

447 阅读1分钟

仓库之间的优先级

  1. 本地仓库
  2. 项目中pom文件配置的仓库
  3. setting文件中配置的mirror

项目中配置依赖和插件仓库

  1. 项目根pom中增加依赖仓库配置
<repositories>
    <repository>
        <id>huaweicloud</id>
        <name>huaweicloud maven</name>
        <url>https://mirrors.huaweicloud.com/repository/maven/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
        </snapshots>
    </repository>
</repositories>

配置说明:

  • repository节点可以有多个
  • 每个仓库的id是唯一的
  • 每个仓库都可配置releasessnapshots策略,默认启用releases
  • 如果将id设置为central将会覆盖默认的中央仓库
  1. 项目根pom中增加插件仓库配置
<pluginRepositories>
    <pluginRepository>
        <id>aliyun-plugin</id>
        <url>https://maven.aliyun.com/repository/public</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

配置说明:

  • pluginRepository节点可以有多个
  • 每个仓库的id是唯一的
  • 每个仓库都可配置releasessnapshots策略,默认启用releases

注意:为什么要单独设定插件的仓库,因为maven中构建的命令,如:cleanintall等下周时走的都是插件仓库,如不指定,会默认从中央仓库中下载,国内会很慢。

其他问题处理

1. maven报错Blocked mirror for repositories解决方案

错误原因:maven从3.7版本开始,仅支持https协议,所有http协议都会不禁止下载。

解决方法:降级到3.6版本以下或升级私服支持https协议

2. maven构建时报错:xxx.jar was cached in the local repository

错误原因:本地仓库中已缓存有此jar包,且生成了此jar包结尾为lastUpdated的文件,坑点在于无论jar包是否下载成功,都会生成此文件,到此后续无法下载最新的依赖。

解决方法

  • 删掉本地仓库此jar包的目录。【已验证】
  • 依赖仓库中增加always来强制每次都更新依赖库。【未验证】
  • 加上-U指令来强制刷新本地仓库。【未验证】