55

My computer runs Windows 10 and I work with Java and Eclipse. Recently Windows warned me that I'm running into short disk space and after some study I noticed that I have a folder named .m2 under my user's folder (C:\Users\MySelf) that occupies 202Gb.

I know this folder belongs to Maven. Since this folder is bigger than all my projects and their dependencies together, it seems to me that Maven is keeping unnecessary files, so I want to get rid of them.

Is is safe simply delete the folder contents or should I run some particular process in order to save space and keep Maven working well?

AlexSC
  • 1,823
  • 3
  • 28
  • 54

6 Answers6

82

It's perfectly safe to delete the folder .m2/repository as maven will re-download all the needed dependencies when needed except for your local projects. It there any other folder under .m2 taking space?

For your local projects, maven will complain about not finding them. In that case, you simply need to re-compile them and install them by running mvn clean install in each project folder. They will get uploaded to the repository.

Community
  • 1
  • 1
Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
  • 3
    There is a typo in folder name, should be: `.m2/repository`. I'm unable to edit the answer because edits have to be at least 6 characters long ¯\\_(ツ)_/¯ – TMG Jun 05 '19 at 07:35
  • 3
    It is not perfectly safe if you have projects/dependencies that are not available on internet anymore. – lubrum Jul 11 '21 at 22:56
  • what If I delete all the contents of the .m2 folder, then what am I supposed to do? I delete the repository and the wrapper folders – iifast2 Feb 21 '22 at 08:27
10

Solution 1:

You can even create new folder in D drive or any other drive. And use it.

Not necessary that you use .m2 folder created by default.

Also change this to new path:

<localRepository>D:\.m2\repository_Product</localRepository>

Change setting as shown in image below:

Solution 2:

delete .m2 folder and then make sure that all the projects are having only new versions/required version of jar entry in pom.xml. Re-build the project. The .m2 folder will remain with only latest version

Tip: Keep your IDE and required projects open so that it will be prevented from deletion.

enter image description here

VedantK
  • 9,728
  • 7
  • 66
  • 71
4

For an Ubuntu/Linux environment, use the below command to point to a new m2 directory without deleting the existing one.

mvn clean install -Dmaven.repo.local=/home/ishara/newm2
isharailanga
  • 565
  • 7
  • 19
3

Maven downloads all dependencies to a folder .m2/repository in the user's home directory (i.e. C:\users\[your user name]\.m2\repository on Windows. When you delete this folder, Maven creates the folder and downloads the dependencies for the projects you're building from then on.

If you want to change the folder to another directory, you can put a Maven configuration file settings.xml in the .m2 directory. You can find a sample file in your Maven distribution in the conf directory. Then look for the entry localRepository which should be commented out like this:

<!-- localRepository
 | The path to the local repository maven will use to store artifacts.
 |
 | Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->

Activate it an set it to the path you like, e.g. make Maven download the dependencies to D:\myMavenRepository set it to

<localRepository>D:/myMavenRepository</localRepository>
Ralf Wagner
  • 1,467
  • 11
  • 19
0

how to clear. m2 directory go for file explorer --- ThisPC/user/systemname/.m2 there you can delete it

-4

Just open eclipse-->window-->preferences-->maven-->user settings-->in there see "local repository location". then find the location for your .m2/repository folder then delete that folder if your maven doesn't work properly.