Categories: Maven

Maven Repository

Maven Repository is simply directory or folder where all jars, plugins or any other projects related artifacts are available and stored for future preference. Maven searches for dependencies in the repositories. There are following three types of Maven repositories.

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

Maven searches the dependency first from the Local Repository then from Central Repository and then from Remote Repository if it configured.

If dependency is not found in these repositories, maven stops processing and throws an error.

1. Local Repository

Maven Local Repository is in your computer location. It is actually a directory in your machine. It is created in home directory of your machine whenever you run any first command of maven. This repository will contain all the dependencies Maven downloads. The same Maven repository is typically used for several different projects. It helps to avoid references to dependencies stored on remote machine every time a project is build.

By default Maven Local Repository is created into your user home directory on your local computer (%USER_HOME%). You can override this location to another location by changing in the maven settings file.Your Maven settings file is also located in your user-home/.m2 directory and is called settings.xml.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 
   http://maven.apache.org/xsd/settings-1.0.0.xsd">
      <localRepository>D:/MyMavenLocalRepository</localRepository>
</settings>

When you run Maven command, Maven will download dependencies to your custom path.

2. Central Repository

Maven central repository is managed by the Maven Community. It is located on the web/cloud. By default if maven is not found dependency in your local repository then looks in the central repository. There is no configuration or specification required to download dependency in to local repository from the central repository.

The path of central repository is: http://repo1.maven.org/maven2/.

The central repository contains a lot of common libraries that can be viewed by this url http://search.maven.org/#browse.

3. Remote Repository

Maven remote repository is also located on the web/cloud or inside a local network but it is not managed by the Maven Community. It is actually a repository created by our organization to access those dependencies which are not available on the central repository such as JBoss library etc, so we need to define remote repository in pom.xml file. A remote repository is often used for hosting projects internal to your organization, which are shared by multiple projects.

You can configure a remote repository in the POM file. Put the following XML elements right after the <dependencies> element:

<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.dineshonjava.myapp</groupId>
   <artifactId>myapp</artifactId>
   <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
   <dependencies>
      <dependency>
        <groupId>org.springframework</groupId>
 <artifactId>spring-web</artifactId>
 <version>${spring.version}</version>
      </dependency>
       ......
       ......
   <dependencies>
   <repositories>
      <repository>
         <id>dineshonjava.lib1</id>
         <url>http://download.dineshonjava.org/maven2/lib1</url>
      </repository>
      <repository>
         <id>dineshonjava.lib2</id>
         <url>http://download.dineshonjava.org/maven2/lib2</url>
      </repository>
   </repositories>
</project>
Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design PatternsĀ using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago