Java 如何让 Maven 停止尝试检查来自 maven-central-repo 的某个组的工件的更新?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1348603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:29:46  来源:igfitidea点击:

How can I get Maven to stop attempting to check for updates for artifacts from a certain group from maven-central-repo?

javamaven-2repository

提问by Tim Gilbert

I'm working on a fairly big Maven project. We have probably around 70 or so individual artifacts, which are roughly split into two libraries of shared code and maybe ten applications which use them. All of these items live in the namespace com.mycompany.*.

我正在做一个相当大的 Maven 项目。我们大概有大约 70 个单独的工件,它们大致分为两个共享代码库和可能使用它们的十个应用程序。所有这些项目都位于命名空间中com.mycompany.*

Most of the time we're running against snapshot builds. So to do a full build of an application, I might first build the library projects so that they are installed to my local repository (as, say, mycompany-libname-2.4-SNAPSHOT.jar).

大多数时候,我们都在针对快照构建运行。因此,要对应用程序进行完整构建,我可能首先构建库项目,以便将它们安装到我的本地存储库(例如,mycompany-libname-2.4-SNAPSHOT.jar)。

The problem is that when I then go build the applications. For some reason, Maven wants to check the main two public repositories (maven-net-repo and java-net-repo) for updates for all of the mycompany-*-SNAPSHOT.jarartifacts. Of course, they aren't found there, and everything eventually resolves back to the versions I just built to my local repository, but I'd like Maven to stop doing this because (a) it makes me feel like a bad net.citizen for constantly checking these repositories for things that will never be there, and (b) it adds some unnecessary and annoying network latency into my build process.

问题是当我去构建应用程序时。出于某种原因,Maven 想要检查主要的两个公共存储库(maven-net-repo 和 java-net-repo)以获取所有mycompany-*-SNAPSHOT.jar工件的更新。当然,在那里找不到它们,并且所有内容最终都会解析回我刚刚构建到本地存储库的版本,但我希望 Maven 停止这样做,因为 (a) 它让我觉得自己像一个糟糕的 net.citizen不断检查这些存储库中永远不会存在的东西,并且(b)它在我的构建过程中增加了一些不必要且烦人的网络延迟。

I've taken to running maven in offline mode most of the time to work around this, but that's not ideal since occasionally a dependency on a public library will be updated. So what I'm looking for is a solution which will cause Maven not to check for updates from given repositories for artifacts which meet certain criteria - in this case, I'd be happy if Maven would ignore either SNAPSHOT versions or artifacts which were in the com.mycompanynamespace.

我大部分时间都在离线模式下运行 maven 来解决这个问题,但这并不理想,因为偶尔会更新对公共库的依赖。所以我正在寻找一种解决方案,它会导致 Maven 不检查给定存储库中满足特定标准的工件的更新 - 在这种情况下,如果 Maven 忽略 SNAPSHOT 版本或工件,我会很高兴该com.mycompany命名空间。

采纳答案by Dennis Lassing

The updatePolicy tag didn't work for me. However Rich Seller mentioned that snapshots should be disabled anyways so I looked further and noticed that the extra repository that I added to my settings.xml was causing the problem actually. Adding the snapshots section to this repository in my settings.xml did the trick!

updatePolicy 标签对我不起作用。然而 Rich Seller 提到无论如何都应该禁用快照,所以我进一步查看并注意到我添加到我的 settings.xml 的额外存储库实际上导致了问题。将快照部分添加到我的 settings.xml 中的这个存储库就成功了!

<repository>
    <id>jboss</id>
    <name>JBoss Repository</name>
    <url>http://repository.jboss.com/maven2</url>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
</repository>

回答by Rich Seller

Update: I should have probably started with this as your projects are SNAPSHOTs. It is part of the SNAPSHOT semantics that Maven will check for updates on each build. Being a SNAPSHOT means that it is volatile and subject to change so updates should be checked for. However it's worth pointing out that the Maven super POMconfigures central to have snapshots disabled, so Maven shouldn't ever check for updates for SNAPSHOTs on central unless you've overridden that in your own pom/settings.

更新:我应该从这个开始,因为你的项目是快照。它是 SNAPSHOT 语义的一部分,Maven 将检查每个构建的更新。作为快照意味着它是易变的并且可能会发生变化,因此应检查更新。然而值得指出的是,Maven 超级 POM将中央配置为禁用快照,因此 Maven 不应该检查中央上的快照更新,除非您在自己的 pom/设置中覆盖了它。



