Apache Maven – Local, Central and Remote Repositories

In this article, we will learn about different types of maven repositories. In simple, maven repositories contains packaged JARS of various modules

1. Maven Repositories:

Three types of Maven repository are

  1. Local Repository
  2. Central Repository
  3. Remote Repository

Maven searches dependencies in the following order

1_Apache-Maven-3-different-repositories

Source: Team BenchResources.net

1.1 Local Repository

Local repository is the local directory on the developer’s computer. Generally, when you run maven command for the first time it downloads all dependent JARS from Central/Remote repositories and store in the default location

The default location is

C:\Users\<user-name>\.m2\repository

If one wishes to change the default location to some user-defined location then you do by changing the <localRepository> element present in the settings.xml at MAVEN_HOME\conf\settings.xml

Update location with absolute path

<localRepository D:\M2_HOME\.m2\repository</localRepository>

New maven settings.xml

<!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
<localRepository D:\M2_HOME\.m2\repository</localRepository>
2_Apache-Maven-Repo-location_m2_repository

Note: For the very first time it downloads packages JARS from either Central or Remote repository, from next time onwards it picks JARS from local repository i.e.; local directory of the developer’s computer even for different project

1.2 Central Repository

Central maven repository is located on the web at http://repo1.maven.org/maven/ and it is provided by Maven community

Note: First time, maven tries to download most of the JARS from this central repository

3_Apache-Maven-Repo-location_central_repository

This site is now revamped and “Directory Browsing” is disabled at http://repo1.maven.org/maven/ and instead it re-directs to new site

This new site is much more advanced and has good browse functionality –> click View

4_Apache-Maven-Repo-location_central_repository_new_site

1.3 Remote Repository

As shown in the first figure above, if the required JARS is not available in Local repository or Central repository then maven checks if there are any remote repository is configured else it stops and throws error

For example, JBoss dependencies are configured in pom.xml

pom.xml

		<!-- RESTEasy JAX RS Implementation -->
		<dependency>
			<groupId>org.jboss.resteasy</groupId>
			<artifactId>resteasy-jaxrs</artifactId>
			<version>${resteasy.version}</version>
			<scope>${resteasy.scope}</scope>
		</dependency>

But if these JARS are not available in Central repository, and then in that case you need to tell maven to search and download these JARS from Remote repository configured in pom.xml

pom.xml

		<repositories>
			<repository>
				<id>jboss</id>
				<url>http://repository.jboss.org/maven2</url>
			</repository>
		</repositories>

2. Maven dependencies checking rules

  • Maven searches to download dependencies from Local Repository, if not found next step
  • Maven searches to download dependencies from Central Repository, if not found checks whether any remote repositories are configured then go to next step otherwise stops & throws error
  • Maven searches to download dependencies from the configured Remote Repository, if found then download and do further processing otherwise stops & throws error

Useful Eclipse IDE shortcuts :

Related Articles:

Happy Coding !!
Happy Learning !!

Apache Maven - Installing custom library into local repository
Apache Maven - Changing default Maven repository location in Windows 7 OS