深入比较几种maven仓库的优先级



深入比较几种maven仓库的优先级

在maven中主要有以下几种仓库的设置,本地仓库,settings里面profile中设置的仓库,mirror仓库,pom文件中的repository。下面将和大家一起探讨和比较下这几种仓库的优先级别。

这里我们有三个仓库 :

远程222.197.xxx仓库

localhost镜像,是我自己在本机搭建的一个仓库

nexus仓库,是nexus官方的仓库

首先考虑这样一种情况:maven本地仓库中拥有该包,而中央仓库、mirror仓库、profile仓库、pom中仓库均不含该包。我们可以看到maven直接首先从本地仓库中找到该包,编译成功。

 

由此可以看出,本地仓库拥有最高的优先级。

接下来,我们继续剩下的仓库的优先级,(下面所有情况,都默认本地不拥有我们需要的包)剩下的仓库都是远程仓库,这里我们设定mirror设置的镜像仓库是中央仓库的镜像。那么就可以把mirror当成中央仓库,因为其已经被mirror替换了。

这次的场景为:pom文件中定义的仓库(repository)(即远程:222.197.xxx)拥有该包,而mirror(localhost镜像)和profile(nexus仓库)中的仓库不拥有该包。进行测试:

 

从这个结果可以看出,maven首先调用了profile中的仓库,其次找到了pom文件中设置的仓库。而mirror没有出现。我们这里可以初步推测优先级别为:profile > pom > mirror,为了验证我们的判断,我们需要在进行一次测试来验证这个结果。

场景如下:mirror中拥有该包(222.197),而pom(localhost镜像)和profile(nexus仓库)中均不含有该包。进行测试:

 

实际情况的确验证了我们的猜想。那么我们得出这几种maven仓库的优先级别为:

本地仓库 >profile > pom中的repository > mirror

注意:我也验证过这样的情况,将mirror这样设置:

<mirror>

<id>huacloud-central</id>

<mirrorOf>*</mirrorOf>

<name>name-of-this</name>

<url>http://222.197.XXXXXX/nexus/content/groups/public/</url>

 </mirror>

即是表示该仓库地址为所有仓库的镜像,那么这个时候,maven会忽略掉其他设置的各种类型仓库仓库,只在mirror里面找。所以建议不要这样设置,他将导致pom文件中、pforile里面的仓库设置都失效。

随便科普一下几种仓库的设置吧:

pom中的repository:

<repositories>

<repository>

<id>dsdf</id>

<releases>

<enabled>true</enabled>

</releases>

<url>http://222.197.XXXXXX/nexus/content/groups/public/</url>

</repository>

 </repositories>

profile中的仓库是在maven的设置文件(maven安装目录/conf/settings.xml)

<profile>

 <id>nexus</id>

 <repositories>

 <repository>

 <id>sonatype-forge</id>

 <url>http://repository.sonatype.org/content/groups/forge/</url>

 <releases>

 <enabled>true</enabled>

 </releases>

 <snapshots>

 <enabled>true</enabled>

 </snapshots>

 </repository>

 </repositories>

 <pluginRepositories>

 <pluginRepository>

 <id>sonatype-forge</id>

 <url>http://repository.sonatype.org/content/groups/forge/</url>

 <releases>

 <enabled>true</enabled>

 </releases>

 <snapshots>

 <enabled>true</enabled>

 </snapshots>

 </pluginRepository>

 </pluginRepositories>

 </profile>

 </profiles>

 

<!-- 使用下面代码来激活profile-->

 

<activeProfiles>

<activeProfile>nexus</activeProfile>

 </activeProfiles>

深入比较几种maven仓库的优先级

在maven中主要有以下几种仓库的设置,本地仓库,settings里面profile中设置的仓库,mirror仓库,pom文件中的repository。下面将和大家一起探讨和比较下这几种仓库的优先级别。

这里我们有三个仓库 :