You can configure Maven to use a mirrorfor the central repository, this will redirect all requests that would normally go to central to your internal repository.

您可以将 Maven 配置为对中央存储库使用镜像,这会将通常会转到中央存储库的所有请求重定向到您的内部存储库。

In your settings.xml you would add something like this to set your internal repository as as mirror for central:

在您的 settings.xml 中,您将添加如下内容以将您的内部存储库设置为中央镜像:

<mirrors>
  <mirror>
    <id>ibiblio.org</id>
    <name>ibiblio Mirror of http://repo1.maven.org/maven2/</name>
    <url>http://path/to/my/repository</url>
    <mirrorOf>central</mirrorOf>
  </mirror>
</mirrors>

If you are using a repository manager like Nexusfor your internal repository. You can set up a proxy repositoryfor proxy central, so any requests that would normally go to Central are instead sent to your proxy repository (or a repository groupcontaining the proxy), and subsequent requests are cached in the internal repository manager. You can even set the proxy cache timeout to -1, so it will never request for contents from central that are already on the proxy repository.

如果您在内部存储库中使用像Nexus这样的存储库管理器。您可以为 proxy central设置一个代理存储库,这样通常会发送到 Central 的任何请求都会发送到您的代理存储库(或包含代理的存储库组),并且后续请求会缓存在内部存储库管理器中。您甚至可以将代理缓存超时设置为 -1,因此它永远不会从中央请求已经在代理存储库中的内容。



A more basic solution if you are only working with local repositories is to set the updatePolicyfor the central repository to "never", this means Maven will only ever check for artifacts that aren't already in the local repository. This can then be overridden at the command line when needed by using the -U switch to force Maven to check for updates.

如果您只使用本地存储库,一个更基本的解决方案是将中央存储库的updatePolicy设置为“从不”,这意味着 Maven 将只检查本地存储库中不存在的工件。然后可以在需要时在命令行中通过使用 -U 开关强制 Maven 检查更新来覆盖它。

You would configure the repository (in your pom or a profile in the settings.xml) as follows:

您将按如下方式配置存储库(在您的 pom 或 settings.xml 中的配置文件中):

<repository>
  <id>central</id>
  <url>http://repo1.maven.org/maven2</url>
  <updatePolicy>never</updatePolicy>
</repository>

回答by Jeff Tsay

Also, you can use -oor --offlinein the mvn command line which will put maven in "offline mode" so it won't check for updates. You'll get some warning about not being able to get dependencies not already in your local repo, but no big deal.

此外,您可以在 mvn 命令行中使用-o--offline,它会将 maven 置于“离线模式”,因此它不会检查更新。您会收到一些关于无法获取本地存储库中尚未存在的依赖项的警告,但这没什么大不了的。

回答by thirdy

I had some trouble similar to this,

我遇到了类似的问题,

<repository>
    <id>java.net</id>
    <url>https://maven-repository.dev.java.net/nonav/repository</url>
    <layout>legacy</layout>
</repository>
<repository>
    <id>java.net2</id>
    <url>https://maven2-repository.dev.java.net/nonav/repository</url>
</repository>

Setting the updatePolicy to "never" did not work. Removing these repo was the way I solved it. ps: I was following this tutorialabout web services (btw, probably the best tutorial for ws for java)

将 updatePolicy 设置为“从不”不起作用。删除这些 repo 是我解决它的方式。ps:我正在关注有关 Web 服务的本教程(顺便说一句,可能是 ws for java 的最佳教程)

回答by joostschouten

Something that is now available in maven as well is

现在在 Maven 中也可用的东西是

mvn goal --no-snapshot-updates

or in short

或者简而言之

mvn goal -nsu

回答by Bruno Régnier

Very simple :

很简单 :

In your Super POM parent or setting.xml, use

在您的 Super POM 父级或 setting.xml 中,使用

        <repository>
        <id>central</id>
        <releases>
            <updatePolicy>never</updatePolicy>
        </releases>
        <snapshots>
            <updatePolicy>never</updatePolicy>
        </snapshots>
        <url>http://repo1.maven.org/maven2</url>
        <layout>legacy</layout>
    </repository>

It's my tips

这是我的提示