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.

Copy.java 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import org.apache.tools.ant.*;
  56. import org.apache.tools.ant.types.*;
  57. import java.io.*;
  58. import java.util.*;
  59. /**
  60. * A consolidated copy task. Copies a file or directory to a new file
  61. * or directory. Files are only copied if the source file is newer
  62. * than the destination file, or when the destination file does not
  63. * exist. It is possible to explicitly overwrite existing files.</p>
  64. *
  65. * <p>This implementation is based on Arnout Kuiper's initial design
  66. * document, the following mailing list discussions, and the
  67. * copyfile/copydir tasks.</p>
  68. *
  69. * @author Glenn McAllister <a href="mailto:glennm@ca.ibm.com">glennm@ca.ibm.com</a>
  70. */
  71. public class Copy extends Task {
  72. protected File file = null; // the source file
  73. protected File destFile = null; // the destination file
  74. protected File destDir = null; // the destination directory
  75. protected Vector filesets = new Vector();
  76. protected boolean filtering = false;
  77. protected boolean forceOverwrite = false;
  78. protected boolean flatten = false;
  79. protected int verbosity = Project.MSG_VERBOSE;
  80. protected boolean includeEmpty = true;
  81. protected Hashtable fileCopyMap = new Hashtable();
  82. protected Hashtable dirCopyMap = new Hashtable();
  83. /**
  84. * Sets a single source file to copy.
  85. */
  86. public void setFile(File file) {
  87. this.file = file;
  88. }
  89. /**
  90. * Sets the destination file.
  91. */
  92. public void setTofile(File destFile) {
  93. this.destFile = destFile;
  94. }
  95. /**
  96. * Sets the destination directory.
  97. */
  98. public void setTodir(File destDir) {
  99. this.destDir = destDir;
  100. }
  101. /**
  102. * Sets filtering.
  103. */
  104. public void setFiltering(boolean filtering) {
  105. this.filtering = filtering;
  106. }
  107. /**
  108. * Overwrite any existing destination file(s).
  109. */
  110. public void setOverwrite(boolean overwrite) {
  111. this.forceOverwrite = overwrite;
  112. }
  113. /**
  114. * When copying directory trees, the files can be "flattened"
  115. * into a single directory. If there are multiple files with
  116. * the same name in the source directory tree, only the first
  117. * file will be copied into the "flattened" directory, unless
  118. * the forceoverwrite attribute is true.
  119. */
  120. public void setFlatten(boolean flatten) {
  121. this.flatten = flatten;
  122. }
  123. /**
  124. * Used to force listing of all names of copied files.
  125. */
  126. public void setVerbose(boolean verbose) {
  127. if (verbose) {
  128. this.verbosity = Project.MSG_INFO;
  129. } else {
  130. this.verbosity = Project.MSG_VERBOSE;
  131. }
  132. }
  133. /**
  134. * Used to copy empty directories.
  135. */
  136. public void setIncludeEmptyDirs(boolean includeEmpty) {
  137. this.includeEmpty = includeEmpty;
  138. }
  139. /**
  140. * Adds a set of files (nested fileset attribute).
  141. */
  142. public void addFileset(FileSet set) {
  143. filesets.addElement(set);
  144. }
  145. /**
  146. * Performs the copy operation.
  147. */
  148. public void execute() throws BuildException {
  149. // make sure we don't have an illegal set of options
  150. validateAttributes();
  151. // deal with the single file
  152. if (file != null) {
  153. if (file.exists()) {
  154. if (destFile == null) {
  155. destFile = new File(destDir, file.getName());
  156. }
  157. if (forceOverwrite ||
  158. (file.lastModified() > destFile.lastModified())) {
  159. fileCopyMap.put(file.getAbsolutePath(), destFile.getAbsolutePath());
  160. }
  161. } else {
  162. log("Could not find file " + file.getAbsolutePath() + " to copy.");
  163. }
  164. }
  165. // deal with the filesets
  166. for (int i=0; i<filesets.size(); i++) {
  167. FileSet fs = (FileSet) filesets.elementAt(i);
  168. DirectoryScanner ds = fs.getDirectoryScanner(project);
  169. File fromDir = fs.getDir(project);
  170. String[] srcFiles = ds.getIncludedFiles();
  171. String[] srcDirs = ds.getIncludedDirectories();
  172. scan(fromDir, destDir, srcFiles, srcDirs);
  173. }
  174. // do all the copy operations now...
  175. doFileOperations();
  176. // clean up destDir again - so this instance can be used a second
  177. // time without throwing an exception
  178. if (destFile != null) {
  179. destDir = null;
  180. }
  181. }
  182. //************************************************************************
  183. // protected and private methods
  184. //************************************************************************
  185. /**
  186. * Ensure we have a consistent and legal set of attributes, and set
  187. * any internal flags necessary based on different combinations
  188. * of attributes.
  189. */
  190. protected void validateAttributes() throws BuildException {
  191. if (file == null && filesets.size() == 0) {
  192. throw new BuildException("Specify at least one source - a file or a fileset.");
  193. }
  194. if (destFile != null && destDir != null) {
  195. throw new BuildException("Only one of destfile and destdir may be set.");
  196. }
  197. if (destFile == null && destDir == null) {
  198. throw new BuildException("One of destfile or destdir must be set.");
  199. }
  200. if (file != null && file.exists() && file.isDirectory()) {
  201. throw new BuildException("Use a fileset to copy directories.");
  202. }
  203. if (destFile != null && filesets.size() > 0) {
  204. throw new BuildException("Cannot concatenate multple files into a single file.");
  205. }
  206. if (destFile != null) {
  207. destDir = new File(destFile.getParent()); // be 1.1 friendly
  208. }
  209. }
  210. /**
  211. * Compares source files to destination files to see if they should be
  212. * copied.
  213. */
  214. protected void scan(File fromDir, File toDir, String[] files, String[] dirs) {
  215. for (int i = 0; i < files.length; i++) {
  216. String filename = files[i];
  217. File src = new File(fromDir, filename);
  218. File dest;
  219. if (flatten) {
  220. dest = new File(toDir, new File(filename).getName());
  221. } else {
  222. dest = new File(toDir, filename);
  223. }
  224. if (forceOverwrite ||
  225. (src.lastModified() > dest.lastModified())) {
  226. fileCopyMap.put(src.getAbsolutePath(),
  227. dest.getAbsolutePath());
  228. }
  229. }
  230. if (includeEmpty) {
  231. for (int i = 0; i < dirs.length; i++) {
  232. String dname = dirs[i];
  233. File sd = new File(fromDir, dname);
  234. File dd;
  235. if (flatten) {
  236. dd = new File(toDir, new File(dname).getName());
  237. } else {
  238. dd = new File(toDir, dname);
  239. }
  240. if (forceOverwrite || (sd.lastModified() > dd.lastModified())) {
  241. dirCopyMap.put(sd.getAbsolutePath(), dd.getAbsolutePath());
  242. }
  243. }
  244. }
  245. }
  246. /**
  247. * Actually does the file (and possibly empty directory) copies.
  248. * This is a good method for subclasses to override.
  249. */
  250. protected void doFileOperations() {
  251. if (fileCopyMap.size() > 0) {
  252. log("Copying " + fileCopyMap.size() +
  253. " file" + (fileCopyMap.size() == 1 ? "" : "s") +
  254. " to " + destDir.getAbsolutePath() );
  255. Enumeration e = fileCopyMap.keys();
  256. while (e.hasMoreElements()) {
  257. String fromFile = (String) e.nextElement();
  258. String toFile = (String) fileCopyMap.get(fromFile);
  259. try {
  260. log("Copying " + fromFile + " to " + toFile, verbosity);
  261. project.copyFile(fromFile,
  262. toFile,
  263. filtering,
  264. forceOverwrite);
  265. } catch (IOException ioe) {
  266. String msg = "Failed to copy " + fromFile + " to " + toFile
  267. + " due to " + ioe.getMessage();
  268. throw new BuildException(msg, ioe, location);
  269. }
  270. }
  271. }
  272. if (includeEmpty) {
  273. Enumeration e = dirCopyMap.elements();
  274. int count = 0;
  275. while (e.hasMoreElements()) {
  276. File d = new File((String)e.nextElement());
  277. if (!d.exists()) {
  278. if (!d.mkdirs()) {
  279. log("Unable to create directory " + d.getAbsolutePath(), Project.MSG_ERR);
  280. } else {
  281. count++;
  282. }
  283. }
  284. }
  285. if (count > 0) {
  286. log("Copied " + count + " empty directories to " + destDir.getAbsolutePath());
  287. }
  288. }
  289. }
  290. }