Maven Repository (local Repository + Remote Repository)

Maven repositories are used to store and manage dependencies (artifacts) used in Maven projects. There are two types of repositories: local repositories and remote repositories.

1. Local Repository

A local repository is a folder on your local machine where Maven caches downloaded artifacts, which can be shared among multiple projects on your computer. When you build a Maven project, Maven first searches for the required artifacts in the local repository. If the artifacts are not found, Maven downloads them from remote repositories and caches them in the local repository.

By default, the local repository is located in the .m2 directory within your user's home directory:

  • Windows: C:\Users\{username}\.m2\repository
  • macOS/Linux: /home/{username}/.m2/repository or ~/.m2/repository

You can change the local repository location by editing your settings.xml file, typically found in the .m2 directory (e.g., ~/.m2/settings.xml). Add or modify the <localRepository> element:

<settings>
  <localRepository>/path/to/your/custom/repository</localRepository>
</settings>

Replace /path/to/your/custom/repository with the path to your desired local repository location.

2. Remote Repository

Remote repositories are used to host and share artifacts between different developers and projects. Maven connects to remote repositories to download artifacts that are not found in the local repository.

There are three types of remote repositories:

  • Central Repository: The default remote repository provided by Maven. It hosts a vast collection of open-source artifacts. No additional configuration is needed to use the Central Repository.

  • External Repository: A third-party repository that hosts additional artifacts not found in the Central Repository. To use an external repository, add the following to your project's pom.xml file:

    <repositories>
      <repository>
        <id>external-repo-id</id>
        <url>https://path.to/external/repo</url>
      </repository>
    </repositories>
    

    Replace external-repo-id with a unique identifier for the repository, and https://path.to/external/repo with the repository URL.

  • Internal Repository: A private repository hosted within your organization. This can be useful for sharing proprietary artifacts. To use an internal repository, update your Maven settings.xml file:

    <profiles>
      <profile>
        <id>internal-repo-profile</id>
        <repositories>
          <repository>
            <id>internal-repo-id</id>
            <url>http://path.to/internal/repo</url>
          </repository>
        </repositories>
      </profile>
    </profiles>
    
    <activeProfiles>
      <activeProfile>internal-repo-profile</activeProfile>
    </activeProfiles>
    

    Replace internal-repo-id with a unique identifier for the repository, and http://path.to/internal/repo with the repository URL.

In conclusion, Maven repositories (local and remote) store and manage project dependencies. The local repository caches artifacts on your machine, while remote repositories host artifacts for sharing between projects and developers.

  1. How to configure Maven local repository:

    • The local repository is automatically created in the ~/.m2/repository directory.
    • You can configure a different local repository by updating the settings.xml file:
    <!-- Example custom local repository configuration in settings.xml -->
    <settings>
        <localRepository>/path/to/custom/local/repository</localRepository>
        <!-- Additional configurations... -->
    </settings>
    
  2. Adding dependencies to Maven local repository:

    • Use the install goal to add dependencies to the local repository:
    mvn install:install-file -Dfile=path/to/artifact.jar -DgroupId=com.example -DartifactId=my-artifact -Dversion=1.0.0 -Dpackaging=jar
    
  3. Configuring custom Maven remote repository:

    • Add remote repository details in the pom.xml or settings.xml:
    <!-- Example remote repository in POM -->
    <repositories>
        <repository>
            <id>custom-repo</id>
            <url>https://your.custom.repo/maven-repository</url>
        </repository>
    </repositories>
    
  4. Maven repository structure and organization:

    • Maven follows a standard repository structure with groupId, artifactId, and version directories.
    /.m2/repository
    ������ com
    ��   ������ example
    ��       ������ my-artifact
    ��           ������ 1.0.0
    ��               ������ my-artifact-1.0.0.jar
    
  5. Proxy settings for Maven repositories:

    • Configure proxy settings in settings.xml if you are behind a corporate firewall:
    <!-- Example proxy configuration in settings.xml -->
    <settings>
        <proxies>
            <proxy>
                <id>example-proxy</id>
                <active>true</active>
                <protocol>http</protocol>
                <host>proxy.example.com</host>
                <port>8080</port>
                <!-- Additional configurations... -->
            </proxy>
        </proxies>
    </settings>
    
  6. Maven repository authentication settings:

    • If your repository requires authentication, provide credentials in settings.xml:
    <!-- Example repository authentication in settings.xml -->
    <servers>
        <server>
            <id>custom-repo</id>
            <username>your-username</username>
            <password>your-password</password>
        </server>
    </servers>
    
  7. Publishing artifacts to Maven local repository:

    • Use the install goal to publish artifacts to the local repository:
    mvn install