From 005ac05887d14d439a50274aed3edd4ab54af34a Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Thu, 27 Sep 2012 21:04:42 -0400
Subject: [PATCH] Automatically delete obsolete artifacts from ext

---
 .classpath                       |   72 ++++++++++++++++++------------------
 src/com/gitblit/build/Build.java |   34 +++++++++++++++++
 2 files changed, 70 insertions(+), 36 deletions(-)

diff --git a/.classpath b/.classpath
index 1c589d2..d4bfd91 100644
--- a/.classpath
+++ b/.classpath
@@ -1,39 +1,39 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <classpath>
-	<classpathentry kind="src" path="src"/>
-	<classpathentry kind="src" path="resources"/>
-	<classpathentry kind="src" path="tests"/>
-	<classpathentry kind="lib" path="ext/jcommander-1.17.jar" sourcepath="ext/src/jcommander-1.17-sources.jar" />
-	<classpathentry kind="lib" path="ext/log4j-1.2.17.jar" sourcepath="ext/src/log4j-1.2.17-sources.jar" />
-	<classpathentry kind="lib" path="ext/slf4j-api-1.6.6.jar" sourcepath="ext/src/slf4j-api-1.6.6-sources.jar" />
-	<classpathentry kind="lib" path="ext/slf4j-log4j12-1.6.6.jar" sourcepath="ext/src/slf4j-log4j12-1.6.6-sources.jar" />
-	<classpathentry kind="lib" path="ext/mail-1.4.3.jar" sourcepath="ext/src/mail-1.4.3-sources.jar" />
-	<classpathentry kind="lib" path="ext/javax.servlet-api-3.0.1.jar" sourcepath="ext/src/javax.servlet-api-3.0.1-sources.jar" />
-	<classpathentry kind="lib" path="ext/jetty-webapp-7.6.5.v20120716.jar" sourcepath="ext/src/jetty-webapp-7.6.5.v20120716-sources.jar" />
-	<classpathentry kind="lib" path="ext/jetty-ajp-7.6.5.v20120716.jar" sourcepath="ext/src/jetty-ajp-7.6.5.v20120716-sources.jar" />
-	<classpathentry kind="lib" path="ext/wicket-1.4.21.jar" sourcepath="ext/src/wicket-1.4.21-sources.jar" />
-	<classpathentry kind="lib" path="ext/wicket-auth-roles-1.4.21.jar" sourcepath="ext/src/wicket-auth-roles-1.4.21-sources.jar" />
-	<classpathentry kind="lib" path="ext/wicket-extensions-1.4.21.jar" sourcepath="ext/src/wicket-extensions-1.4.21-sources.jar" />
-	<classpathentry kind="lib" path="ext/googlecharts-1.4.21.jar" sourcepath="ext/src/googlecharts-1.4.21-sources.jar" />
-	<classpathentry kind="lib" path="ext/lucene-core-3.6.1.jar" sourcepath="ext/src/lucene-core-3.6.1-sources.jar" />
-	<classpathentry kind="lib" path="ext/lucene-highlighter-3.6.1.jar" sourcepath="ext/src/lucene-highlighter-3.6.1-sources.jar" />
-	<classpathentry kind="lib" path="ext/lucene-memory-3.6.1.jar" sourcepath="ext/src/lucene-memory-3.6.1-sources.jar" />
-	<classpathentry kind="lib" path="ext/lucene-queries-3.6.1.jar" sourcepath="ext/src/lucene-queries-3.6.1-sources.jar" />
-	<classpathentry kind="lib" path="ext/jakarta-regexp-1.4.jar" />
-	<classpathentry kind="lib" path="ext/markdownpapers-core-1.2.7.jar" sourcepath="ext/src/markdownpapers-core-1.2.7-sources.jar" />
-	<classpathentry kind="lib" path="ext/org.eclipse.jgit-2.1.0.201209190230-r.jar" sourcepath="ext/src/org.eclipse.jgit-2.1.0.201209190230-r-sources.jar" />
-	<classpathentry kind="lib" path="ext/jsch-0.1.44-1.jar" sourcepath="ext/src/jsch-0.1.44-1-sources.jar" />
-	<classpathentry kind="lib" path="ext/org.eclipse.jgit.http.server-2.1.0.201209190230-r.jar" sourcepath="ext/src/org.eclipse.jgit.http.server-2.1.0.201209190230-r-sources.jar" />
-	<classpathentry kind="lib" path="ext/bcprov-jdk16-1.46.jar" sourcepath="ext/src/bcprov-jdk16-1.46-sources.jar" />
-	<classpathentry kind="lib" path="ext/bcmail-jdk16-1.46.jar" sourcepath="ext/src/bcmail-jdk16-1.46-sources.jar" />
-	<classpathentry kind="lib" path="ext/rome-0.9.jar" sourcepath="ext/src/rome-0.9-sources.jar" />
-	<classpathentry kind="lib" path="ext/jdom-1.0.jar" sourcepath="ext/src/jdom-1.0-sources.jar" />
-	<classpathentry kind="lib" path="ext/gson-1.7.2.jar" sourcepath="ext/src/gson-1.7.2-sources.jar" />
-	<classpathentry kind="lib" path="ext/groovy-all-1.8.8.jar" sourcepath="ext/src/groovy-all-1.8.8-sources.jar" />
-	<classpathentry kind="lib" path="ext/unboundid-ldapsdk-2.3.0.jar" sourcepath="ext/src/unboundid-ldapsdk-2.3.0-sources.jar" />
-	<classpathentry kind="lib" path="ext/ivy-2.2.0.jar" sourcepath="ext/src/ivy-2.2.0-sources.jar" />
-	<classpathentry kind="lib" path="ext/junit-4.10.jar" sourcepath="ext/src/junit-4.10-sources.jar" />
-	<classpathentry kind="lib" path="ext/hamcrest-core-1.1.jar" />
-	<classpathentry kind="output" path="bin"/>
-	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+    <classpathentry kind="src" path="src"/>
+    <classpathentry kind="src" path="resources"/>
+    <classpathentry kind="src" path="tests"/>
+    <classpathentry kind="lib" path="ext/jcommander-1.17.jar" sourcepath="ext/src/jcommander-1.17-sources.jar" />
+    <classpathentry kind="lib" path="ext/log4j-1.2.17.jar" sourcepath="ext/src/log4j-1.2.17-sources.jar" />
+    <classpathentry kind="lib" path="ext/slf4j-api-1.6.6.jar" sourcepath="ext/src/slf4j-api-1.6.6-sources.jar" />
+    <classpathentry kind="lib" path="ext/slf4j-log4j12-1.6.6.jar" sourcepath="ext/src/slf4j-log4j12-1.6.6-sources.jar" />
+    <classpathentry kind="lib" path="ext/mail-1.4.3.jar" sourcepath="ext/src/mail-1.4.3-sources.jar" />
+    <classpathentry kind="lib" path="ext/javax.servlet-api-3.0.1.jar" sourcepath="ext/src/javax.servlet-api-3.0.1-sources.jar" />
+    <classpathentry kind="lib" path="ext/jetty-webapp-7.6.5.v20120716.jar" sourcepath="ext/src/jetty-webapp-7.6.5.v20120716-sources.jar" />
+    <classpathentry kind="lib" path="ext/jetty-ajp-7.6.5.v20120716.jar" sourcepath="ext/src/jetty-ajp-7.6.5.v20120716-sources.jar" />
+    <classpathentry kind="lib" path="ext/wicket-1.4.21.jar" sourcepath="ext/src/wicket-1.4.21-sources.jar" />
+    <classpathentry kind="lib" path="ext/wicket-auth-roles-1.4.21.jar" sourcepath="ext/src/wicket-auth-roles-1.4.21-sources.jar" />
+    <classpathentry kind="lib" path="ext/wicket-extensions-1.4.21.jar" sourcepath="ext/src/wicket-extensions-1.4.21-sources.jar" />
+    <classpathentry kind="lib" path="ext/googlecharts-1.4.21.jar" sourcepath="ext/src/googlecharts-1.4.21-sources.jar" />
+    <classpathentry kind="lib" path="ext/lucene-core-3.6.1.jar" sourcepath="ext/src/lucene-core-3.6.1-sources.jar" />
+    <classpathentry kind="lib" path="ext/lucene-highlighter-3.6.1.jar" sourcepath="ext/src/lucene-highlighter-3.6.1-sources.jar" />
+    <classpathentry kind="lib" path="ext/lucene-memory-3.6.1.jar" sourcepath="ext/src/lucene-memory-3.6.1-sources.jar" />
+    <classpathentry kind="lib" path="ext/lucene-queries-3.6.1.jar" sourcepath="ext/src/lucene-queries-3.6.1-sources.jar" />
+    <classpathentry kind="lib" path="ext/jakarta-regexp-1.4.jar" />
+    <classpathentry kind="lib" path="ext/markdownpapers-core-1.2.7.jar" sourcepath="ext/src/markdownpapers-core-1.2.7-sources.jar" />
+    <classpathentry kind="lib" path="ext/org.eclipse.jgit-2.1.0.201209190230-r.jar" sourcepath="ext/src/org.eclipse.jgit-2.1.0.201209190230-r-sources.jar" />
+    <classpathentry kind="lib" path="ext/jsch-0.1.44-1.jar" sourcepath="ext/src/jsch-0.1.44-1-sources.jar" />
+    <classpathentry kind="lib" path="ext/org.eclipse.jgit.http.server-2.1.0.201209190230-r.jar" sourcepath="ext/src/org.eclipse.jgit.http.server-2.1.0.201209190230-r-sources.jar" />
+    <classpathentry kind="lib" path="ext/bcprov-jdk16-1.46.jar" sourcepath="ext/src/bcprov-jdk16-1.46-sources.jar" />
+    <classpathentry kind="lib" path="ext/bcmail-jdk16-1.46.jar" sourcepath="ext/src/bcmail-jdk16-1.46-sources.jar" />
+    <classpathentry kind="lib" path="ext/rome-0.9.jar" sourcepath="ext/src/rome-0.9-sources.jar" />
+    <classpathentry kind="lib" path="ext/jdom-1.0.jar" sourcepath="ext/src/jdom-1.0-sources.jar" />
+    <classpathentry kind="lib" path="ext/gson-1.7.2.jar" sourcepath="ext/src/gson-1.7.2-sources.jar" />
+    <classpathentry kind="lib" path="ext/groovy-all-1.8.8.jar" sourcepath="ext/src/groovy-all-1.8.8-sources.jar" />
+    <classpathentry kind="lib" path="ext/unboundid-ldapsdk-2.3.0.jar" sourcepath="ext/src/unboundid-ldapsdk-2.3.0-sources.jar" />
+    <classpathentry kind="lib" path="ext/ivy-2.2.0.jar" sourcepath="ext/src/ivy-2.2.0-sources.jar" />
+    <classpathentry kind="lib" path="ext/junit-4.10.jar" sourcepath="ext/src/junit-4.10-sources.jar" />
+    <classpathentry kind="lib" path="ext/hamcrest-core-1.1.jar" />
+    <classpathentry kind="output" path="bin"/>
+    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
 </classpath>
