In the interest of posting solutions to problems that may prove useful to others, here's the code that a fellow programmer whipped up to solve the problem... it's java rather than a shell script, but perhaps others may be helped:
Code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class SnapshotPurger {
private static String[] extensions
= new String[]{
"-sources.jar",
"-sources.jar.md5",
"-sources.jar.sha1",
".pom",
".pom.md5",
".pom.sha1",
".jar",
".jar.md5",
".jar.sha1",
".war",
".war.md5",
".war.sha1",
".ear",
".ear.md5",
".ear.sha1"
};
private static String getMatchingExtension(File aFile) {
for (String ext : extensions) {
if (aFile.getName().endsWith(ext)) {
return ext;
}
}
return null;
}
public static void purgeStaleSnapshots(File aFile, boolean
deleteEnabled) throws Exception {
Map<String, Set<File>> sortedFiles = new HashMap<String, Set<File>>();
String ext = null;
for (File childFile : aFile.listFiles()) {
if (childFile.isDirectory()) {
purgeStaleSnapshots(childFile, deleteEnabled);
}
else {
ext = getMatchingExtension(childFile);
if (ext != null) {
Set<File> files = sortedFiles.get(ext);
if (files == null) {
files = new HashSet<File>();
sortedFiles.put(ext, files);
}
files.add(childFile);
}
}
}
for (Set<File> files : sortedFiles.values()) {
File mostRecentFile = null;
for (File file : files) {
if (mostRecentFile == null) {
mostRecentFile = file;
}
else if (mostRecentFile.lastModified() >
file.lastModified()) {
System.out.println("Deleting " + file.getAbsolutePath());
if (deleteEnabled) {
file.delete();
}
}
else {
System.out.println("Deleting " + mostRecentFile.getAbsolutePath());
if (deleteEnabled) {
mostRecentFile.delete();
}
mostRecentFile = file;
}
}
}
}
public static void main(String[] args) throws Exception {
if (args.length == 0) {
throw new IllegalArgumentException("Snapshot repository path is required.");
}
File aFile = new File(args[0]);
if (!aFile.exists()) {
throw new FileNotFoundException(args[0] + " is not a valid path.");
}
boolean deleteEnabled = false;
if (args.length > 1) {
deleteEnabled = Boolean.parseBoolean(args[1]);
}
purgeStaleSnapshots(aFile, deleteEnabled);
}
}