Maven Housekeeping: Cleanup your local repository on Linux
Monday, July 19. 2010
For all those that where seeking for a method to roll out outdated artifacts from their Maven repository like me, but could not find a solution. Here is a simple shell script doing that work for you.
This script assumes that you have your maven repository in your home directory. It will delete all jar, swf and swc artifacts not used for AGE (31) days. Perhaps this script also works on Windows using Cygwin, but this is untested.
#!/bin/sh
M2_REPO=${HOME}/.m2
OLDFILES=/tmp/oldfiles.txt
AGE=31
find "${M2_REPO}" -name '*jar' -atime +${AGE} -exec dirname {} \; \\
>> ${OLDFILES}
find "${M2_REPO}" -name '*swf' -atime +${AGE} -exec dirname {} \; \\
>> ${OLDFILES}
find "${M2_REPO}" -name '*swc' -atime +${AGE} -exec dirname {} \; \\
>> ${OLDFILES}
for x in `cat ${OLDFILES}`; do rm -rf "$x"; done
Just copy and paste the script into a file like cleanM2.sh and set the file permissions with
chmod 755 cleanM2.sh
Finally execute it with
./cleanM2.sh
Warning: Make sure that you have backups of artifacts that cannot be downloaded from remote anymore. For example some special SNAPSHOT versions that have been updated in the meantime will be lost, if the file was not used for one month.
Trackbacks
Trackback specific URI for this entry
No Trackbacks



Comments
Just wanted to point out that the find command you use actually finds anything that *has* been used in the last 31 days. Removing the ! mark fixes this. My revised script that looks for zip files and strips out duplicates:
#!/bin/sh
M2_REPO=${HOME}/.m2
OLDFILES=/tmp/oldfiles.txt
AGE=31
find "${M2_REPO}" -name '*jar' -atime +${AGE} -exec dirname {} \; >> ${OLDFILES}
find "${M2_REPO}" -name '*zip' -atime +${AGE} -exec dirname {} \; >> ${OLDFILES}
find "${M2_REPO}" -name '*swf' -atime +${AGE} -exec dirname {} \; >> ${OLDFILES}
find "${M2_REPO}" -name '*swc' -atime +${AGE} -exec dirname {} \; >> ${OLDFILES}
#for x in `sort ${OLDFILES} | uniq`; do rm -rf "$x"; done
for x in `sort ${OLDFILES} | uniq`; do echo "$x"; done
rm $OLDFILES