[306732] Update Tomcat publishing to handle dependency removal when the deploy path included a customized file name.
diff --git a/plugins/org.eclipse.jst.server.tomcat.core/tomcatcore/org/eclipse/jst/server/tomcat/core/internal/PublishOperation2.java b/plugins/org.eclipse.jst.server.tomcat.core/tomcatcore/org/eclipse/jst/server/tomcat/core/internal/PublishOperation2.java
index ede75bc..286e472 100644
--- a/plugins/org.eclipse.jst.server.tomcat.core/tomcatcore/org/eclipse/jst/server/tomcat/core/internal/PublishOperation2.java
+++ b/plugins/org.eclipse.jst.server.tomcat.core/tomcatcore/org/eclipse/jst/server/tomcat/core/internal/PublishOperation2.java
@@ -13,6 +13,7 @@
 import java.io.File;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Properties;
 
 import org.eclipse.core.runtime.*;
 import org.eclipse.jst.server.core.IJ2EEModule;
@@ -82,6 +83,8 @@
 		}
 		// Else a child module
 		else {
+			Properties p = server.loadModulePublishLocations();
+
 			// Try to determine the URI for the child module
 			IWebModule webModule = (IWebModule)module[0].loadAdapter(IWebModule.class, monitor);
 			String childURI = null;
@@ -96,11 +99,12 @@
 			}
 
 			if (isBinary) {
-				publishArchiveModule(childURI, status, monitor);
+				publishArchiveModule(childURI, p, status, monitor);
 			}
 			else {
-				publishJar(childURI, status, monitor);
+				publishJar(childURI, p, status, monitor);
 			}
+			server.saveModulePublishLocations(p);
 		}
 		throwException(status);
 		server.setModulePublishState2(module, IServer.PUBLISH_STATE_NONE);
@@ -139,26 +143,43 @@
 		}
 	}
 
