简体   繁体   English

从不同的存储库下载2个相同的jar时,Maven如何选择使用哪个.jar

[英]How does Maven choose which .jar to use when it downloads 2 identical jars from different repositories

Sorry for the long description: 很抱歉,详细说明:

Background : I have 2 projects, let's say A and B. Builds in Project A deploy to Nexus Repository Manager OSS 2.12.0 repo A, and builds in Project B deploy to Nexus repo B. My maven project needs to use .jars from both repo A and repo B. 背景 :我有2个项目,例如A和B。项目A中的构建部署到Nexus Repository Manager OSS 2.12.0存储库A,项目B中的构建部署到Nexus存储库B。我的Maven项目需要同时使用.jars回购A和回购B.

Therefore, I configured my maven.settings file to use 2 different Nexus repositories (Having 2 mirrors). 因此,我将maven.settings文件配置为使用2个不同的Nexus存储库(具有2个镜像)。

Now, I see that when I compile my maven project, it will go through repo A, search for the jar, then go through repo B, search for the jar. 现在,我看到编译我的Maven项目时,它将通过存储库A,搜索jar,然后通过存储库B,搜索jar。 When it finds the jar, it will download it. 找到罐子后,将下载它。

Problem : I used to use Repo B for both project A and project B's jars (the owner of the repo kicked me out). 问题 :我曾经将回购B用于项目A和项目B的罐子(回购的所有者将我踢了出去)。 Therefore, I now have the same 0.0.1-SNAPSHOT of a component in both repo A, and repo B. 因此,我现在在回购A和回购B中都具有相同的0.0.1-SNAPSHOT组件。

Maven now downloads both these snapshots during compilation. 现在,Maven会在编译期间下载这两个快照。

Question : How does maven resolve which .jar to use if both have the same name (ex: componentName-0.0.1-SNAPSHOT) ? 问题 :如果maven具有相同的名称(例如:componentName-0.0.1-SNAPSHOT),maven如何解析要使用的.jar? I see that in my local ./m2 repository, there's a resolver-status file, but I'm unsure of how it resolves between both the jars. 我看到在我的本地./m2存储库中,有一个resolver-status文件,但是我不确定在两个jar之间如何解析。 Ideally, I would want maven to use the most recently updated (based on timestamp) jar. 理想情况下,我希望Maven使用最近更新的(基于时间戳)jar。

Short answer: Whichever repository comes first in the effective POM that is computed by maven. 简短的答案:哪个存储库在由maven计算的有效POM中排在最前面。 (You can view your effective pom by running mvn help:effective-pom .) (您可以通过运行mvn help:effective-pom来查看您的有效mvn help:effective-pom 。)

Long answer: 长答案:

The way maven resolves SNAPSHOT artifacts is (roughly) as follows: maven解决SNAPSHOT工件的方式大致如下:

1) Go through each dependency that has a SNAPSHOT version in <dependencies> 1)遍历<dependencies>中具有SNAPSHOT版本的每个依赖项

2) Walk through all repositories listed in <snapshotRepositories> 2)浏览<snapshotRepositories>列出的所有存储库

3) For each SNAPSHOT repository, check if there is a maven-metadata.xml file for that dependency's group, package name, and version. 3)对于每个SNAPSHOT存储库,请检查是否存在有关该依赖项的组,程序包名称和版本的maven-metadata.xml文件。

For instance, given the package coordinates: io.packagecloud:jake:4.0-SNAPSHOT , it would look for /io/packagecloud/jake/4.0-SNAPSHOT/maven-metadata.xml in each configured repository. 例如,给定包坐标: io.packagecloud:jake:4.0-SNAPSHOT ,它将在每个已配置的存储库中查找/io/packagecloud/jake/4.0-SNAPSHOT/maven-metadata.xml

4) If this file is found, then use it to find the latest artifact to dwonload. 4)如果找到了此文件,则使用它来找到要加载的最新工件。 Taking a look at an example maven-metadata.xml : 看一下示例maven-metadata.xml

$ curl -L https://packagecloud.io/capotej/snapshot_example/maven2/io/packagecloud/jake/4.0-SNAPSHOT/maven-metadata.xml

<?xml version="1.0" encoding="UTF-8"?>
<metadata modelVersion="1.1.0">
  <groupId>io.packagecloud</groupId>
  <artifactId>jake</artifactId>
  <version>4.0-SNAPSHOT</version>
  <versioning>
    <snapshot>
      <timestamp>20161003.234325</timestamp>
      <buildNumber>2</buildNumber>
    </snapshot>
    <lastUpdated>20161003234325</lastUpdated>
    <snapshotVersions>
      <snapshotVersion>
        <extension>jar</extension>
        <value>4.0-20161003.234325-2</value>
        <updated>20161003234325</updated>
      </snapshotVersion>
      <snapshotVersion>
        <extension>pom</extension>
        <value>4.0-20161003.234325-2</value>
        <updated>20161003234325</updated>
      </snapshotVersion>
    </snapshotVersions>
  </versioning>
</metadata>

We can see that the <lastUpdated> timestamp is 20161003234325 . 我们可以看到<lastUpdated>时间戳是20161003234325 Using this timestamp, we can then look it up in <snapshotVersions> to get the full value, in this case it's: 4.0-20161003.234325-2 . 使用此时间戳,我们可以在<snapshotVersions>查找它以获得完整的值,在这种情况下,它是: 4.0-20161003.234325-2

5) Now maven knows the full, unique version of this SNAPSHOT, and it can download it from the repository: 5)现在,maven知道此SNAPSHOT的完整且唯一的版本,并且可以从存储库中下载它:

curl -I https://packagecloud.io/capotej/snapshot_example/maven2/io/packagecloud/jake/4.0-SNAPSHOT/jake-4.0-20161003.234325-2.jar

Hope this answers your question! 希望这能回答您的问题!

Related: 有关:

Maven repository support Maven存储库支持

SBT SNAPSHOT Deploys and fatjar Support SBT SNAPSHOT部署并提供Fatjar支持

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM