Sunday 31 July 2022

Quick guide to maven local repository

 

Maven support three kinds of repositories.

a.   Local repository: It is a is a directory on the computer where Maven runs. It caches remote downloads and contains temporary build artifacts that you have not yet released.

b.   Remote repository: Custom repository provided by your organization. In general, every organization configures a remote repository, where maven download the dependencies that are not exist in the local repository.

c.    Central repository: Repository provided by Maven community (https://repo1.maven.org/maven2/).

 

 


Typical flow looks like below.

a.   When you build a project that require dependency ‘X’.

b.   Maven check for the dependency ‘X’ in the local repository. If it exist, it build the artefact with given dependency. If the dependency is not exist in the local repository, maven contact the remote repository, download the dependency to local repository, and build the artifact.

 

Location of maven local repository

Default local repository location varies based on the operating system, find the following details.

 

Windows: 

C:\Users\<user_name>\.m2


Linux: 

/home/<user_name>/.m2 or ~/.m2


Mac: 

/Users/<user_name>/.m2 or ~/.m2


Customize maven local repository

You can customize the local repository location by updating the ‘localRepository’ property in the file '{MAVEN_HOME}\conf\settings.xml'.

<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->

  <localRepository>/Users/krishna/Documents/my_m2</localRepository>

</settings>

Can I pass my custom local repository location to the maven command?

Yes, you can specify the custom local repository location to the maven command using the argument -Dmaven.repo.local.

mvn -Dmaven.repo.local=/Users/krishna/Documents/my_m2 clean install

References

https://maven.apache.org/guides/introduction/introduction-to-repositories.html


Previous                                                 Next                                                 Home

No comments:

Post a Comment