远程222.197.xxx仓库

localhost镜像,是我自己在本机搭建的一个仓库

nexus仓库,是nexus官方的仓库

首先考虑这样一种情况:maven本地仓库中拥有该包,而中央仓库、mirror仓库、profile仓库、pom中仓库均不含该包。我们可以看到maven直接首先从本地仓库中找到该包,编译成功。

 

由此可以看出,本地仓库拥有最高的优先级。

接下来,我们继续剩下的仓库的优先级,(下面所有情况,都默认本地不拥有我们需要的包)剩下的仓库都是远程仓库,这里我们设定mirror设置的镜像仓库是中央仓库的镜像。那么就可以把mirror当成中央仓库,因为其已经被mirror替换了。

这次的场景为:pom文件中定义的仓库(repository)(即远程:222.197.xxx)拥有该包,而mirror(localhost镜像)和profile(nexus仓库)中的仓库不拥有该包。进行测试:

 

从这个结果可以看出,maven首先调用了profile中的仓库,其次找到了pom文件中设置的仓库。而mirror没有出现。我们这里可以初步推测优先级别为:profile > pom > mirror,为了验证我们的判断,我们需要在进行一次测试来验证这个结果。

场景如下:mirror中拥有该包(222.197),而pom(localhost镜像)和profile(nexus仓库)中均不含有该包。进行测试:

 

实际情况的确验证了我们的猜想。那么我们得出这几种maven仓库的优先级别为:

本地仓库 >profile > pom中的repository > mirror

注意:我也验证过这样的情况,将mirror这样设置:

<mirror>

<id>huacloud-central</id>

<mirrorOf>*</mirrorOf>

<name>name-of-this</name>

<url>http://222.197.XXXXXX/nexus/content/groups/public/</url>

 </mirror>

即是表示该仓库地址为所有仓库的镜像,那么这个时候,maven会忽略掉其他设置的各种类型仓库仓库,只在mirror里面找。所以建议不要这样设置,他将导致pom文件中、pforile里面的仓库设置都失效。

随便科普一下几种仓库的设置吧:

pom中的repository:

<repositories>

<repository>

<id>dsdf</id>

<releases>

<enabled>true</enabled>

</releases>

<url>http://222.197.XXXXXX/nexus/content/groups/public/</url>

</repository>

 </repositories>

profile中的仓库是在maven的设置文件(maven安装目录/conf/settings.xml)

<profile>

 <id>nexus</id>

 <repositories>

 <repository>

 <id>sonatype-forge</id>

 <url>http://repository.sonatype.org/content/groups/forge/</url>

 <releases>

 <enabled>true</enabled>

 </releases>

 <snapshots>

 <enabled>true</enabled>

 </snapshots>

 </repository>

 </repositories>

 <pluginRepositories>

 <pluginRepository>

 <id>sonatype-forge</id>

 <url>http://repository.sonatype.org/content/groups/forge/</url>

 <releases>

 <enabled>true</enabled>

 </releases>

 <snapshots>

 <enabled>true</enabled>

 </snapshots>

 </pluginRepository>

 </pluginRepositories>

 </profile>

 </profiles>

 

<!-- 使用下面代码来激活profile-->

 

<activeProfiles>

<activeProfile>nexus</activeProfile>

 </activeProfiles>

