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?
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.
To clear/delete your local maven repository cache, simply delete the . m2/repository folder.
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.
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.
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'.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With