Maven – Local Repository Location

Lokesh Gupta

In this Maven tutorial, we will know the default location of Maven repository and how to configure a custom location.

Maven has the following types of repositories:

  • Local: This is the repository in our computer.
  • Remote: This is the repository in Internet from where the required Maven files get downloaded.
  • Mirrors: These are repository managers managed by the organizations which want to control the artifact versions allowed in their development teams. Nexus and Artifactory are examples of these mirror repositories.

Maven local repository is a folder in our computer where Maven stores the dependencies in download from the remote repositories.

1. Default Location of the Maven Local Repository

Once we have installed Maven, by default, Maven’s local repository is configured to be ‘${user.home}/.m2/repository‘.

In different operating systems, these paths are resolved to –

Windows 7: 		C:/Documents and Settings/<username>/.m2/repository
Windows 10:		C:/Users/<username>/.m2/repository
Linux: 			/home/<username>/.m2/repository
Mac: 			/Users/<username>/.m2/repository

We may want to change this location to another path for various reasons. For example, we may want to save storage space in C:\ and store downloaded files in another drive.

2. Changing the Location of Local Repository

Maven is downloaded and extracted as an archive file. Generally, we download the Maven and extract it in their computer.

After downloading the Maven, follow the given simple steps to change the local repository location to some other path.

  • Navigate to path {M2_HOME}\conf\ where M2_HOME is maven installation folder.
  • Open file settings.xml in edit mode in some text editor.
  • Fine the tag <localRepository>
  • Update the desired path in value of this tag. Save the file.
<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 https://maven.apache.org/xsd/settings-1.0.0.xsd">
 
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository -->
 
  <localRepository>E:/devsetup/M2</localRepository>
 
  ...
  ...
 
</settings>

We have now successfully changed the maven local repository path.

We can verify by updating any pom.xml file and all downloaded dependencies in the new location.

Maven local repository path
Maven local repository path

Note that if there are existing jar files and pom files stored in the previous local repository location, they will not be moved. We need to move them or remove them manually.

3. Changing the Repository Location in Eclipse

These days, IDEs also come with inbuilt Maven support and we only need to install Maven if we are making the project builds from Console.

Each IDE has a separate process to change the local repository path and you can read its official documentation.

For example in Eclipse and STS (Spring Tools Suite), we can change the local repository path in the following location: Windows -> Pfreferences -> Maven -> User Settings

Here you can give the path of settings.xml file where we have configured the folder location of the local repository.

4. Setting the Repository Location in pom.xml

Maven allows configuring the repository locations in the pom.xml file also. We need to use the file:// protocol to specify the location.

<project>
  ...
  <repositories>
    <repository>
      <id>my-repo</id>
      <name>My Repository</name>
      <url>file://path/to/our/local/repository</url>
    </repository>
  </repositories>
</project>

As described in Maven docs, remote repository URLs are queried in the following order for artifacts until one returns a valid result:

  1. effective settings:
    • Global settings.xml
    • User settings.xml
  2. local effective build POM:
    • Local pom.xml
    • Parent POMs, recursively
    • Super POM
  3. effective POMs from dependency path to the artifact.

This means that Maven will first use the globally configured location of Maven local repo. If the global configuration is not found, then only the pom.xml configured location will be used.

Effective settings and local build POM, with profile taken into account, can easily be verified with mvn help:effective-settings and mvn help:effective-pom -Dverbose.

<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>E:\DevSetup\mavenLibs</localRepository>
  <mirrors>
    <mirror>
      <mirrorOf>external:http:*</mirrorOf>
      <name>Pseudo repository to mirror external repositories initially using HTTP.</name>
      <url>http://0.0.0.0/</url>
      <blocked>true</blocked>
      <id>maven-default-http-blocker</id>
    </mirror>
  </mirrors>
  <pluginGroups>
    <pluginGroup>org.apache.maven.plugins</pluginGroup>
    <pluginGroup>org.codehaus.mojo</pluginGroup>
  </pluginGroups>
</settings>

Happy Learning !!

Comments

Subscribe
Notify of
guest
5 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode