From e770282ba4267d4589093affac30f827d4f79edf Mon Sep 17 00:00:00 2001 From: "andreas.janning" Date: Tue, 12 Mar 2019 11:46:52 +0100 Subject: [PATCH] Fix #670: Use Files.move instead of File.rename to move files because File.rename does not support overwriting existing files on windows --- .../maven/plugins/frontend/lib/NodeInstaller.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/NodeInstaller.java b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/NodeInstaller.java index 302f691a2..474fce183 100644 --- a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/NodeInstaller.java +++ b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/NodeInstaller.java @@ -3,6 +3,8 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; import java.util.Arrays; import org.apache.commons.io.FileUtils; @@ -151,7 +153,12 @@ private void installNodeDefault() throws InstallationException { File destination = new File(destinationDirectory, "node"); this.logger.info("Copying node binary from {} to {}", nodeBinary, destination); - if (!nodeBinary.renameTo(destination)) { + if (destination.exists() && !destination.delete()) { + throw new InstallationException("Could not install Node: Was not allowed to delete " + destination); + } + try { + Files.move(nodeBinary.toPath(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING); + } catch (IOException e) { throw new InstallationException("Could not install Node: Was not allowed to rename " + nodeBinary + " to " + destination); } @@ -219,7 +226,9 @@ private void installNodeWithNpmForWindows() throws InstallationException { File destination = new File(destinationDirectory, "node.exe"); this.logger.info("Copying node binary from {} to {}", nodeBinary, destination); - if (!nodeBinary.renameTo(destination)) { + try { + Files.move(nodeBinary.toPath(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING); + } catch (IOException e) { throw new InstallationException("Could not install Node: Was not allowed to rename " + nodeBinary + " to " + destination); }