Maven – Local, Remote and Central Repositories

Maven repositories are physical directories which contain packaged JAR files along with extra meta data about these jar files. This meta data is in form of POM files which have jar file project information, including what other external dependencies this JAR file has. These other external dependencies are downloaded transitively into your project and become part of effective pom for the project.

Table of Contents

Local repository
Central repository
Remote repository

Local repository

Maven local repository reside in the developer’s machine. Whenever you run maven goals which require these dependencies, maven will download the dependencies from remote servers and store them into developer’s machine.

By default, Maven create the local repository inside user home directory i.e. C:/Users/superdev/.m2 directory. You can change the location of the local repository in setting.xml file using localRepository tag.

<settings>
    <localRepository>
        C:\M2
    </localRepository>
</settings>

Having stored the dependencies into local machine has two main benefits. First, multiple projects can access same artifact so it reduces the storage need. Second, as dependency is downloaded only once, it reduces the network usage as well.

Central repository

Maven central repository is located at http://repo.maven.apache.org/maven2/. Whenever you run build job, maven first try to find dependency from local repository. If it is not there, then, by default, maven will trigger the download from this central repository location.

Maven Central Repository
Maven Central Repository

To override this default location, you can can make changes to your settings.xml file to use one or more mirrors.

You do not need to make any special configuration to allow access the central repository, except network proxy settings if you are behind any firewall.

Remote repository

Apart from central repository, you may have needed artifacts deployed on other remote locations. For example, in your corporate office there may be projects or modules specific to organization only. In this cases, organization can create remote repository and deploy these private artifacts. This remote repository will be accessible only inside organization.

These maven remote repository work exactly same way as maven’s central repository. Whenever an artifact is needed from these repositories, it is first downloaded to developer’s local repository and then it is used.

You can configure a remote repository in the POM file or super POM file in remote repository itself.

<repositories>
   <repository>
       <id>org.source.repo</id>
       <url>http://maven.orgName.com/maven2/</url>
   </repository>
</repositories>

Drop me your questions in comments section.

Happy Learning !!

Ref: Introduction to Repositories

Leave a Reply

14 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.