\ No newline at end of file
diff --git a/src/com/gitblit/build/Build.java b/src/com/gitblit/build/Build.java
index 316b2a6..5788b25 100644
--- a/src/com/gitblit/build/Build.java
+++ b/src/com/gitblit/build/Build.java
@@ -20,6 +20,7 @@
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileWriter;
+import java.io.FilenameFilter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.RandomAccessFile;
@@ -379,6 +380,7 @@
 			
 			if (targetFile.exists()) {
 				downloads.add(targetFile);
+				removeObsoleteArtifacts(mo, type, targetFile.getParentFile());
 				continue;
 			}
 			String expectedSHA1 = mo.getSHA1(jar);
@@ -454,9 +456,41 @@
 				throw new RuntimeException("Error writing to file " + targetFile, e);
 			}
 			downloads.add(targetFile);
+			
+			removeObsoleteArtifacts(mo, type, targetFile.getParentFile());
 		}
 		return downloads;
 	}
+	
+	private static void removeObsoleteArtifacts(final MavenObject mo, final BuildType type, File folder) {
+		File [] removals = folder.listFiles(new FilenameFilter() {
+			@Override
+			public boolean accept(File dir, String name) {
+				String n = name.toLowerCase();
+				String dep = mo.artifact.toLowerCase();
+				if (n.startsWith(dep)) {
+					String suffix = "-" + mo.version;
+					if (type.equals(BuildType.COMPILETIME)) {
+						suffix += "-sources.jar";
+					} else {
+						suffix += ".jar";
+					}
+					if (!n.endsWith(suffix)) {
+						return true;
+					}
+				}
+				return false;
+			}
+		});
+		
+		// delete any matches
+		if (removals != null) {
+			for (File file : removals) {
+				System.out.println("deleting " + file);
+				file.delete();
+			}
+		}
+	}
 
 	private static void updateDownload(float progress, File file) {
 		updateProgress(progress, "d/l: " + file.getName());

--
Gitblit v1.9.1