Introduction to Maven Repository

nilan avtar
Nilanchala Panigrahy 馃尡

Maven uses a centralized repository of software libraries called the Maven Repository to manage and download project dependencies.

There are three types of Maven Repositories:

  • Local repository
  • Central repository
  • Remote repository 

Local Maven Repository:

The local repository is a directory on the developer's machine where all the dependencies for a specific project are stored.

When a you attempt to build the maven project, Maven first checks checks for the required dependencies in the local repository.

If the dependency is not available, it is downloaded from a remote repository and cache it in the local repository for future use. This reduces the build time when you build your project very often.

Change the Local Maven Repository path

The local repository in Maven is usually located in the user's home directory under the ~/.m2 folder.

However, you can change the path to the local repository by modifying the settings.xml file, which is located in the conf directory of your Maven installation.

Here are the steps to change the Local Maven Repository path:

  1. Locate the settings.xml file in the conf directory of your Maven installation.
  1. Open the settings.xml file in a text editor.
  1. Search for the <localRepository> tag in the file. If the tag is not present, you can add it to the file.
  1. Modify the value of the <localRepository> tag to the desired location. For example, if you want to change the local repository to a folder named my-local-repo in the D:\ drive, the tag's value would be <localRepository>D:\my-local-repo</localRepository>.
  1. Save the changes to the settings.xml file.

Once you have saved the changes to the settings.xml file, the new location will be used for all future Maven builds.

Note that, any dependencies that were previously downloaded to the old local repository will not be automatically moved to the new location, so you may need to either download again or copy them manually if required.

Central Maven Repository:

The Central Repository is the default remote repository used by Maven.

It contains a vast collection of open-source libraries and dependencies, making it a one-stop shop for most projects. When Maven needs to download a dependency, it first looks in the local repository and then in the Central Repository.

The central maven repository is managed by Sonatype. There are almost all java libraries that exist in it.

When building a project, if it can not find any dependent jars in the local maven repository it will search in the central maven repository and download those JARs if found. You can also search for an artifact in  if you want to download the artifact鈥檚 pom or jars.

To search/browse in the central repository you can use:

Remote Maven Repository:

A remote repository is a repository of dependencies located on a remote server, typically managed by a third-party organization. Maven can be configured to use multiple remote repositories, and dependencies can be downloaded from these repositories as needed.

You can also publish your own artifacts to a remote repository for use by other projects.

Here's an example of how to use a remote Maven repository in a Java project:

  • Open your project's pom.xml file in a text editor.
  • Add a new <repositories> section to the file, if it doesn't exist already. For example:
        <name>Repository Name</name>
        <url>your remote repository url</url>

In this example, we have added two remote repositories - the Central Repository and JCenter. These repositories are identified by a unique id, which is used to reference them in the project's dependencies.

  • Now you can add dependencies to your project that are hosted in these remote repositories. For example:

In this example, we are adding two dependencies to the project - JUnit and Guava. JUnit is hosted in the Central Repository, while Guava is hosted in JCenter.

Order of Dependency Search in Maven

The order in which Maven searches for dependencies in the local and remote repositories is as follows:

  1. First, Maven looks for the dependency in the local repository. If the dependency is not found, it proceeds to the next step.
  1. Next, Maven looks at the remote repositories in the order they are defined in the pom.xml file. If the dependency is found in a remote repository, it is downloaded and stored in the local repository for future use.
  1. If the dependency is not found in any of the remote repositories, Maven will report a build error.

So, in summary, the dependency search order in Maven is first the local repository, followed by the remote repositories in the order they are defined in the pom.xml file.

In summary, the local repository is where Maven stores project-specific dependencies, while the remote repository is where Maven can retrieve dependencies from third-party organizations. The central repository is the default remote repository used by Maven, which provides access to a vast collection of open-source libraries and dependencies.

Sharing is caring!

Did you like what wrote? Thank them for their work by sharing it on social media.