1. /*
  2. * Copyright 2000,2002-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.util.Enumeration;
  21. import java.util.Hashtable;
  22. import org.apache.tools.ant.BuildException;
  23. import org.apache.tools.ant.DirectoryScanner;
  24. import org.apache.tools.ant.Project;
  25. /**
  26. * Copies a directory.
  27. *
  28. *
  29. * @since Ant 1.1
  30. *
  31. * @deprecated The copydir task is deprecated since Ant 1.2. Use copy instead.
  32. */
  33. public class Copydir extends MatchingTask {
  34. private File srcDir;
  35. private File destDir;
  36. private boolean filtering = false;
  37. private boolean flatten = false;
  38. private boolean forceOverwrite = false;
  39. private Hashtable filecopyList = new Hashtable();
  40. /**
  41. * The src attribute
  42. *
  43. * @param src the source file
  44. */
  45. public void setSrc(File src) {
  46. srcDir = src;
  47. }
  48. /**
  49. * The dest attribute
  50. *
  51. * @param dest the destination file
  52. */
  53. public void setDest(File dest) {
  54. destDir = dest;
  55. }
  56. public void setFiltering(boolean filter) {
  57. filtering = filter;
  58. }
  59. public void setFlatten(boolean flatten) {
  60. this.flatten = flatten;
  61. }
  62. public void setForceoverwrite(boolean force) {
  63. forceOverwrite = force;
  64. }
  65. public void execute() throws BuildException {
  66. log("DEPRECATED - The copydir task is deprecated. Use copy instead.");
  67. if (srcDir == null) {
  68. throw new BuildException("src attribute must be set!",
  69. getLocation());
  70. }
  71. if (!srcDir.exists()) {
  72. throw new BuildException("srcdir " + srcDir.toString()
  73. + " does not exist!", getLocation());
  74. }
  75. if (destDir == null) {
  76. throw new BuildException("The dest attribute must be set.",
  77. getLocation());
  78. }
  79. if (srcDir.equals(destDir)) {
  80. log("Warning: src == dest", Project.MSG_WARN);
  81. }
  82. DirectoryScanner ds = super.getDirectoryScanner(srcDir);
  83. try {
  84. String[] files = ds.getIncludedFiles();
  85. scanDir(srcDir, destDir, files);
  86. if (filecopyList.size() > 0) {
  87. log("Copying " + filecopyList.size() + " file"
  88. + (filecopyList.size() == 1 ? "" : "s")
  89. + " to " + destDir.getAbsolutePath());
  90. Enumeration e = filecopyList.keys();
  91. while (e.hasMoreElements()) {
  92. String fromFile = (String) e.nextElement();
  93. String toFile = (String) filecopyList.get(fromFile);
  94. try {
  95. getProject().copyFile(fromFile, toFile, filtering,
  96. forceOverwrite);
  97. } catch (IOException ioe) {
  98. String msg = "Failed to copy " + fromFile + " to "
  99. + toFile + " due to " + ioe.getMessage();
  100. throw new BuildException(msg, ioe, getLocation());
  101. }
  102. }
  103. }
  104. } finally {
  105. filecopyList.clear();
  106. }
  107. }
  108. private void scanDir(File from, File to, String[] files) {
  109. for (int i = 0; i < files.length; i++) {
  110. String filename = files[i];
  111. File srcFile = new File(from, filename);
  112. File destFile;
  113. if (flatten) {
  114. destFile = new File(to, new File(filename).getName());
  115. } else {
  116. destFile = new File(to, filename);
  117. }
  118. if (forceOverwrite
  119. || (srcFile.lastModified() > destFile.lastModified())) {
  120. filecopyList.put(srcFile.getAbsolutePath(),
  121. destFile.getAbsolutePath());
  122. }
  123. }
  124. }
  125. }