R – Can Nexus/Artifactory store a copy of an internet Maven artifact

artifactorymaven-2netbeansnexusrepository

I would like to have Nexus (or Artifactory, we're not decided yet) store a copy of an artifact when it is downloaded from a public repository on the internet (like Maven Central).

Basically, if you don't have the jar in your local repo and the repo manager doesn't have it either, I want to ask the repo manager for the jar, have it send it to me, and store it both in the repo manager and in my local repo. Then, when another person asks the repo manager for the same jar, it sends it to them from the repo manager and they save it in their local repo, without needing to hit Maven central.

This sounds like how it should work out of the box, but I don't see it. I can see the artifacts on Maven central through the repo manager, but when I use Netbeans to add a dependency to my project, it downloads it straight from Maven central (apparently). Nothing gets cached in the repo manager (as far as I can see).

Best Answer

Nexus has the concept of proxy repository which does almost exactly what you describe. See the Proxying Public Repositories section of the book for details.

Artifactory has a similar feature called Virtual repositories.

Proxying and caching a remote public repository can speed up your builds by reducing redundant downloads over the public internet. If a developer in your organization needs to download version 2.5 of the Spring Framework and you are using Nexus, the dependencies (and the dependency's dependencies) only need to be downloaded from the remote repository once. With a high-speed connection to the Internet this might seem like a minor concern, but if you are constantly asking your developers to download hundreds of megabytes of third-party dependencies, the real cost savings are going to be the time it takes Maven to check for new versions of dependencies and to download dependencies over the public internet.

You can stop your Maven install from ever trying to connect to Central by setting up your repository manager as a mirror of central, requests to central will then be directed to your repository manager. For example add the following to the <mirrors> section of Maven's settings.xml to redirect requests to central to your repository manager:

<mirror>
  <id>central-proxy</id>
  <mirrorOf>central</mirrorOf>
  <url>http://myrepo/nexus/content/groups/public</url>
</mirror>

I tend to set up a repository group containing several remote repositories. The group acts as a single repository as far as my Maven install is concerned, but collates artifacts from several remote repositories.