Guide to Maven Repository

A maven repository is a directory of packaged JAR files with the pom.xml file. Maven searches for dependencies in the repositories. 
There are 3 types of maven repositories:
  • Local Repository
  • Central Repository
  • Remote Repository
  1. Local Repository – Folder location on the local Dev machine
  2. Central Repository – Repository provided by Maven community
  3. Remote Repository – Organization owned custom repository
Maven searches for the dependencies in the following order:
Local repository -> Central repository -> Remote repository.

In simple words, when we run a Maven build, all the dependencies of our project (jars, plugin jars, other artifacts) are all stored locally for later use.

2. Types of Maven Repository

2.1. The Local Repository

The local repository of Maven is a folder location on the developer’s machine, where all the project artifacts are stored locally.
When maven build is executed, Maven automatically downloads all the dependency jars into the local repository.
Usually, this folder is named .m2.
Here’s where the default path to this folder is – based on OS:
  • Windows: C:\Users<User_Name>.m2
  • Linux: /home/<User_Name>/.m2
  • Mac: /Users/<user_name>/.m2
And of course, for both on Linux or Mac:
  • Linux/Mac: ~/.m2
How to change the location of the Local Repository?
We can change the location of the maven local repository by changing the settings.xml file. It is located in MAVEN_HOME/conf/settings.xml, for example, E:\apache-maven-3.1.1\conf\settings.xml.
Let's see the default code of the settings.xml file:
...  
<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  
   | The path to the local repository maven will use to store artifacts.  
   |  
   | Default: ${user.home}/.m2/repository  
  <localRepository>/path/to/local/repo</localRepository>  
  -->  
  
...  
</settings> 
Now change the path to the local repository. After changing the path of the local repository, it will look like this:
...  
<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>e:/mavenlocalrepository</localRepository>  
    
...  
</settings>  
As you can see, now the path of the local repository is e:/mavenlocalrepository.

2.2 Maven Central Repository

Maven's central repository is located on the web. It has been created by the apache maven community itself.
The path of the 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.

2.3 Maven Remote Repository

Maven remote repository is located on the web. Most of the libraries can be missing from the central repository such as the JBoss library etc, so we need to define a remote repository in the pom.xml file.
Let's see the code to add the JUnit library in the pom.xml file:
<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.javaguides.app</groupId>  
  <artifactId>my-app</artifactId>  
  <version>1.0</version>  
  <packaging>jar</packaging>  
  
  <name>Maven Quick Start Archetype</name>  
  <url>http://maven.apache.org</url>  
  
  <dependencies>  
    <dependency>  
      <groupId>junit</groupId>  
      <artifactId>junit</artifactId>  
      <version>4.8.2</version>  
      <scope>test</scope>  
    </dependency>  
  </dependencies>  
  
</project>  
You can search any repository from Maven's official website mvnrepository.com.

3. Conclusion

In this quick guide, we had a look at the Maven local repository, central repository, and remote repository.

Read more on the Maven Developer Guide.

Comments