Clear and save local maven cached artifact in .m2

I have a .m2 repository on my Jenkins slavery that is growing every day, it is currently almost ~ 40GB.

Since I have multiple jobs and a choice of .m2 dependencies, I cannot delete everything, but I can see that there is an older and useless version of the artifact in every .m2 repo.

Are there any ways to access maven so that when the $ mvn install job is run, maven will only keep the latest version in the .m2 repository (example xyzw version which is incremental) for each repo inside .m2?

+3


source to share


2 answers


If you don't care that every build has external dependencies involved, you can use a private Maven repository for each job (Maven -> Advanced -> Check 'use a private Maven repository') and clean up the workspace at the start of your build. The private repository creates .repository

in your workspace, so cleaning up the workspace ensures that you start with an empty repository.

If you have a lot of common external dependencies, then you can use even more disk space as they are present multiple times in different repositories. In this case, you can write a script that periodically (using the task scheduler, for example cron

) removes unused files from the shared repository, see this answer for example .



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

0


source


After going through a similar problem, I came up with a solution and made it open source as it might help others. The app is available on Github and can clean up old dependencies and keep only the latest versions.

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



Apart from cleaning up old dependencies, it has other features such as date -based cleanup based on upload date / last available date, deleting snapshots, sources, javadocs , ignoring or forcing removal of certain groups or artifacts.

Moreover, it is cross platform and can work in both Windows and Unix / Linux environments.

+1


source







All Articles