fix Unable to Synchronize Maven snapshots while program running

Change-Id: I252748d7e87911c082cf25444f7d9d0c28bd3cb2
Signed-off-by: jkubitz <jkubitz-eclipse@gmx.de>
diff --git a/org.eclipse.tea.library.build/src/org/eclipse/tea/library/build/tasks/maven/SynchronizeMavenArtifact.java b/org.eclipse.tea.library.build/src/org/eclipse/tea/library/build/tasks/maven/SynchronizeMavenArtifact.java
index 5e47dd4..4caa0ed 100644
--- a/org.eclipse.tea.library.build/src/org/eclipse/tea/library/build/tasks/maven/SynchronizeMavenArtifact.java
+++ b/org.eclipse.tea.library.build/src/org/eclipse/tea/library/build/tasks/maven/SynchronizeMavenArtifact.java
@@ -299,14 +299,29 @@
 			}
 		}
 
-		// force refresh of file/link in any case.
-		FileUtils.delete(targetFile);
-
+		boolean copyNeeded = true;
+		// refresh of file/link:
 		try {
-			targetFile = java.nio.file.Files.createSymbolicLink(targetFile.toPath(), file.toPath()).toFile();
-			log.info("symlink created: " + targetFile);
-		} catch (IOException e) {
-			FileUtils.copyFileToDirectory(file, target);
+			FileUtils.delete(targetFile);
+		} catch (java.lang.IllegalStateException e) { // cannot delete
+			if (FileUtils.equals(targetFile, file)) {
+				// ignore
+				log.info("Could not update file. Probably in use. Can be ignored since contents is the same: "
+						+ targetFile);
+				copyNeeded = false;
+			} else {
+				throw e;
+			}
+		}
+
+		if (copyNeeded) {
+			try {
+				targetFile = java.nio.file.Files.createSymbolicLink(targetFile.toPath(), file.toPath()).toFile();
+			} catch (IOException e) {
+				log.info("Failed to create symlink for: " + targetFile + " (" + e.getClass().getName() + " "
+						+ e.getMessage() + ")");
+				FileUtils.copyFileToDirectory(file, target);
+			}
 		}
 	}
 
diff --git a/org.eclipse.tea.library.build/src/org/eclipse/tea/library/build/util/FileUtils.java b/org.eclipse.tea.library.build/src/org/eclipse/tea/library/build/util/FileUtils.java
index aed91f6..6a405f6 100644
--- a/org.eclipse.tea.library.build/src/org/eclipse/tea/library/build/util/FileUtils.java
+++ b/org.eclipse.tea.library.build/src/org/eclipse/tea/library/build/util/FileUtils.java
@@ -26,6 +26,7 @@
 import java.nio.charset.Charset;
 import java.nio.file.Files;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 import java.util.Properties;
@@ -486,4 +487,14 @@
 		return r;
 	}
 
+	public static boolean equals(File f1, File f2) {
+		try {
+			byte[] b1 = java.nio.file.Files.readAllBytes(f1.toPath());
+			byte[] b2 = java.nio.file.Files.readAllBytes(f2.toPath());
+			return Arrays.equals(b1, b2);
+		} catch (IOException e) {
+			return false;
+		}
+	}
+
 }