Java Programming Tutorials

Java programming tutorials with many code examples!

Maven use company repository

Maven use company repository (Nexus)

In this post we’ll show how to configure Maven to download project libraries from your company’s repository/mirror instead of the Internet.

In most cases, when developing software in corporations, you would like to download all project libraries from a repository (Nexus) located in corporate network, which is much faster than from the Internet.

Mirror repository in settings.xml

Configuration for all your Maven projects can be done in ~/.m2/settings.xml (.m2 directory located in your home/user directory):

<?xml version="1.0" encoding="utf-8"?>
<settings>
  <!-- other configuration if any -->
  <mirrors>
    <!-- you can setup more mirrors, but usually it's one -->
    <mirror>
      <id>your-company-mirror</id>
      <name>Your Company Mirror</name>
      <url>http://maven.your-company.com/content/groups/all_repos</url>
      <mirrorOf>external:*</mirrorOf>
    </mirror>
  </mirrors>
  <!-- other configuration if any -->
</settings>

Just use the address of your company’s Nexus repository and make sure to set path (here /content/groups/all_repos) to what is used in your company’s Nexus.

Maven mirrorOf syntax

The key part is configuration is mirrorOf. It allows to select, using ids or wildcards, which repositories should be mirrored:

  • *
    Tells Maven that all libraries should be taken from company’s repository.
  • external:*
    Fetch all from company repo, except those on the localhost and from files (sometimes libs are in a project’s subdirectory).
  • repoId1,repoId2
    Use company repository instead of repositories with these ids. For others download normally.
  • *,!repoId
    Mirror everything except (note the exclamation mark) repository with id repoId.

Now, when you run maven package or just import a project in your IDE, then Maven will download packages from the URL provided above.

Voila! :-)

Share with the World!