Maven Repository
In Maven terminology, a repository is a location.
A Maven repository is where third-party libraries for a project are stored, and this location is called a repository.
In Maven, any dependency, plugin, or project build output can be referred to as an artifact.
Maven repositories help us manage these artifacts (mainly JAR files), which are places where all JAR files (WAR, ZIP, POM, etc.) are stored.
There are three types of Maven repositories:
- Local
- Central
- Remote
Local Repository
Maven's local repository is not created upon installing Maven; it is created the first time a Maven command is executed.
When running Maven, any artifacts required are fetched directly from the local repository. If the artifact is not available locally, Maven will attempt to download it from a remote repository to the local repository, and then use the local artifact.
By default, on both Linux and Windows, each user has a repository directory named .m2/repository/ in their user directory.
The Maven local repository is created by default in the %USER_HOME% directory. To change the default location, define an alternate path in the Maven settings.xml file located in the %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>
When you run a Maven command, Maven will download the dependency files to your specified path.
Central Repository
The Maven Central Repository is provided by the Maven community and contains a large number of commonly used libraries.
The central repository includes most popular open-source Java artifacts, along with source code, author information, SCM information, license information, etc. Generally, the artifacts required by simple Java projects can be downloaded from here.
Key concepts of the central repository:
- Managed by the Maven community.
- No configuration required.
- Requires network access.
To browse the contents of the central repository, the Maven community provides a URL: http://search.maven.org/#browse. Developers can use this repository to search for all available code libraries.
Remote Repository
If Maven cannot find the required file in the central repository, it will halt the build process and output an error message to the console. To avoid this, Maven introduces the concept of a remote repository, which is a custom repository created by developers that contains the required code libraries or other project-related JAR files.
For example, using the following pom.xml, Maven will download the dependencies declared in the pom.xml (that are not available in the central repository) from the remote repository.
<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.companyname.projectgroup</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<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>