Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clean old dependencies from maven repositories?

I have too many files in .m2 folder where maven stores downloaded dependencies. Is there a way to clean all old dependencies? For example, if there is a dependency with 3 different versions: 1, 2 and 3, after cleaning there must be only 3rd. How I can do it for all dependencies in .m2 folder?

like image 740
Cherry Avatar asked Oct 11 '13 05:10

Cherry


People also ask

Does Maven clean remove dependencies?

It only cleans the project. Show activity on this post. Show activity on this post. With the help of Purging local repository dependencies you need to do that.

How do I clean up my local Maven repository?

To clear/delete your local maven repository cache, simply delete the . m2/repository folder.

Which command can you use to clear up old artifacts in a Maven project?

When running mvn install on a local multi module project it builds and install the projects artifacts into the local repo. mvn clean seems to clean up my project specific target directories.

Is it safe to remove .m2 folder?

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.


2 Answers

If you are on Unix, you could use the access time of the files in there. Just enable access time for your filesystem, then run a clean build of all your projects you would like to keep dependencies for and then do something like this (UNTESTED!):

find ~/.m2 -amin +5 -iname '*.pom' | while read pom; do parent=`dirname "$pom"`; rm -Rf "$parent"; done 

This will find all *.pom files which have last been accessed more than 5 minutes ago (assuming you started your builds max 5 minutes ago) and delete their directories.

Add "echo " before the rm to do a 'dry-run'.

like image 54
Florian Avatar answered Sep 23 '22 18:09

Florian


Short answer - Deleted .m2 folder in {user.home}. E.g. in windows 10 user home is C:\Users\user1. Re-build your project using mvn clean package. Only those dependencies would remain, which are required by the projects.

Long Answer - .m2 folder is just like a normal folder and the content of the folder is built from different projects. I think there is no way to figure out automatically that which library is "old". In fact old is a vague word. There could be so many reasons when a previous version of a library is used in a project, hence determining which one is unused is not possible.

All you could do, is to delete the .m2 folder and re-build all of your projects and then the folder would automatically build with all the required library.

If you are concern about only a particular version of a library to be used in all the projects; it is important that the project's pom should also update to latest version. i.e. if different POMs refer different versions of the library, all will get downloaded in .m2.

like image 20
Gyanendra Dwivedi Avatar answered Sep 23 '22 18:09

Gyanendra Dwivedi