failonerror |
- Log a warning message, but do not stop the
+ | If false, log a warning message, but do not stop the
build, when the file to copy does not exist or one of the nested
filesets points to a directory that doesn't exist or an error occurs
while moving.
@@ -154,6 +158,10 @@ followed by <filterset> elements.
<move todir="new/dir/to/move/to">
<fileset dir="src/dir"/>
</move>
+
+ or, since Ant 1.6.3:
+
+ <move file="src/dir" tofile="new/dir/to/move/to" />
Move a set of files to a new directory
diff --git a/src/etc/testcases/taskdefs/move.xml b/src/etc/testcases/taskdefs/move.xml
index abdc09202..1cd941227 100644
--- a/src/etc/testcases/taskdefs/move.xml
+++ b/src/etc/testcases/taskdefs/move.xml
@@ -59,11 +59,78 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
diff --git a/src/main/org/apache/tools/ant/taskdefs/Move.java b/src/main/org/apache/tools/ant/taskdefs/Move.java
index 893cacd60..d5bd9fa68 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Move.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Move.java
@@ -61,6 +61,58 @@ public class Move extends Copy {
setOverwrite(true);
}
+ /**
+ * Performs the move operation.
+ */
+ public void execute() throws BuildException {
+ if (file != null && file.isDirectory()) {
+ if (destFile != null && destDir != null) {
+ throw new BuildException("Only one of tofile and todir "
+ + "may be set.");
+ }
+
+ if (destFile == null && destDir == null) {
+ throw new BuildException("One of tofile or todir must be set.");
+ }
+
+ destFile = (destFile != null)
+ ? destFile : new File(destDir, file.getName());
+
+ try {
+ boolean renamed = false;
+ log("Moving directory " + file
+ + " to " + destFile, Project.MSG_INFO);
+ try {
+ renamed =
+ renameFile(file, destFile, filtering, forceOverwrite);
+ } catch (IOException eyeOhEx) {
+ throw new BuildException(eyeOhEx.getMessage());
+ }
+ if (!renamed) {
+ StringBuffer buf = new StringBuffer(
+ "Failed to move directory ").append(
+ file.getAbsolutePath());
+
+ if ((getFilterChains() != null && getFilterChains().size() > 0)
+ || (getFilterSets() != null && getFilterSets().size() > 0)
+ || filtering) {
+ buf.append(
+ "; use a fileset to move directories with filtering");
+ }
+ throw new BuildException(buf.append('.').toString());
+ }
+ } catch (BuildException e) {
+ if (!failonerror) {
+ log("Warning: " + e.getMessage(), Project.MSG_ERR);
+ } else {
+ throw e;
+ }
+ }
+ } else {
+ super.execute();
+ }
+ }
+
//************************************************************************
// protected and private methods
//************************************************************************
@@ -325,17 +377,19 @@ public class Move extends Copy {
} else {
if (!filtering) {
// ensure that parent dir of dest file exists!
- // not using getParentFile method to stay 1.1 compatibility
- String parentPath = destFile.getParent();
- if (parentPath != null) {
- File parent = new File(parentPath);
- if (!parent.exists()) {
- parent.mkdirs();
- }
+ File parent = destFile.getParentFile();
+ if (parent != null && !parent.exists()) {
+ parent.mkdirs();
}
- if (destFile.exists() && destFile.isFile()) {
- if (!destFile.delete()) {
+ if (destFile.exists()) {
+ if (sourceFile.isDirectory()) {
+ throw new BuildException(
+ new StringBuffer("Cannot replace ").append(
+ ((destFile.isFile()) ? "file " : "directory ")).append(
+ destFile).append(" with directory ").append(
+ sourceFile).toString());
+ } else if (destFile.isFile() && !destFile.delete()) {
throw new BuildException("Unable to remove existing "
+ "file " + destFile);
}
diff --git a/src/testcases/org/apache/tools/ant/taskdefs/MoveTest.java b/src/testcases/org/apache/tools/ant/taskdefs/MoveTest.java
index 6d510423f..45b711ca0 100644
--- a/src/testcases/org/apache/tools/ant/taskdefs/MoveTest.java
+++ b/src/testcases/org/apache/tools/ant/taskdefs/MoveTest.java
@@ -88,4 +88,33 @@ public class MoveTest extends BuildFileTest {
assertTrue(!getProject().resolveFile("A/1").exists());
assertTrue(!getProject().resolveFile("A").exists());
}
+
+ public void testCompleteDirectoryMoveFileToFile() {
+ executeTarget("testCompleteDirectoryMoveFileToFile");
+ }
+
+ public void testCompleteDirectoryMoveFileToDir() {
+ executeTarget("testCompleteDirectoryMoveFileToDir");
+ }
+
+ public void testCompleteDirectoryMoveFileToExistingFile() {
+ expectBuildExceptionContaining("testCompleteDirectoryMoveFileToExistingFile",
+ "", "Cannot replace file");
+ }
+
+ public void testCompleteDirectoryMoveFileToExistingDir() {
+ expectBuildExceptionContaining("testCompleteDirectoryMoveFileToExistingDir",
+ "", "Cannot replace directory");
+ }
+
+ public void testCompleteDirectoryMoveFileToDirWithExistingFile() {
+ expectBuildExceptionContaining("testCompleteDirectoryMoveFileToDirWithExistingFile",
+ "", "Cannot replace file");
+ }
+
+ public void testCompleteDirectoryMoveFileToDirWithExistingDir() {
+ expectBuildExceptionContaining("testCompleteDirectoryMoveFileToDirWithExistingDir",
+ "", "Cannot replace directory");
+ }
+
}
|