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 5.1 kB

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