You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Copydir.java 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. * @since Ant 1.1
  29. *
  30. * @deprecated The copydir task is deprecated since Ant 1.2. Use copy instead.
  31. */
  32. public class Copydir extends MatchingTask {
  33. private File srcDir;
  34. private File destDir;
  35. private boolean filtering = false;
  36. private boolean flatten = false;
  37. private boolean forceOverwrite = false;
  38. private Hashtable filecopyList = new Hashtable();
  39. /**
  40. * The src attribute
  41. *
  42. * @param src the source file
  43. */
  44. public void setSrc(File src) {
  45. srcDir = src;
  46. }
  47. /**
  48. * The dest attribute
  49. *
  50. * @param dest the destination file
  51. */
  52. public void setDest(File dest) {
  53. destDir = dest;
  54. }
  55. public void setFiltering(boolean filter) {
  56. filtering = filter;
  57. }
  58. public void setFlatten(boolean flatten) {
  59. this.flatten = flatten;
  60. }
  61. public void setForceoverwrite(boolean force) {
  62. forceOverwrite = force;
  63. }
  64. public void execute() throws BuildException {
  65. log("DEPRECATED - The copydir task is deprecated. Use copy instead.");
  66. if (srcDir == null) {
  67. throw new BuildException("src attribute must be set!",
  68. getLocation());
  69. }
  70. if (!srcDir.exists()) {
  71. throw new BuildException("srcdir " + srcDir.toString()
  72. + " does not exist!", getLocation());
  73. }
  74. if (destDir == null) {
  75. throw new BuildException("The dest attribute must be set.",
  76. getLocation());
  77. }
  78. if (srcDir.equals(destDir)) {
  79. log("Warning: src == dest", Project.MSG_WARN);
  80. }
  81. DirectoryScanner ds = super.getDirectoryScanner(srcDir);
  82. try {
  83. String[] files = ds.getIncludedFiles();
  84. scanDir(srcDir, destDir, files);
  85. if (filecopyList.size() > 0) {
  86. log("Copying " + filecopyList.size() + " file"
  87. + (filecopyList.size() == 1 ? "" : "s")
  88. + " to " + destDir.getAbsolutePath());
  89. Enumeration e = filecopyList.keys();
  90. while (e.hasMoreElements()) {
  91. String fromFile = (String) e.nextElement();
  92. String toFile = (String) filecopyList.get(fromFile);
  93. try {
  94. getProject().copyFile(fromFile, toFile, filtering,
  95. forceOverwrite);
  96. } catch (IOException ioe) {
  97. String msg = "Failed to copy " + fromFile + " to "
  98. + toFile + " due to " + ioe.getMessage();
  99. throw new BuildException(msg, ioe, getLocation());
  100. }
  101. }
  102. }
  103. } finally {
  104. filecopyList.clear();
  105. }
  106. }
  107. private void scanDir(File from, File to, String[] files) {
  108. for (int i = 0; i < files.length; i++) {
  109. String filename = files[i];
  110. File srcFile = new File(from, filename);
  111. File destFile;
  112. if (flatten) {
  113. destFile = new File(to, new File(filename).getName());
  114. } else {
  115. destFile = new File(to, filename);
  116. }
  117. if (forceOverwrite
  118. || (srcFile.lastModified() > destFile.lastModified())) {
  119. filecopyList.put(srcFile.getAbsolutePath(),
  120. destFile.getAbsolutePath());
  121. }
  122. }
  123. }
  124. }