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.
- Local Repository
- Central Repository
- 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>