I did spend some hours looking at this problem and to the answers, many of them rely on the atime
(which is the last access time on UNIX systems), which is an unreliable solution for two reasons:
- Most UNIX systems (including Linux and macOS) update the
atime
irregularly at best, and that is for a reason: a complete implementation of atime
would imply the whole file system would be slowed down by having to update (i.e., write to the disk) the atime
every time a file is read, moreover having a such an extreme number of updates would very rapidly wear out the modern, high performance SSD drives
- On a CI/CD environment, the VM that's used to build your Maven project will have its Maven repository restored from a shared storage, which in turn will make the
atime
get set to a "recent" value
I hence created a Maven repository cleaner and made it available on https://github.com/alitokmen/maven-repository-cleaner/. The bash maven-repository-cleaner.sh
script has one function, cleanDirectory
, which is a recursive function looping through the ~/.m2/repository/
and does the following:
- When the subdirectory is not a version number, it digs into that subdirectory for analysis
- When a directory has subdirectories which appear to be version numbers, it only deletes all lower versions
In practice, if you have a hierarchy such as:
... maven-repository-cleaner.sh
script will:
- Navigate to
artifact-group
- In
artifact-group
, navigate to artifact-name
- In
artifact-name
, delete the subfolders 1.8
and 1.2
, as 1.10
is superior to both 1.2
and 1.8
To run the tool on your CI/CD platform (or any other form of UNIX system), simply use the below three lines, either at the beginning or at the end of the build:
wget https://raw.githubusercontent.com/alitokmen/maven-repository-cleaner/main/maven-repository-cleaner.sh
chmod +x maven-repository-cleaner.sh
./maven-repository-cleaner.sh