<think>我们正在使用Maven,需要查看本地仓库的地址。Maven的本地仓库是存储项目依赖的默认位置。我们可以通过几种方式查看本地仓库地址。 1. 查看Maven的配置文件(settings.xml) 2. 查看Maven项目的默认配置 3. 使用Maven命令 下面详细说明每种方法:</think>要查看 Maven 的本地仓库地址,可以通过以下几种方法实现: ### 1. 查看 Maven 配置文件(最可靠方法) Maven 的本地仓库地址定义在 `settings.xml` 配置文件中: #### 步骤: 1. 打开 Maven 的 `settings.xml` 文件 - 路径通常位于: - 全局配置:`${M2_HOME}/conf/settings.xml` - 用户配置:`${user.home}/.m2/settings.xml` 2. 查找 `<localRepository>` 标签 ```xml <settings> <!-- 默认未指定时使用 ~/.m2/repository --> <localRepository>/path/to/your/local/repo</localRepository> </settings> ``` #### 示例代码(Java 获取路径): ```java import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import java.io.File; import java.io.FileReader; import java.io.IOException; public class MavenLocalRepoFinder { public static void main(String[] args) { try { // 1. 用户级 settings.xml File userSettings = new File(System.getProperty("user.home"), ".m2/settings.xml"); // 2. 全局 settings.xml (需要知道 M2_HOME) String m2Home = System.getenv("M2_HOME"); File globalSettings = m2Home != null ? new File(m2Home, "conf/settings.xml") : null; // 读取配置文件 String localRepoPath = findLocalRepo(userSettings); if (localRepoPath == null && globalSettings != null) { localRepoPath = findLocalRepo(globalSettings); } // 默认路径 if (localRepoPath == null) { localRepoPath = new File(System.getProperty("user.home"), ".m2/repository").getAbsolutePath(); } System.out.println("Maven Local Repository: " + localRepoPath); } catch (Exception e) { e.printStackTrace(); } } private static String findLocalRepo(File settingsFile) throws IOException, XmlPullParserException { if (settingsFile.exists()) { MavenXpp3Reader reader = new MavenXpp3Reader(); try (FileReader fileReader = new FileReader(settingsFile)) { org.apache.maven.model.Model model = reader.read(fileReader); if (model.getLocalRepository() != null) { return model.getLocalRepository(); } } } return null; } } ``` ### 2. 使用 Maven 命令查看 在命令行中执行以下命令: ```bash mvn help:evaluate -Dexpression=settings.localRepository -q -DforceStdout ``` **命令解释**: - `help:evaluate`:执行 Maven 的表达式求值 - `-Dexpression=settings.localRepository`:指定查询本地仓库路径 - `-q`:安静模式(减少输出) - `-DforceStdout`:直接输出结果(避免日志干扰) **输出示例**: ``` /Users/username/.m2/repository ``` ### 3. 查看环境变量 Maven 会按以下优先级确定仓库位置: 1. `settings.xml` 中显式配置的 `<localRepository>` 2. 环境变量 `M2_REPO` 3. 默认路径:`${user.home}/.m2/repository` 在终端中查看: ```bash # Linux/macOS echo $M2_REPO # Windows echo %M2_REPO% ``` ### 4. 通过 IDE 查看(IntelliJ IDEA 示例) 1. 打开 **Preferences/Settings** → **Build, Execution, Deployment** → **Build Tools** → **Maven** 2. 查看 **Local repository** 字段 ![](https://resources.jetbrains.com/help/img/idea/2023.3/idea_maven_local_repo.png) ### 5. 直接检查默认路径 如果未配置特殊路径,默认仓库位置为: - **Windows**:`C:\Users\<用户名>\.m2\repository` - **Linux/macOS**:`/home/<用户名>/.m2/repository` ### 验证仓库内容 确认找到的路径确实是 Maven 仓库: ```bash # 查看仓库目录结构 ls -l ~/.m2/repository # 典型目录结构 repository/ ├── antlr ├── ch ├── commons-codec ├── commons-io ├── junit └── org ``` ### 关键路径总结 | 配置方式 | 路径示例 | 优先级 | |---------|---------|-------| | 用户 settings.xml | `~/.m2/settings.xml` 中定义 | 最高 | | 全局 settings.xml | `$M2_HOME/conf/settings.xml` 中定义 | 中 | | 环境变量 | `M2_REPO` 的值 | 次高 | | 默认路径 | `~/.m2/repository` | 最低 |
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值