There are three types of Maven repositories:
You will have seen Maven downloading a number of files (called poms and jars). Let us see where they are located in your computer:
Go to your
HOME
folder (C:\Users\username
) in the case of Microsoft Windows,/Users/username
for Mac, and,/home/username
(or a similar location) for LinuxYou will notice the
.m2
folder and within that, a subfolder calledrepository
You will see a number of folders and files that are used by Maven
You may want to change this location for the following reasons:
You may want to conserve space in the
C
drive and store these folders and files in theD
drive on Microsoft Windows.You may want to take a back up of the contents. Backup software usually backs up contents in specific folders of the filesystem.
Your organization may have a policy for all users to store a local repository in the same folder.
To change the location of the Maven repository, perform the following steps:
Create a file called
settings.xml
in the.m2
folder.Add the following contents to the settings.xml file that you just created:
<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:/software/maven</localRepository> </settings>
Notice the highlighted part of the preceding code. We have changed the location of the repository contents to C:\software\maven. You can change it to any valid folder name.
Delete the
repository
subfolder and run themvn package
command again.You will now notice that the
repository
folder is not created in the.m2
folder. Instead, it is created inC:\software\maven
.
Maven determines the location of the local repository in the following way:
If
settings.xml
exists in the user's.m2
folder, which contains the<localRepository>
tag, then Maven uses its contents to determine the locationIf not, Maven will check if
localRepository
is explicitly defined in the defaultsettings.xml
, present in theconf
folder of the Maven installationIf it is not present there, Maven will use the default value for the local repository, which is the user's
.m2
folder