6

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?

Cecilya
  • 519
  • 1
  • 5
  • 20
dkmaven
  • 77
  • 1
  • 1
  • 6
  • First thing never share a maven cache with multiple jobs. Each job should have it's own cache in the workspace... – khmarbaise Jun 08 '17 at 06:41
  • If I care for external dependencies are pulled in every build then bcz I have jobs like that ? as explained in below. – dkmaven Jun 08 '17 at 10:04
  • The first question: Is it really need to clean the workspace for each build? It depends. Furthermore if you need to share dependencies which are build from one job and should be consumed by another job you should use a repository manager... – khmarbaise Jun 08 '17 at 18:22

2 Answers2

3

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.

Joep Weijers
  • 1,844
  • 12
  • 19
  • Interesting reply thanks :) , but I have jobs which basically uses external dependencies like one job is going to pull dependency artifact from different job. – dkmaven Jun 08 '17 at 10:15
  • What do you mean by shared external dependencies? Every dependency you are using is a shared external dependency? What's clear to me is what do you mean by: `job is going to pull dependency artifact from different job`? So one job produces and artifact which is consumed by another job? So where is the problem here? – khmarbaise Jun 08 '17 at 18:25
  • .m2/repository/com/tb/TbMainFolder containing 0.0.1-SNAPSHOT and 0.0.2-SNAPSHOT which is getting generated by same job. explaining by example: 0.0.1-SNAPSHOT —older artifact 0.0.2-SNAPSHOT —older artifact …. … 1.0.10-SNAPSHOT -latest artifact all older artifact is not getting used anywhere it is unnecessary present in .m2 .I want when latest is available then older one should move to trace or should get deleted. – dkmaven Jun 14 '17 at 08:53
  • External dependencies can be cached in a maven repository like nexus making ithem cheaper to download – Thorbjørn Ravn Andersen Jun 14 '19 at 10:33
2

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.

Pavan Kumar
  • 4,182
  • 1
  • 30
  • 45