Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clean up and maintain local maven cached artifact in .m2

Tags:

I have an .m2 repository on my Jenkins slave which is growing every day, currently it's nearly ~40 GB.

Since I have multiple jobs running and picking dependencies from .m2 I cannot remove everything, but I can see in each repo of .m2 there is an older and useless version of the artefact.

Are there any means of way available in maven so that when a job triggers $mvn install maven will keep the latest version only in the .m2 repo (example versioning x.y.z.w which is incremental) for every repo inside .m2?

like image 312
dkmaven Avatar asked Jun 08 '17 06:06

dkmaven


People also ask

How do I clean up .m2 folder?

m2 folder? Simply delete the . m2repository folder. It will get created automatically once you compile the project.

How do I clear my local repository cache?

Local Maven Repository location To clear/delete your local maven repository cache, simply delete the . m2/repository folder. The local repository path can also be configured in Maven setting.

Where is Maven local cache?

The first place that Maven looks for artifacts is in the local repository, which is the local cache where Maven stores all of the artifacts it has downloaded or found elsewhere. The default location of the local repository is the . m2/repository/ directory under the user's home directory.

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.


2 Answers

If you don't care that external dependencies are pulled in every build, you could use a private Maven repository per job (Maven -> Advanced -> Check 'use private Maven repository') and clean the workspace at the start of your build. The private repository creates a .repository in your workspace, so cleaning your workspace will ensure you start with an empty repository.

Should you have many shared external dependencies, then you may be using even more diskspace, since they are present multiple times in the different repositories. In that case you could write a script that periodically (using a task scheduler like cron) removes unused files from the shared repository, see for example this Stack Overflow answer.

However be cautious with a shared Maven repository! Maven by default is not threadsafe, so concurrent jobs downloading the same artifact might use the incomplete downloads. Consider using the Takari extensions to make your Maven repository thread-safe.

like image 186
Joep Weijers Avatar answered Oct 11 '22 14:10

Joep Weijers


Having been through a similar problem, I came up with a solution and made it open source as it might help others. The application is available on Github and it can clean up old dependencies and retain just the latest.

https://github.com/techpavan/mvn-repo-cleaner

Apart from cleaning old dependencies, it has other features like date based cleanup based on download date / last accessed date, removing snapshots, sources, javadocs, ignoring or enforcing deletion of specific groups or artifacts.

Additionally, this is cross platform and can run on both Windows and Unix / Linux environments.

like image 27
Pavan Kumar Avatar answered Oct 11 '22 14:10

Pavan Kumar