Maven Tutorial - Maven Repositories








A Maven repository is a directory to store all the project jars, library jar, plugins or any other artifacts.

There are three types of Maven repository.

  • local
  • central
  • remote
null




Local Repository

Maven local repository is a local folder on your machine.

Maven local repository is created when you run any maven command for the first time.

Maven local repository stores all dependency library jars, plugin jars, etc on your development machine.

When Maven downloads the dependency jars it stores the jar files in the local Maven repository.

If a newer version is needed Maven would download the newer version. If the version being declared in the dependency element in pom.xml file is already in the local Maven repository it just uses it without downloading.

Local Maven repository avoid referencing to dependencies stored on remote machine every time a project is build.

By default Maven creates the local repository under %USER_HOME% directory.

We can set the folder for Maven local repository in Maven settings.xml file available at %M2_HOME%\conf directory.

<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>C:/MyLocalRepository</localRepository>
</settings>

After setting the new local repository folder when running Maven command, Maven will download dependencies to the custom path.





Central Repository

Maven central repository is repository managed by Maven community.

Maven central repository contains a large number of commonly used libraries. And we can publish our own libraries to Maven central repository as well.

When Maven cannot find any dependency jar file in your local repository, it starts searching in Maven central repository using following URL: http://repo1.maven.org/maven2/.

We don't need to configure the Maven central repository URL. But we do need internet access to download and search the Maven central repository.

To browse the central maven repository type in the following URL in your browser address bar.

http://search.maven.org/#browse

We can search a jar file in central repository.

Remote Repository

Sometime we need to set up a Maven repository inside a company or a project development team to host our own libraries.

The company mantained repository is outside developer's machine and is called Maven remove repository.

The following pom.xml declares dependencies and also declared remote repository URL.

<project ...>
   <dependencies>
      <dependency>
         <groupId>com.companyname.common-lib</groupId>
         <artifactId>common-lib</artifactId>
         <version>1.0.0</version>
      </dependency>
   <dependencies>
   <repositories>
      <repository>
         <id>companyname.lib1</id>
         <url>http://download.companyname.org/maven2/lib1</url>
      </repository>
      <repository>
         <id>companyname.lib2</id>
         <url>http://download.companyname.org/maven2/lib2</url>
      </repository>
   </repositories>
</project>

Maven Dependency Search Sequence

Maven searches for dependency libraries in the following sequence:

  1. Search local dependency repository.
  2. Search central dependency repository
  3. Search the remote dependency repository

Maven stops the searching once it finds the jar file.