Maven Repositories

  

What is a Maven Repository?

Maven repository is a storage location where Maven artifacts (like JAR files, plugins, and other dependencies) are stored and managed. These artifacts are required for building Java projects using Maven.

Repositories play a key role in dependency management, allowing Maven to download required libraries automatically, rather than developers having to manually include them.



Types of Maven Repositories

Maven repositories are generally classified into three types:



Repository TypeDescription
Local RepositoryA folder on the developer’s computer where Maven stores downloaded dependencies from remote repositories.
Central RepositoryA default, public repository provided by Maven community at https://repo.maven.apache.org/maven2. It contains a vast collection of open-source libraries.
Remote RepositoryAny custom repository hosted on a server, typically within an organization or on the internet (e.g., JFrog Artifactory, Nexus). Used when artifacts are not available in the central repo.



1. Local Repository

  • Location: ~/.m2/repository (default location on your machine).

  • Maven first checks the local repository before looking elsewhere.

  • If a dependency is not found locally, it is fetched from a remote repository and stored locally for future use.



2. Central Repository

  • Provided by the Maven community.

  • Maven refers here if the dependency is not found in the local repo.

  • URL: https://repo.maven.apache.org/maven2

  • No configuration is needed — it's used by default.



3. Remote Repository

  • Maintained by organizations to host private or custom-built artifacts.

  • Can be configured in pom.xml or settings.xml:


<repositories>
  <repository>
    <id>my-org-repo</id>
    <url>https://repo.mycompany.com/maven2</url>
  </repository>
</repositories>



How Maven Uses Repositories During Build:

  • Check local repository for required dependencies.
  • If not found, go to the configured remote repositories or central repository.
  • Download the dependency and store it in the local repository.
  • Use it to compile and build the project.



Points to Remember:
  • repository is like a warehouse for Java libraries.

  • Helps automate dependency management.

  • Reduces manual work by automatically downloading and resolving dependencies.