Question

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

Was it helpful?

Solution

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.

OTHER TIPS

Proxying and caching artifacts from other public repositories is (or should be) indeed built-in in any decent enterprise repository manager. In a corporate environment, development teams should be able to develop and build their software even when a public repository is down. This is a very basic use case and one of the reasons to use a corporate repository (not being dependent on public resources that you don't control).

Once the repository manager is installed and configured, you'll need to configure maven to check the repository instead of the public repositories. To do this, edit your mirror settings (as pointed out by Rich) in your ~/.m2/settings.xml. Refer to the section 3.2. Configuring Maven to Use a Single Nexus Group of the Nexus Book for more details (the configuration applies to any repository manager).

In a corporate environment, I prefer to force Maven to use a single repository by having it mirror all repository requests. To achieve this, set mirrorOf to *.

<settings>
  ...
  <mirrors>
    <mirror>
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <name>Maven Repository Manager running on repo.mycompany.com</name>
      <url>http://repo.mycompany.com/proxy</url>
    </mirror>
  </mirrors>
  ...
</settings>

In Artifactory, you can use the "Home->Maven Settings" page to download a maven settings that will enforce maven to use only Artifactory virtual repositories you choose. This combined with repository reference cleanup in Virtual repositories will make sure you will never query maven central.

Your issue with NetBeans may be solved by enforcing the repo declaration in your Maven installation. The last time I used NetBeans Maven support, I also specified a project level settings.xml file to cover every cases.

Side Note: Using the mirrorOf feature is very rude and may lead to strange behavior.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top