简体   繁体   English

在构建Maven时使用多个本地存储库

[英]Using multiple local repositories while building Maven

How can I configure Maven to use several local repositories while building one package? 在构建一个软件包时,如何配置Maven以使用多个本地存储库?

I have two folders, ~/.m2/repository (default for Maven) and ~/local_repo_tmp and want to implement following: 我有两个文件夹, ~/.m2/repository (Maven的默认设置)和~/local_repo_tmp ,想要实现以下内容:

If Maven can't find something inside of ~/local_repo_tmp it has to download it from remote repository and put to ~/.m2/repository , otherwise it has to put the copy from ~/local_repo_tmp to ~/.m2/repository . 如果Maven在~/local_repo_tmp内找不到东西,则必须从远程存储库下载并放入~/.m2/repository ,否则必须将~/local_repo_tmp的副本放入~/.m2/repository

I've tried to add following to my pom.xml : 我尝试将以下内容添加到我的pom.xml

<repositories>
      <repository>
          <id>firstrepo</id>
          <name>repo</name>
          <url>file:///Users/myusername/local_repo_tmp</url>
      </repository>
</repositories>

But it seems that it doesn't work, because it takes same time for following: 但是似乎不起作用,因为执行以下操作需要花费相同的时间:

  1. When build my project when both ~/local_repo_tmp and ~/.m2/repository are empty 当构建我的项目时, ~/local_repo_tmp~/.m2/repository都为空
  2. When ~/local_repo_tmp is copy of ~/.m2/repository previous state, but ~/.m2/repository is empty ~/local_repo_tmp~/.m2/repository以前状态的副本,但是~/.m2/repository为空时

What do I do wrong ? 我做错了什么?

Basically, you have the right approach. 基本上,您拥有正确的方法。 You set up a second "remote repository" and allow both of them to populate the "local repository" typically found in the ~/.m2/ directories 您设置了第二个“远程存储库”,并允许它们都填充通常在~/.m2/目录中找到的“本地存储库”

That said, there are a few subtle differences between a local and a remote repository. 也就是说,本地和远程存储库之间存在一些细微的差异。 Simply copying your ~/.m2/repository/ directory into the locally installed "remote" repository will likely not get you the results you desire. 仅将~/.m2/repository/目录复制到本地安装的“远程”存储库中可能不会获得所需的结果。

You probably need to log into a remote repo, or at least browse it through a web interface to see the differences between it an the local repo. 您可能需要登录到远程存储库,或至少通过Web界面浏览它,以查看远程存储库与本地存储库之间的差异。 Basically there are subtle changes in the local repo that make more sense in helping determine if the local cache is older than the remote. 基本上,本地存储库中有一些细微的变化,这些变化在帮助确定本地缓存是否比远程缓存更早时更有意义。 For your "local remote" you need to emulate the file setup of a remote repo, not a copy / clone of your local. 对于“本地远程”,您需要模拟远程存储库的文件设置,而不是本地的副本/克隆。

The order you list your remote repositories will control which repository you receive the artifact from. 您列出远程存储库的顺序将控制您从哪个存储库接收工件。 If it is found in the first repository, as long as you are on a modern Maven, you will get the package from the first repo. 如果在第一个存储库中找到它,只要您在现代Maven上,您都将从第一个存储库中获取软件包。 Otherwise, you'll get the package from the second repo. 否则,您将从第二个存储库中获取软件包。

Sometimes people want a local repo cache of the remote repo they work with. 有时人们想要他们使用的远程仓库的本地仓库缓存。 In that case I'd recommend setting up a local Artifactory instance, and having it configured as a caching mirror of the upstream repos. 在那种情况下,我建议设置一个本地Artifactory实例,并将其配置为上游仓库的缓存镜像。

It's been a long time since I did what you are asking, but I can confirm that I got it working. 自从我按照您的要求进行了很长时间以来,但是我可以确认我已经成功了。 I can't remember one of the details, but I remember approaching it from both file URLs and ssh URLs. 我不记得其中一个细节,但是我记得从文件URL和ssh URL都使用它。 I remember that one of the two worked better for my purpose, but am unsure which one and why. 我记得两者之一可以更好地达到我的目的,但是不确定哪一个以及为什么。

Good Luck! 祝好运!

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

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