简体   繁体   中英

Using multiple local repositories while building Maven

How can I configure Maven to use several local repositories while building one package?

I have two folders, ~/.m2/repository (default for Maven) and ~/local_repo_tmp and want to implement following:

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 .

I've tried to add following to my 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
  2. When ~/local_repo_tmp is copy of ~/.m2/repository previous state, but ~/.m2/repository is empty

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

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.

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. 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. 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.

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. I remember that one of the two worked better for my purpose, but am unsure which one and why.

Good Luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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