1. 前言

maven是作为Javer日常开发中必不可少的工具,然而很多人对于它的应用也只是仅限于的几个性能。

前几天在应用一个依赖总是说找不到该依赖,然而在地方仓库中确实存在该构建。这个问题让我很困惑,忽然发现对于maven这个优良构建工具的应用也只是浮于外表。痛定思痛,于是就有了这篇简短的总结。

对于maven依赖程序的搜寻,网上七嘴八舌,那么假相到底是怎么样的呢?

2. 筹备

依据咱们的开发教训,应用maven能够配置多个仓库,先来看看咱们最相熟的一个。

关上咱们的settings.xml文件,一开始是一个最纯净的配置文件:

能够看到,只是配置了一个镜像地址和一个本地仓库的门路,这也是咱们第一次应用maven时大多数教程中会提到的一点-批改mirror为国内的一个地址。

当初咱们创立一个maven的工程,而后看看它的依赖查找程序是怎么的?

我创立了一个mvn-dep文件夹,在这外面创立我的项目所须要的文件,为了简略,我就应用maven cli来搭建一个简略的我的项目,应用的命令如下:

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp  -DinteractiveMode=true -DgroupId=com.watertreestar -DartifactId=mvn-dep -Dversion=1.0 -Dpackage=com.watertreestar

创立好当前咱们的我的项目构造如下:

<img align="left" height="300" src="https://cdn.jsdelivr.net/gh/watertreestar/CDN@master/picimage-20210826222721918.png">

接下来看一下不同的配置下依赖查找的门路

3. 依赖查找摸索

应用上一步创立的我的项目,并在下面的根底settings.xml配置文件上做批改,察看依赖查找的优先级。

在操作之前,咱们在我的项目的pom.xml中曾经蕴含了一个依赖就是fastjson

 <dependency>   <groupId>com.alibaba</groupId>   <artifactId>fastjson</artifactId>   <version>1.2.78</version></dependency>

咱们先确保本地仓库中该版本的junit不存在,应用rm命令来删除它:

rm -rf ~/.m2/repository/com/alibaba/fastjson

3.1 不批改的状况

咱们应用下面的setings.xml文件,不做任何批改,执行mvn compile命令,输入如下:

能够看出,maven是从咱们配置的central镜像-阿里云镜像中拉取依赖

3.2 没有配置地方仓库镜像

当初咱们把settings文件中的mirror配置删除,就成了一个赤裸裸的配置:

<?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">    <pluginGroups></pluginGroups>    <proxies></proxies>        <localRepository>/Users/young/.m2/repository</localRepository></settings>

这种状况下,依赖会从哪儿获取呢

能够看到,是从默认的地方仓库中查找和下载依赖。

依据下面的后果能够看到优先级:

特定仓库reporitory的镜像mirror > settings中配置的仓库repository

3.3 我的项目配置仓库repository

  1. 当初咱们把仓库的配置还原到最后的状态,如下:
<?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">   <pluginGroups></pluginGroups>   <proxies></proxies>   <localRepository>/Users/young/.m2/repository</localRepository>   <mirrors>      <mirror>         <id>settings-mirror</id>         <url>https://maven.aliyun.com/repository/public</url>         <mirrorOf>central</mirrorOf>      </mirror>   </mirrors></settings>
  1. 而后在第二节中创立的我的项目pom.xml文件中增加一个repository配置,咱们应用了一个sonatype的maven仓库

    以后的我的项目pom.xml配置如下:

<dependencies>   <dependency>     <groupId>com.alibaba</groupId>     <artifactId>fastjson</artifactId>     <version>1.2.78</version>  </dependency></dependencies><repositories>  <repository>    <id>pom_repository</id>    <name>sonatype</name>    <url>https://repository.sonatype.org/content/groups/public/</url>    <releases>      <enabled>true</enabled>    </releases>    <snapshots>      <enabled>true</enabled>    </snapshots>  </repository></repositories>

执行mvn compile命令,输入如下:

能够看出依赖从我的项目pom.xml配置的pom-repository仓库中查找并下载。

3.4 配置全局profile中的repository

在maven配置文件settings.xml中减少profile配置

应用maven compile -Psettings-profile

能够看出,依赖从settings.xml中咱们配置settings-profile的repository中下载的

settings_profile_repository > pom_repositories

3.5 配置我的项目profile的repository

3.5.1 激活pom中的profile和setting中的profile

全局settings.xml中的配置不见,在pom.xml中减少profile配置:

<profiles>  <profile>    <id>pom-profile</id>    <repositories>      <repository>        <id>pom-profile-repository</id>        <name>maven2</name>        <url>https://repo.maven.apache.org/maven2/</url>        <releases>          <enabled>true</enabled>        </releases>        <snapshots>          <enabled>true</enabled>        </snapshots>      </repository>    </repositories>  </profile></profiles>

删除本地仓库的fastjson依赖,而后执行mvn compile -Psettings-profile,pom-profile

输入:

咱们同时激活了settings-profile和pom-profile,然而最终依赖的下载是从settings-profile中配置的仓库下载的。

settings-profile > pom-profile

3.5.2 只是激活pom中的profile

如果只是激活pom中的profile,也就是应用mvn compile -Ppom-profile,输入后果如下:

能够看出,最终依赖的下载是从pom中配置的profile-repository中下载的

pom-profile-repository > pom-repository

3.6 local repository

因为之前的步骤中咱们曾经下载jar到了local仓库,为了测试最初一步,咱们就不必在执行rm -rf ~/.m2/repository/com/alibaba/fastjson来删除本地的依赖了。

我这里应用了 mvn compile -Ppom-profile来做测试,输入的后果如下:

能够看到,没有从任何近程仓库中下载依赖

local > 所有近程仓库

4. 总结

从下面一系列的验证中能够总结出依赖查找的优先级:

local-repo > settings-profile-repository > pom-profile-repository > pom-repository > central

了解maven查找的优先级,能够帮忙咱们在工作和学习中解决一些依赖找不到的谬误。

最初,举荐几个能够应用的maven仓库:

  • https://repo.maven.apache.org...
  • https://repo1.maven.org/maven2/
  • https://maven.aliyun.com/repo...