Maven Repository – A Brief Guide

Filed Under: Maven
Maven Repositories

What is a Maven Repository?

A maven repository is a central location that holds build artifacts and their metadata.

What are the different types of Maven Repositories?

There are two types of maven repositories.

  1. Local Repository
  2. Remote Repository

Local Repository

It’s a directory in your system where you are running maven commands. It holds the artifacts downloaded from the remote repositories. When we run a maven build, the required artifacts are downloaded from the remote repository and stored in the local repository to use.

Maven Local repository also holds the artifacts you build in the local system with the “install” goal. The build artifacts and their metadata are copied to the local repository.

The default location of the local repository is ${user.home}/.m2/repository. But, we can change that by specifying a different location in the maven settings.xml file.


<localRepository>/path/to/local/repository</localRepository>

Remote Repository

These are the repositories accessed with http or file protocol. They are on a remote system and holds artifacts from third-party libraries.

When we build a maven project, the artifacts from the remote repository are downloaded to the local repository.

When we build a maven project with the “deploy” goal, the artifact is uploaded to the remote repository.

The remote repositories can be classified into two types:

  1. Internal Remote Repository
  2. Maven Central Repositoru

Internal Remote Repository

While working in an Enterprise environment, connecting to the internet to download dependencies is not acceptable for security, speed or bandwidth reasons. It’s advisable to set up an internal repository to keep the artifacts and publish the private artifacts.

An internal remote repository is generally accessed over the internet. Generally, the artifacts are downloaded from the central repository and cached to this repository as and when required by the internal projects.

Maven Central Repository

Maven Central repository contains the open-source artifacts. These are artifacts that are used by everyone. Anyone can access the central repository and download the artifacts. However, you need to get an account to publish your artifacts to the central repository.

Maven Dependencies Search Algorithm

When a maven project is built, the required dependencies are searched in the following order.

  • local
  • central
  • remote, if available
Maven Build Search Repository Order

Maven Build Search Repository Order

If the dependencies are not found, the build fails with an unknown dependency error.

Adding Remote Repository to Maven pom.xml File

Maven knows about the local repository and the central repository. But, it doesn’t know about any custom remote repository that you might have setup. You can provide details about that in the pom.xml file.


<repositories>
	<repository>
		<id>jd-repo</id>
		<name>JournalDev Maven Repo</name>
		<url>https://repo.journaldev.com/maven2/</url>
	</repository>
</repositories>

References

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages