十年JAVA搬砖路——Maven仓库优先级以及和maven镜像之间的关系

**

Maven仓库优先级

**
本地>项目>全局存>中央

Maven设置本地仓库位置
在 conf文件夹下settings.xml文件中找到 localRepository
添加

  <localRepository>你需要存放的地址</localRepository>

Maven项目仓库设置
在POM文件中添加

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://com.xxx.xxx/repository/maven-public/</url>
    </repository>
</repositories>

Maven全局存仓库设置
在settings 中添加

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>https://com.xxx.xxx/repository/maven-public/</url>
        </repository>
    </repositories>

Maven中央存仓库设置

在settings 中添加

<repositories>
  <repository>
    <id>central</id>
    <url>https://repo.maven.apache.org/maven2</url>
  </repository>
</repositories>

Maven镜像

在中配置,如配置阿里镜像

  <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>

如何理解仓库和镜像的关系?

Maven来取jar包时,优先从仓库中拉取,如果仓库不存在或者仓库有对应的镜像配置,着从对应镜像中去拉取jar包,达到提速的目的。
所谓的对应的配,主要是 central 中的值决定,比如central,则对应仓库中的center,而maven默认的仓库就是central 这样我们配置阿里镜像,用central 。这样当某个jar包需要从center仓库拉取时,就通过代理,变成了走阿里镜像加速。

通俗的说,如果Maven配置了Maven中央仓库的镜像,那么在需要从中央仓库获取依赖时,Maven会根据配置的镜像地址去镜像源查找。如果镜像源上存在所需的依赖,Maven将直接从镜像源下载依赖文件。如果镜像源上不存在所需的依赖,Maven会退回到中央仓库本身去查找并下载依赖文件。这样可以加快依赖的下载速度,并减轻中央仓库的负载压力。

猜你喜欢

转载自blog.csdn.net/weixin_43485737/article/details/132355008