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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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", "Ant", 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 org.apache.tools.ant.util.*;
  58. import java.io.*;
  59. import java.util.*;
  60. /**
  61. * A consolidated copy task. Copies a file or directory to a new file
  62. * or directory. Files are only copied if the source file is newer
  63. * than the destination file, or when the destination file does not
  64. * exist. It is possible to explicitly overwrite existing files.</p>
  65. *
  66. * <p>This implementation is based on Arnout Kuiper's initial design
  67. * document, the following mailing list discussions, and the
  68. * copyfile/copydir tasks.</p>
  69. *
  70. * @author Glenn McAllister <a href="mailto:glennm@ca.ibm.com">glennm@ca.ibm.com</a>
  71. * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  72. */
  73. public class Copy extends Task {
  74. protected File file = null; // the source file
  75. protected File destFile = null; // the destination file
  76. protected File destDir = null; // the destination directory
  77. protected Vector filesets = new Vector();
  78. protected boolean filtering = false;
  79. protected boolean preserveLastModified = false;
  80. protected boolean forceOverwrite = false;
  81. protected boolean flatten = false;
  82. protected int verbosity = Project.MSG_VERBOSE;
  83. protected boolean includeEmpty = true;
  84. protected Hashtable fileCopyMap = new Hashtable();
  85. protected Hashtable dirCopyMap = new Hashtable();
  86. protected Mapper mapperElement = null;
  87. /**
  88. * Sets a single source file to copy.
  89. */
  90. public void setFile(File file) {
  91. this.file = file;
  92. }
  93. /**
  94. * Sets the destination file.
  95. */
  96. public void setTofile(File destFile) {
  97. this.destFile = destFile;
  98. }
  99. /**
  100. * Sets the destination directory.
  101. */
  102. public void setTodir(File destDir) {
  103. this.destDir = destDir;
  104. }
  105. /**
  106. * Give the copied files the same last modified time as the original files.
  107. */
  108. public void setPreserveLastModified(String preserve) {
  109. preserveLastModified = Project.toBoolean(preserve);
  110. }
  111. /**
  112. * Sets filtering.
  113. */
  114. public void setFiltering(boolean filtering) {
  115. this.filtering = filtering;
  116. }
  117. /**
  118. * Overwrite any existing destination file(s).
  119. */
  120. public void setOverwrite(boolean overwrite) {
  121. this.forceOverwrite = overwrite;
  122. }
  123. /**
  124. * When copying directory trees, the files can be "flattened"
  125. * into a single directory. If there are multiple files with
  126. * the same name in the source directory tree, only the first
  127. * file will be copied into the "flattened" directory, unless
  128. * the forceoverwrite attribute is true.
  129. */
  130. public void setFlatten(boolean flatten) {
  131. this.flatten = flatten;
  132. }
  133. /**
  134. * Used to force listing of all names of copied files.
  135. */
  136. public void setVerbose(boolean verbose) {
  137. if (verbose) {
  138. this.verbosity = Project.MSG_INFO;
  139. } else {
  140. this.verbosity = Project.MSG_VERBOSE;
  141. }
  142. }
  143. /**
  144. * Used to copy empty directories.
  145. */
  146. public void setIncludeEmptyDirs(boolean includeEmpty) {
  147. this.includeEmpty = includeEmpty;
  148. }
  149. /**
  150. * Adds a set of files (nested fileset attribute).
  151. */
  152. public void addFileset(FileSet set) {
  153. filesets.addElement(set);
  154. }
  155. /**
  156. * Defines the FileNameMapper to use (nested mapper element).
  157. */
  158. public Mapper createMapper() throws BuildException {
  159. if (mapperElement != null) {
  160. throw new BuildException("Cannot define more than one mapper",
  161. location);
  162. }
  163. mapperElement = new Mapper(project);
  164. return mapperElement;
  165. }
  166. /**
  167. * Performs the copy operation.
  168. */
  169. public void execute() throws BuildException {
  170. // make sure we don't have an illegal set of options
  171. validateAttributes();
  172. // deal with the single file
  173. if (file != null) {
  174. if (file.exists()) {
  175. if (destFile == null) {
  176. destFile = new File(destDir, file.getName());
  177. }
  178. if (forceOverwrite ||
  179. (file.lastModified() > destFile.lastModified())) {
  180. fileCopyMap.put(file.getAbsolutePath(), destFile.getAbsolutePath());
  181. }
  182. } else {
  183. log("Could not find file " + file.getAbsolutePath() + " to copy.");
  184. }
  185. }
  186. // deal with the filesets
  187. for (int i=0; i<filesets.size(); i++) {
  188. FileSet fs = (FileSet) filesets.elementAt(i);
  189. DirectoryScanner ds = fs.getDirectoryScanner(project);
  190. File fromDir = fs.getDir(project);
  191. String[] srcFiles = ds.getIncludedFiles();
  192. String[] srcDirs = ds.getIncludedDirectories();
  193. scan(fromDir, destDir, srcFiles, srcDirs);
  194. }
  195. // do all the copy operations now...
  196. doFileOperations();
  197. // clean up destDir again - so this instance can be used a second
  198. // time without throwing an exception
  199. if (destFile != null) {
  200. destDir = null;
  201. }
  202. }
  203. //************************************************************************
  204. // protected and private methods
  205. //************************************************************************
  206. /**
  207. * Ensure we have a consistent and legal set of attributes, and set
  208. * any internal flags necessary based on different combinations
  209. * of attributes.
  210. */
  211. protected void validateAttributes() throws BuildException {
  212. if (file == null && filesets.size() == 0) {
  213. throw new BuildException("Specify at least one source - a file or a fileset.");
  214. }
  215. if (destFile != null && destDir != null) {
  216. throw new BuildException("Only one of destfile and destdir may be set.");
  217. }
  218. if (destFile == null && destDir == null) {
  219. throw new BuildException("One of destfile or destdir must be set.");
  220. }
  221. if (file != null && file.exists() && file.isDirectory()) {
  222. throw new BuildException("Use a fileset to copy directories.");
  223. }
  224. if (destFile != null && filesets.size() > 0) {
  225. throw new BuildException("Cannot concatenate multple files into a single file.");
  226. }
  227. if (destFile != null) {
  228. destDir = new File(destFile.getParent()); // be 1.1 friendly
  229. }
  230. }
  231. /**
  232. * Compares source files to destination files to see if they should be
  233. * copied.
  234. */
  235. protected void scan(File fromDir, File toDir, String[] files, String[] dirs) {
  236. FileNameMapper mapper = null;
  237. if (mapperElement != null) {
  238. mapper = mapperElement.getImplementation();
  239. } else if (flatten) {
  240. mapper = new FlatFileNameMapper();
  241. } else {
  242. mapper = new IdentityMapper();
  243. }
  244. buildMap(fromDir, toDir, files, mapper, fileCopyMap);
  245. if (includeEmpty) {
  246. buildMap(fromDir, toDir, dirs, mapper, dirCopyMap);
  247. }
  248. }
  249. protected void buildMap(File fromDir, File toDir, String[] names,
  250. FileNameMapper mapper, Hashtable map) {
  251. String[] toCopy = null;
  252. if (forceOverwrite) {
  253. Vector v = new Vector();
  254. for (int i=0; i<names.length; i++) {
  255. if (mapper.mapFileName(names[i]) != null) {
  256. v.addElement(names[i]);
  257. }
  258. }
  259. toCopy = new String[v.size()];
  260. v.copyInto(toCopy);
  261. } else {
  262. SourceFileScanner ds = new SourceFileScanner(this);
  263. toCopy = ds.restrict(names, fromDir, toDir, mapper);
  264. }
  265. for (int i = 0; i < toCopy.length; i++) {
  266. File src = new File(fromDir, toCopy[i]);
  267. File dest = new File(toDir, mapper.mapFileName(toCopy[i])[0]);
  268. map.put( src.getAbsolutePath(), dest.getAbsolutePath() );
  269. }
  270. }
  271. /**
  272. * Actually does the file (and possibly empty directory) copies.
  273. * This is a good method for subclasses to override.
  274. */
  275. protected void doFileOperations() {
  276. if (fileCopyMap.size() > 0) {
  277. log("Copying " + fileCopyMap.size() +
  278. " file" + (fileCopyMap.size() == 1 ? "" : "s") +
  279. " to " + destDir.getAbsolutePath() );
  280. Enumeration e = fileCopyMap.keys();
  281. while (e.hasMoreElements()) {
  282. String fromFile = (String) e.nextElement();
  283. String toFile = (String) fileCopyMap.get(fromFile);
  284. if( fromFile.equals( toFile ) ) {
  285. log("Skipping self-copy of " + fromFile, verbosity);
  286. continue;
  287. }
  288. try {
  289. log("Copying " + fromFile + " to " + toFile, verbosity);
  290. project.copyFile(fromFile,
  291. toFile,
  292. filtering,
  293. forceOverwrite,
  294. preserveLastModified);
  295. } catch (IOException ioe) {
  296. String msg = "Failed to copy " + fromFile + " to " + toFile
  297. + " due to " + ioe.getMessage();
  298. throw new BuildException(msg, ioe, location);
  299. }
  300. }
  301. }
  302. if (includeEmpty) {
  303. Enumeration e = dirCopyMap.elements();
  304. int count = 0;
  305. while (e.hasMoreElements()) {
  306. File d = new File((String)e.nextElement());
  307. if (!d.exists()) {
  308. if (!d.mkdirs()) {
  309. log("Unable to create directory " + d.getAbsolutePath(), Project.MSG_ERR);
  310. } else {
  311. count++;
  312. }
  313. }
  314. }
  315. if (count > 0) {
  316. log("Copied " + count +
  317. " empty director" +
  318. (count==1?"y":"ies") +
  319. " to " + destDir.getAbsolutePath());
  320. }
  321. }
  322. }
  323. }