-	private void publishJar(String jarURI, List status, IProgressMonitor monitor) throws CoreException {
+	private void publishJar(String jarURI, Properties p, List status, IProgressMonitor monitor) throws CoreException {
 		IPath path = server.getModuleDeployDirectory(module[0]);
+		boolean moving = false;
+		// Get URI used for previous publish, if known
+		String oldURI = (String)p.get(module[1].getId());
+		if (oldURI != null) {
+			// If old URI found, detect if jar is moving or changing its name
+			if (jarURI != null) {
+				moving = !oldURI.equals(jarURI);
+			}
+		}
+		// If we don't have a jar URI, make a guess so we have one if we need it
 		if (jarURI == null) {
 			jarURI = "WEB-INF/lib/" + module[1].getName() + ".jar";
 		}
 		IPath jarPath = path.append(jarURI);
+		// Make our best determination of the path to the old jar
+		IPath oldJarPath = jarPath;
+		if (oldURI != null) {
+			oldJarPath = path.append(oldURI);
+		}
+		// Establish the destination directory
 		path = jarPath.removeLastSegments(1);
 		
 		// Remove if requested or if previously published and are now serving without publishing
-		if (kind == IServer.PUBLISH_CLEAN || deltaKind == ServerBehaviourDelegate.REMOVED
+		if (moving || kind == IServer.PUBLISH_CLEAN || deltaKind == ServerBehaviourDelegate.REMOVED
 				|| server.getTomcatServer().isServeModulesWithoutPublish()) {
-			File file = jarPath.toFile();
+			File file = oldJarPath.toFile();
 			if (file.exists())
 				file.delete();
-			
+			p.remove(module[1].getId());
+
 			if (deltaKind == ServerBehaviourDelegate.REMOVED
 					|| server.getTomcatServer().isServeModulesWithoutPublish())
 				return;
 		}
-		if (kind != IServer.PUBLISH_CLEAN && kind != IServer.PUBLISH_FULL) {
+		if (!moving && kind != IServer.PUBLISH_CLEAN && kind != IServer.PUBLISH_FULL) {
 			// avoid changes if no changes to module since last publish
 			IModuleResourceDelta[] delta = server.getPublishedResourceDelta(module);
 			if (delta == null || delta.length == 0)
@@ -172,43 +193,62 @@
 		IModuleResource[] mr = server.getResources(module);
 		IStatus[] stat = helper.publishZip(mr, jarPath, monitor);
 		addArrayToList(status, stat);
+		p.put(module[1].getId(), jarURI);
 	}
 
-	private void publishArchiveModule(String jarURI, List status, IProgressMonitor monitor) {
+	private void publishArchiveModule(String jarURI, Properties p, List status, IProgressMonitor monitor) {
 		IPath path = server.getModuleDeployDirectory(module[0]);
+		boolean moving = false;
+		// Get URI used for previous publish, if known
+		String oldURI = (String)p.get(module[1].getId());
+		if (oldURI != null) {
+			// If old URI found, detect if jar is moving or changing its name
+			if (jarURI != null) {
+				moving = !oldURI.equals(jarURI);
+			}
+		}
+		// If we don't have a jar URI, make a guess so we have one if we need it
 		if (jarURI == null) {
 			jarURI = "WEB-INF/lib/" + module[1].getName();
 		}
 		IPath jarPath = path.append(jarURI);
+		// Make our best determination of the path to the old jar
+		IPath oldJarPath = jarPath;
+		if (oldURI != null) {
+			oldJarPath = path.append(oldURI);
+		}
+		// Establish the destination directory
 		path = jarPath.removeLastSegments(1);
 
 		// Remove if requested or if previously published and are now serving without publishing
-		if (kind == IServer.PUBLISH_CLEAN || deltaKind == ServerBehaviourDelegate.REMOVED
+		if (moving || kind == IServer.PUBLISH_CLEAN || deltaKind == ServerBehaviourDelegate.REMOVED
 				|| server.getTomcatServer().isServeModulesWithoutPublish()) {
-			File file = jarPath.toFile();
+			File file = oldJarPath.toFile();
 			if (file.exists()) {
 				file.delete();
 			}
+			p.remove(module[1].getId());
 			
 			if (deltaKind == ServerBehaviourDelegate.REMOVED
 					|| server.getTomcatServer().isServeModulesWithoutPublish())
 				return;
 		}
-		
-		if (kind == IServer.PUBLISH_CLEAN || kind == IServer.PUBLISH_FULL) {
-			IModuleResource[] mr = server.getResources(module);
-			IStatus[] stat = helper.publishFull(mr, path, monitor);
-			addArrayToList(status, stat);
-			return;
+		if (!moving && kind != IServer.PUBLISH_CLEAN && kind != IServer.PUBLISH_FULL) {
+			// avoid changes if no changes to module since last publish
+			IModuleResourceDelta[] delta = server.getPublishedResourceDelta(module);
+			if (delta == null || delta.length == 0)
+				return;
 		}
-		
-		IModuleResourceDelta[] delta = server.getPublishedResourceDelta(module);
-		
-		int size = delta.length;
-		for (int i = 0; i < size; i++) {
-			IStatus[] stat = helper.publishDelta(delta[i], path, monitor);
-			addArrayToList(status, stat);
-		}
+
+		// make directory if it doesn't exist
+		if (!path.toFile().exists())
+			path.toFile().mkdirs();
+
+		IModuleResource[] mr = server.getResources(module);
+		// XXX This doesn't honor the name of the jar in the URI if it differs
+		IStatus[] stat = helper.publishFull(mr, path, monitor);
+		addArrayToList(status, stat);
+		p.put(module[1].getId(), jarURI);
 	}
 
 	/**
diff --git a/plugins/org.eclipse.jst.server.tomcat.core/tomcatcore/org/eclipse/jst/server/tomcat/core/internal/TomcatServerBehaviour.java b/plugins/org.eclipse.jst.server.tomcat.core/tomcatcore/org/eclipse/jst/server/tomcat/core/internal/TomcatServerBehaviour.java
index 6c96d8d..fb4ec5a 100644
--- a/plugins/org.eclipse.jst.server.tomcat.core/tomcatcore/org/eclipse/jst/server/tomcat/core/internal/TomcatServerBehaviour.java
+++ b/plugins/org.eclipse.jst.server.tomcat.core/tomcatcore/org/eclipse/jst/server/tomcat/core/internal/TomcatServerBehaviour.java
@@ -260,21 +260,7 @@
 		if (getTomcatServer().isTestEnvironment())
 			return;
 
-		IPath path = getTempDirectory().append("publish.txt");
-		Properties p = new Properties();
-		FileInputStream fin = null;
-		try {
-			fin = new FileInputStream(path.toFile());
-			p.load(fin);
-		} catch (Exception e) {
-			// ignore
-		} finally {
-			try {
-				fin.close();
-			} catch (Exception ex) {
-				// ignore
-			}
-		}
+		Properties p = loadModulePublishLocations();
 		
 		PublishHelper helper = new PublishHelper(getRuntimeBaseDirectory().append("temp").toFile());
 		// If parent web module
@@ -306,11 +292,7 @@
 		
 		setModulePublishState(moduleTree, IServer.PUBLISH_STATE_NONE);
 		
-		try {
-			p.store(new FileOutputStream(path.toFile()), "Tomcat publish data");
-		} catch (Exception e) {
-			// ignore
-		}
+		saveModulePublishLocations(p);
 	}
 
 	/**
@@ -452,6 +434,7 @@
 			}
 			
 			IModuleResource[] mr = getResources(module);
+			// XXX This doesn't honor the name of the jar in the URI if it differs
 			IStatus[] stat = helper.publishFull(mr, path, monitor);
 			PublishOperation2.addArrayToList(status, stat);
 			PublishOperation2.throwException(status);
@@ -1151,4 +1134,41 @@
 	public void setModulePublishState2(IModule[] module, int state) {
 		setModulePublishState(module, state);
 	}
+	
+	public Properties loadModulePublishLocations() {
+		Properties p = new Properties();
+		IPath path = getTempDirectory().append("publish.txt");
+		FileInputStream fin = null;
+		try {
+			fin = new FileInputStream(path.toFile());
+			p.load(fin);
+		} catch (Exception e) {
+			// ignore
+		} finally {
+			try {
+				fin.close();
+			} catch (Exception ex) {
+				// ignore
+			}
+		}
+		return p;
+	}
+	
+	public void saveModulePublishLocations(Properties p) {
+		IPath path = getTempDirectory().append("publish.txt");
+		FileOutputStream fout = null;
+		try {
+			fout = new FileOutputStream(path.toFile());
+			p.store(fout, "Tomcat publish data");
+		} catch (Exception e) {
+			// ignore
+		}
+		finally {
+			try {
+				fout.close();
+			} catch (Exception ex) {
+				// ignore
+			}
+		}
+	}
 }