Maven settings.xml文件中的Mirror是什么? [英] What is Mirror in Maven settings.xml file?

查看:593
本文介绍了Maven settings.xml文件中的Mirror是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我错了,请纠正我,但是使用镜像将所有流量重定向到特定的存储库URL并阻止其他所有内容(包括Maven中央存储库).

Correct me if I'm wrong, but a Mirror is used to redirect all traffic to a specific repository URL and block everything else (including Maven central repo).

现在,如果我有一个http://a.com:8081的镜像和一个http://b.com:8081的存储库,该怎么办.第二个URL是否会被使用?我认为答案是否定的.您可以拥有多个镜子吗?

Now what if I have a Mirror to http://a.com:8081 and a repository to http://b.com:8081. Would the second URL ever get used? I assume the answer is a No. Can you have multiple Mirrors?

推荐答案

如果我错了,请纠正我,但是使用镜像将所有流量重定向到特定的存储库URL并阻止其他所有内容(包括Maven中央存储库).

Correct me if I'm wrong, but a Mirror is used to redirect all traffic to a specific repository URL and block everything else (including Maven central repo).

这并不完全正确. Maven通过在已定义的存储库中进行搜索来下载工件.这些存储库在项目的POM或Maven设置中定义.但是,如镜像文档中所述:

This is not entirely correct. Artifacts are downloaded by Maven by searching them inside defined repositories. Those repositories are defined in project's POM or in the Maven settings. But, as specified in the Mirror documentation:

但是,您可能希望在不更改项目文件的情况下为特定存储库使用备用镜像.

However, you may want to use an alternative mirror for a particular repository without changing the project files.

让我们以文档中的示例为例并对其进行评论:

Let's take the example from the documentation and comment on it:

<mirrors>
  <mirror>
    <id>UK</id>
    <name>UK Central</name>
    <url>http://uk.maven.org/maven2</url>
    <mirrorOf>central</mirrorOf>
  </mirror>
</mirrors>

这定义了一个单一的镜像,当Maven想要从Maven中央存储库中获取依赖项时将使用该镜像.假设您有2个当前存储库,分别是Maven Central和自定义存储库A.发生的情况如下:

This defines a single mirror that will be used when Maven will want to fetch a dependency from the Maven Central Repository. Let's say you have 2 current repositories, which are Maven Central and a custom repo A. What happens is the following:

  1. 您要声明对工件的依赖性;
  2. Maven将在您定义的存储库中寻找该依赖关系,因此它将在存储库A和Maven Central中寻找它;
  3. 让我们说它以A开头:它将像您期望的那样命中为该存储库定义的URL.然后让我们说它在那里找不到;
  4. 然后它将在Maven Central内部查看.但是,Maven将注意到为该存储库配置了一个镜像.它不会在repo1.maven.org命中Maven Central.相反,它将从uk.maven.org(镜像配置元素中定义的URL)获取它.
  1. You are declaring a dependency to an artifact;
  2. Maven will look for that dependency inside your defined repositories, so it will look for it inside the repository A and Maven Central;
  3. Let's say it starts with A: it will hit the URL defined for that repository, like you would expect. Then let's say it wasn't found there;
  4. It will then look inside Maven Central. However, Maven will notice that a mirror was configured for that repository. It will not hit Maven Central at repo1.maven.org. Instead, it will fetch it from uk.maven.org, which is the URL defined in the mirror configuration element.

如该示例所示,

镜像仅适用于某些存储库,这些存储库由<mirrorOf>配置元素定义.在前一种情况下,我们有<mirrorOf>central</mirrorOf>,其意思是"Mirror Maven Central".该元素可以具有各种值,而文档

As this example shows, a mirror only applies to certain repositories, which are defined with the <mirrorOf> configuration element. In the previous case, we had <mirrorOf>central</mirrorOf> which means "Mirror Maven Central". This element can have various values, and the documentation gives examples:

示例:

  • * =一切
  • external:* =不在本地主机上且不是基于文件的所有内容.
  • repo,repo1 = repo或repo1
  • *,!repo1 =除repo1之外的所有内容:
  • * = everything
  • external:* = everything not on the localhost and not file based.
  • repo,repo1 = repo or repo1
  • *,!repo1 = everything except repo1:


现在,如果我有一个http://a.com:8081的镜像和一个http://b.com:8081的存储库,该怎么办.第二个URL是否会被使用?我认为答案是否定的.

Now what if I have a Mirror to http://a.com:8081 and a repository to http://b.com:8081. Would the second URL ever get used? I assume the answer is a No.

上面的结果表明答案并不是严格的否定的.当Maven尝试从其镜像的存储库之一获取依赖项时,将使用http://a.com:8081的镜像.如果确实将存储库镜像到http://b.com:8081,则将不会对http://b.com:8081进行任何请求(它们将被重定向到http://a.com:8081);但是如果没有,Maven将像往常一样继续从http://b.com:8081获取依赖项.

The above shows that the answer is not strictly no. The mirror to http://a.com:8081 will be used when Maven will try to fetch a dependency from one of the repository that it mirrors. If it indeed mirrors the repository to http://b.com:8081 then no requests will ever be made to http://b.com:8081 (they will be redirected to http://a.com:8081); but if it doesn't, Maven will continue to fetch dependencies from http://b.com:8081 like usual.

可以有多个镜像吗?

Can you have multiple Mirrors?

是的,可以.

这篇关于Maven settings.xml文件中的Mirror是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