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

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.HashSet;
  24. import java.util.Hashtable;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Vector;
  28. import org.apache.tools.ant.BuildException;
  29. import org.apache.tools.ant.DirectoryScanner;
  30. import org.apache.tools.ant.Project;
  31. import org.apache.tools.ant.Task;
  32. import org.apache.tools.ant.types.FileSet;
  33. import org.apache.tools.ant.types.FilterChain;
  34. import org.apache.tools.ant.types.FilterSet;
  35. import org.apache.tools.ant.types.FilterSetCollection;
  36. import org.apache.tools.ant.types.Mapper;
  37. import org.apache.tools.ant.types.Resource;
  38. import org.apache.tools.ant.types.ResourceCollection;
  39. import org.apache.tools.ant.types.ResourceFactory;
  40. import org.apache.tools.ant.types.resources.FileProvider;
  41. import org.apache.tools.ant.types.resources.FileResource;
  42. import org.apache.tools.ant.util.FileNameMapper;
  43. import org.apache.tools.ant.util.FileUtils;
  44. import org.apache.tools.ant.util.FlatFileNameMapper;
  45. import org.apache.tools.ant.util.IdentityMapper;
  46. import org.apache.tools.ant.util.LinkedHashtable;
  47. import org.apache.tools.ant.util.ResourceUtils;
  48. import org.apache.tools.ant.util.SourceFileScanner;
  49. /**
  50. * <p>Copies a file or directory to a new file
  51. * or directory. Files are only copied if the source file is newer
  52. * than the destination file, or when the destination file does not
  53. * exist. It is possible to explicitly overwrite existing files.</p>
  54. *
  55. * <p>This implementation is based on Arnout Kuiper's initial design
  56. * document, the following mailing list discussions, and the
  57. * copyfile/copydir tasks.</p>
  58. *
  59. *
  60. * @since Ant 1.2
  61. *
  62. * @ant.task category="filesystem"
  63. */
  64. public class Copy extends Task {
  65. private static final String MSG_WHEN_COPYING_EMPTY_RC_TO_FILE =
  66. "Cannot perform operation from directory to file.";
  67. static final File NULL_FILE_PLACEHOLDER = new File("/NULL_FILE");
  68. static final String LINE_SEPARATOR = System.getProperty("line.separator");
  69. // CheckStyle:VisibilityModifier OFF - bc
  70. protected File file = null; // the source file
  71. protected File destFile = null; // the destination file
  72. protected File destDir = null; // the destination directory
  73. protected Vector<ResourceCollection> rcs = new Vector<ResourceCollection>();
  74. // here to provide API backwards compatibility
  75. protected Vector<ResourceCollection> filesets = rcs;
  76. private boolean enableMultipleMappings = false;
  77. protected boolean filtering = false;
  78. protected boolean preserveLastModified = false;
  79. protected boolean forceOverwrite = false;
  80. protected boolean flatten = false;
  81. protected int verbosity = Project.MSG_VERBOSE;
  82. protected boolean includeEmpty = true;
  83. protected boolean failonerror = true;
  84. protected Hashtable<String, String[]> fileCopyMap = new LinkedHashtable<String, String[]>();
  85. protected Hashtable<String, String[]> dirCopyMap = new LinkedHashtable<String, String[]>();
  86. protected Hashtable<File, File> completeDirMap = new LinkedHashtable<File, File>();
  87. protected Mapper mapperElement = null;
  88. protected FileUtils fileUtils;
  89. //CheckStyle:VisibilityModifier ON
  90. private final Vector<FilterChain> filterChains = new Vector<FilterChain>();
  91. private final Vector<FilterSet> filterSets = new Vector<FilterSet>();
  92. private String inputEncoding = null;
  93. private String outputEncoding = null;
  94. private long granularity = 0;
  95. private boolean force = false;
  96. private boolean quiet = false;
  97. // used to store the single non-file resource to copy when the
  98. // tofile attribute has been used
  99. private Resource singleResource = null;
  100. /**
  101. * Copy task constructor.
  102. */
  103. public Copy() {
  104. fileUtils = FileUtils.getFileUtils();
  105. granularity = fileUtils.getFileTimestampGranularity();
  106. }
  107. /**
  108. * Get the FileUtils for this task.
  109. * @return the fileutils object.
  110. */
  111. protected FileUtils getFileUtils() {
  112. return fileUtils;
  113. }
  114. /**
  115. * Set a single source file to copy.
  116. * @param file the file to copy.
  117. */
  118. public void setFile(final File file) {
  119. this.file = file;
  120. }
  121. /**
  122. * Set the destination file.
  123. * @param destFile the file to copy to.
  124. */
  125. public void setTofile(final File destFile) {
  126. this.destFile = destFile;
  127. }
  128. /**
  129. * Set the destination directory.
  130. * @param destDir the destination directory.
  131. */
  132. public void setTodir(final File destDir) {
  133. this.destDir = destDir;
  134. }
  135. /**
  136. * Add a FilterChain.
  137. * @return a filter chain object.
  138. */
  139. public FilterChain createFilterChain() {
  140. final FilterChain filterChain = new FilterChain();
  141. filterChains.addElement(filterChain);
  142. return filterChain;
  143. }
  144. /**
  145. * Add a filterset.
  146. * @return a filter set object.
  147. */
  148. public FilterSet createFilterSet() {
  149. final FilterSet filterSet = new FilterSet();
  150. filterSets.addElement(filterSet);
  151. return filterSet;
  152. }
  153. /**
  154. * Give the copied files the same last modified time as the original files.
  155. * @param preserve a boolean string.
  156. * @deprecated since 1.5.x.
  157. * setPreserveLastModified(String) has been deprecated and
  158. * replaced with setPreserveLastModified(boolean) to
  159. * consistently let the Introspection mechanism work.
  160. */
  161. @Deprecated
  162. public void setPreserveLastModified(final String preserve) {
  163. setPreserveLastModified(Project.toBoolean(preserve));
  164. }
  165. /**
  166. * Give the copied files the same last modified time as the original files.
  167. * @param preserve if true preserve the modified time; default is false.
  168. */
  169. public void setPreserveLastModified(final boolean preserve) {
  170. preserveLastModified = preserve;
  171. }
  172. /**
  173. * Get whether to give the copied files the same last modified time as
  174. * the original files.
  175. * @return the whether destination files will inherit the modification
  176. * times of the corresponding source files.
  177. * @since 1.32, Ant 1.5
  178. */
  179. public boolean getPreserveLastModified() {
  180. return preserveLastModified;
  181. }
  182. /**
  183. * Get the filtersets being applied to this operation.
  184. *
  185. * @return a vector of FilterSet objects.
  186. */
  187. protected Vector<FilterSet> getFilterSets() {
  188. return filterSets;
  189. }
  190. /**
  191. * Get the filterchains being applied to this operation.
  192. *
  193. * @return a vector of FilterChain objects.
  194. */
  195. protected Vector<FilterChain> getFilterChains() {
  196. return filterChains;
  197. }
  198. /**
  199. * Set filtering mode.
  200. * @param filtering if true enable filtering; default is false.
  201. */
  202. public void setFiltering(final boolean filtering) {
  203. this.filtering = filtering;
  204. }
  205. /**
  206. * Set overwrite mode regarding existing destination file(s).
  207. * @param overwrite if true force overwriting of destination file(s)
  208. * even if the destination file(s) are younger than
  209. * the corresponding source file. Default is false.
  210. */
  211. public void setOverwrite(final boolean overwrite) {
  212. this.forceOverwrite = overwrite;
  213. }
  214. /**
  215. * Whether read-only destinations will be overwritten.
  216. *
  217. * <p>Defaults to false</p>
  218. *
  219. * @param f boolean
  220. * @since Ant 1.8.2
  221. */
  222. public void setForce(final boolean f) {
  223. force = f;
  224. }
  225. /**
  226. * Whether read-only destinations will be overwritten.
  227. *
  228. * @return boolean
  229. * @since Ant 1.8.2
  230. */
  231. public boolean getForce() {
  232. return force;
  233. }
  234. /**
  235. * Set whether files copied from directory trees will be "flattened"
  236. * into a single directory. If there are multiple files with
  237. * the same name in the source directory tree, only the first
  238. * file will be copied into the "flattened" directory, unless
  239. * the forceoverwrite attribute is true.
  240. * @param flatten if true flatten the destination directory. Default
  241. * is false.
  242. */
  243. public void setFlatten(final boolean flatten) {
  244. this.flatten = flatten;
  245. }
  246. /**
  247. * Set verbose mode. Used to force listing of all names of copied files.
  248. * @param verbose whether to output the names of copied files.
  249. * Default is false.
  250. */
  251. public void setVerbose(final boolean verbose) {
  252. this.verbosity = verbose ? Project.MSG_INFO : Project.MSG_VERBOSE;
  253. }
  254. /**
  255. * Set whether to copy empty directories.
  256. * @param includeEmpty if true copy empty directories. Default is true.
  257. */
  258. public void setIncludeEmptyDirs(final boolean includeEmpty) {
  259. this.includeEmpty = includeEmpty;
  260. }
  261. /**
  262. * Set quiet mode. Used to hide messages when a file or directory to be
  263. * copied does not exist.
  264. *
  265. * @param quiet
  266. * whether or not to display error messages when a file or
  267. * directory does not exist. Default is false.
  268. */
  269. public void setQuiet(final boolean quiet) {
  270. this.quiet = quiet;
  271. }
  272. /**
  273. * Set method of handling mappers that return multiple
  274. * mappings for a given source path.
  275. * @param enableMultipleMappings If true the task will
  276. * copy to all the mappings for a given source path, if
  277. * false, only the first file or directory is
  278. * processed.
  279. * By default, this setting is false to provide backward
  280. * compatibility with earlier releases.
  281. * @since Ant 1.6
  282. */
  283. public void setEnableMultipleMappings(final boolean enableMultipleMappings) {
  284. this.enableMultipleMappings = enableMultipleMappings;
  285. }
  286. /**
  287. * Get whether multiple mapping is enabled.
  288. * @return true if multiple mapping is enabled; false otherwise.
  289. */
  290. public boolean isEnableMultipleMapping() {
  291. return enableMultipleMappings;
  292. }
  293. /**
  294. * Set whether to fail when errors are encountered. If false, note errors
  295. * to the output but keep going. Default is true.
  296. * @param failonerror true or false.
  297. */
  298. public void setFailOnError(final boolean failonerror) {
  299. this.failonerror = failonerror;
  300. }
  301. /**
  302. * Add a set of files to copy.
  303. * @param set a set of files to copy.
  304. */
  305. public void addFileset(final FileSet set) {
  306. add(set);
  307. }
  308. /**
  309. * Add a collection of files to copy.
  310. * @param res a resource collection to copy.
  311. * @since Ant 1.7
  312. */
  313. public void add(final ResourceCollection res) {
  314. rcs.add(res);
  315. }
  316. /**
  317. * Define the mapper to map source to destination files.
  318. * @return a mapper to be configured.
  319. * @exception BuildException if more than one mapper is defined.
  320. */
  321. public Mapper createMapper() throws BuildException {
  322. if (mapperElement != null) {
  323. throw new BuildException("Cannot define more than one mapper",
  324. getLocation());
  325. }
  326. mapperElement = new Mapper(getProject());
  327. return mapperElement;
  328. }
  329. /**
  330. * Add a nested filenamemapper.
  331. * @param fileNameMapper the mapper to add.
  332. * @since Ant 1.6.3
  333. */
  334. public void add(final FileNameMapper fileNameMapper) {
  335. createMapper().add(fileNameMapper);
  336. }
  337. /**
  338. * Set the character encoding.
  339. * @param encoding the character encoding.
  340. * @since 1.32, Ant 1.5
  341. */
  342. public void setEncoding(final String encoding) {
  343. this.inputEncoding = encoding;
  344. if (outputEncoding == null) {
  345. outputEncoding = encoding;
  346. }
  347. }
  348. /**
  349. * Get the character encoding to be used.
  350. * @return the character encoding, <code>null</code> if not set.
  351. *
  352. * @since 1.32, Ant 1.5
  353. */
  354. public String getEncoding() {
  355. return inputEncoding;
  356. }
  357. /**
  358. * Set the character encoding for output files.
  359. * @param encoding the output character encoding.
  360. * @since Ant 1.6
  361. */
  362. public void setOutputEncoding(final String encoding) {
  363. this.outputEncoding = encoding;
  364. }
  365. /**
  366. * Get the character encoding for output files.
  367. * @return the character encoding for output files,
  368. * <code>null</code> if not set.
  369. *
  370. * @since Ant 1.6
  371. */
  372. public String getOutputEncoding() {
  373. return outputEncoding;
  374. }
  375. /**
  376. * Set the number of milliseconds leeway to give before deciding a
  377. * target is out of date.
  378. *
  379. * <p>Default is 1 second, or 2 seconds on DOS systems.</p>
  380. * @param granularity the granularity used to decide if a target is out of
  381. * date.
  382. * @since Ant 1.6.2
  383. */
  384. public void setGranularity(final long granularity) {
  385. this.granularity = granularity;
  386. }
  387. /**
  388. * Perform the copy operation.
  389. * @exception BuildException if an error occurs.
  390. */
  391. @Override
  392. public void execute() throws BuildException {
  393. final File savedFile = file; // may be altered in validateAttributes
  394. final File savedDestFile = destFile;
  395. final File savedDestDir = destDir;
  396. ResourceCollection savedRc = null;
  397. if (file == null && destFile != null && rcs.size() == 1) {
  398. // will be removed in validateAttributes
  399. savedRc = rcs.elementAt(0);
  400. }
  401. try {
  402. // make sure we don't have an illegal set of options
  403. try {
  404. validateAttributes();
  405. } catch (final BuildException e) {
  406. if (failonerror
  407. || !getMessage(e)
  408. .equals(MSG_WHEN_COPYING_EMPTY_RC_TO_FILE)) {
  409. throw e;
  410. } else {
  411. log("Warning: " + getMessage(e), Project.MSG_ERR);
  412. return;
  413. }
  414. }
  415. // deal with the single file
  416. copySingleFile();
  417. // deal with the ResourceCollections
  418. /* for historical and performance reasons we have to do
  419. things in a rather complex way.
  420. (1) Move is optimized to move directories if a fileset
  421. has been included completely, therefore FileSets need a
  422. special treatment. This is also required to support
  423. the failOnError semantic (skip filesets with broken
  424. basedir but handle the remaining collections).
  425. (2) We carry around a few protected methods that work
  426. on basedirs and arrays of names. To optimize stuff, all
  427. resources with the same basedir get collected in
  428. separate lists and then each list is handled in one go.
  429. */
  430. final HashMap<File, List<String>> filesByBasedir = new HashMap<File, List<String>>();
  431. final HashMap<File, List<String>> dirsByBasedir = new HashMap<File, List<String>>();
  432. final HashSet<File> baseDirs = new HashSet<File>();
  433. final ArrayList<Resource> nonFileResources = new ArrayList<Resource>();
  434. final int size = rcs.size();
  435. for (int i = 0; i < size; i++) {
  436. final ResourceCollection rc = rcs.elementAt(i);
  437. // Step (1) - beware of the ZipFileSet
  438. if (rc instanceof FileSet && rc.isFilesystemOnly()) {
  439. final FileSet fs = (FileSet) rc;
  440. DirectoryScanner ds = null;
  441. try {
  442. ds = fs.getDirectoryScanner(getProject());
  443. } catch (final BuildException e) {
  444. if (failonerror
  445. || !getMessage(e).endsWith(DirectoryScanner
  446. .DOES_NOT_EXIST_POSTFIX)) {
  447. throw e;
  448. } else {
  449. if (!quiet) {
  450. log("Warning: " + getMessage(e), Project.MSG_ERR);
  451. }
  452. continue;
  453. }
  454. }
  455. final File fromDir = fs.getDir(getProject());
  456. final String[] srcFiles = ds.getIncludedFiles();
  457. final String[] srcDirs = ds.getIncludedDirectories();
  458. if (!flatten && mapperElement == null
  459. && ds.isEverythingIncluded() && !fs.hasPatterns()) {
  460. completeDirMap.put(fromDir, destDir);
  461. }
  462. add(fromDir, srcFiles, filesByBasedir);
  463. add(fromDir, srcDirs, dirsByBasedir);
  464. baseDirs.add(fromDir);
  465. } else { // not a fileset or contains non-file resources
  466. if (!rc.isFilesystemOnly() && !supportsNonFileResources()) {
  467. throw new BuildException(
  468. "Only FileSystem resources are supported.");
  469. }
  470. for (final Resource r : rc) {
  471. if (!r.isExists()) {
  472. final String message = "Warning: Could not find resource "
  473. + r.toLongString() + " to copy.";
  474. if (!failonerror) {
  475. if (!quiet) {
  476. log(message, Project.MSG_ERR);
  477. }
  478. } else {
  479. throw new BuildException(message);
  480. }
  481. continue;
  482. }
  483. File baseDir = NULL_FILE_PLACEHOLDER;
  484. String name = r.getName();
  485. final FileProvider fp = r.as(FileProvider.class);
  486. if (fp != null) {
  487. final FileResource fr = ResourceUtils.asFileResource(fp);
  488. baseDir = getKeyFile(fr.getBaseDir());
  489. if (fr.getBaseDir() == null) {
  490. name = fr.getFile().getAbsolutePath();
  491. }
  492. }
  493. // copying of dirs is trivial and can be done
  494. // for non-file resources as well as for real
  495. // files.
  496. if (r.isDirectory() || fp != null) {
  497. add(baseDir, name,
  498. r.isDirectory() ? dirsByBasedir
  499. : filesByBasedir);
  500. baseDirs.add(baseDir);
  501. } else { // a not-directory file resource
  502. // needs special treatment
  503. nonFileResources.add(r);
  504. }
  505. }
  506. }
  507. }
  508. iterateOverBaseDirs(baseDirs, dirsByBasedir, filesByBasedir);
  509. // do all the copy operations now...
  510. try {
  511. doFileOperations();
  512. } catch (final BuildException e) {
  513. if (!failonerror) {
  514. if (!quiet) {
  515. log("Warning: " + getMessage(e), Project.MSG_ERR);
  516. }
  517. } else {
  518. throw e;
  519. }
  520. }
  521. if (nonFileResources.size() > 0 || singleResource != null) {
  522. final Resource[] nonFiles =
  523. nonFileResources.toArray(new Resource[nonFileResources.size()]);
  524. // restrict to out-of-date resources
  525. final Map<Resource, String[]> map = scan(nonFiles, destDir);
  526. if (singleResource != null) {
  527. map.put(singleResource,
  528. new String[] {destFile.getAbsolutePath()});
  529. }
  530. try {
  531. doResourceOperations(map);
  532. } catch (final BuildException e) {
  533. if (!failonerror) {
  534. if (!quiet) {
  535. log("Warning: " + getMessage(e), Project.MSG_ERR);
  536. }
  537. } else {
  538. throw e;
  539. }
  540. }
  541. }
  542. } finally {
  543. // clean up again, so this instance can be used a second
  544. // time
  545. singleResource = null;
  546. file = savedFile;
  547. destFile = savedDestFile;
  548. destDir = savedDestDir;
  549. if (savedRc != null) {
  550. rcs.insertElementAt(savedRc, 0);
  551. }
  552. fileCopyMap.clear();
  553. dirCopyMap.clear();
  554. completeDirMap.clear();
  555. }
  556. }
  557. /************************************************************************
  558. ** protected and private methods
  559. ************************************************************************/
  560. private void copySingleFile() {
  561. // deal with the single file
  562. if (file != null) {
  563. if (file.exists()) {
  564. if (destFile == null) {
  565. destFile = new File(destDir, file.getName());
  566. }
  567. if (forceOverwrite || !destFile.exists()
  568. || (file.lastModified() - granularity
  569. > destFile.lastModified())) {
  570. fileCopyMap.put(file.getAbsolutePath(),
  571. new String[] {destFile.getAbsolutePath()});
  572. } else {
  573. log(file + " omitted as " + destFile
  574. + " is up to date.", Project.MSG_VERBOSE);
  575. }
  576. } else {
  577. final String message = "Warning: Could not find file "
  578. + file.getAbsolutePath() + " to copy.";
  579. if (!failonerror) {
  580. if (!quiet) {
  581. log(message, Project.MSG_ERR);
  582. }
  583. } else {
  584. throw new BuildException(message);
  585. }
  586. }
  587. }
  588. }
  589. private void iterateOverBaseDirs(
  590. final HashSet<File> baseDirs, final HashMap<File, List<String>> dirsByBasedir, final HashMap<File, List<String>> filesByBasedir) {
  591. for (final File f : baseDirs) {
  592. final List<String> files = filesByBasedir.get(f);
  593. final List<String> dirs = dirsByBasedir.get(f);
  594. String[] srcFiles = new String[0];
  595. if (files != null) {
  596. srcFiles = files.toArray(srcFiles);
  597. }
  598. String[] srcDirs = new String[0];
  599. if (dirs != null) {
  600. srcDirs = dirs.toArray(srcDirs);
  601. }
  602. scan(f == NULL_FILE_PLACEHOLDER ? null : f, destDir, srcFiles,
  603. srcDirs);
  604. }
  605. }
  606. /**
  607. * Ensure we have a consistent and legal set of attributes, and set
  608. * any internal flags necessary based on different combinations
  609. * of attributes.
  610. * @exception BuildException if an error occurs.
  611. */
  612. protected void validateAttributes() throws BuildException {
  613. if (file == null && rcs.size() == 0) {
  614. throw new BuildException(
  615. "Specify at least one source--a file or a resource collection.");
  616. }
  617. if (destFile != null && destDir != null) {
  618. throw new BuildException(
  619. "Only one of tofile and todir may be set.");
  620. }
  621. if (destFile == null && destDir == null) {
  622. throw new BuildException("One of tofile or todir must be set.");
  623. }
  624. if (file != null && file.isDirectory()) {
  625. throw new BuildException("Use a resource collection to copy directories.");
  626. }
  627. if (destFile != null && rcs.size() > 0) {
  628. if (rcs.size() > 1) {
  629. throw new BuildException(
  630. "Cannot concatenate multiple files into a single file.");
  631. } else {
  632. final ResourceCollection rc = rcs.elementAt(0);
  633. if (!rc.isFilesystemOnly() && !supportsNonFileResources()) {
  634. throw new BuildException("Only FileSystem resources are"
  635. + " supported.");
  636. }
  637. if (rc.size() == 0) {
  638. throw new BuildException(MSG_WHEN_COPYING_EMPTY_RC_TO_FILE);
  639. } else if (rc.size() == 1) {
  640. final Resource res = rc.iterator().next();
  641. final FileProvider r = res.as(FileProvider.class);
  642. if (file == null) {
  643. if (r != null) {
  644. file = r.getFile();
  645. } else {
  646. singleResource = res;
  647. }
  648. rcs.removeElementAt(0);
  649. } else {
  650. throw new BuildException(
  651. "Cannot concatenate multiple files into a single file.");
  652. }
  653. } else {
  654. throw new BuildException(
  655. "Cannot concatenate multiple files into a single file.");
  656. }
  657. }
  658. }
  659. if (destFile != null) {
  660. destDir = destFile.getParentFile();
  661. }
  662. }
  663. /**
  664. * Compares source files to destination files to see if they should be
  665. * copied.
  666. *
  667. * @param fromDir The source directory.
  668. * @param toDir The destination directory.
  669. * @param files A list of files to copy.
  670. * @param dirs A list of directories to copy.
  671. */
  672. protected void scan(final File fromDir, final File toDir, final String[] files,
  673. final String[] dirs) {
  674. final FileNameMapper mapper = getMapper();
  675. buildMap(fromDir, toDir, files, mapper, fileCopyMap);
  676. if (includeEmpty) {
  677. buildMap(fromDir, toDir, dirs, mapper, dirCopyMap);
  678. }
  679. }
  680. /**
  681. * Compares source resources to destination files to see if they
  682. * should be copied.
  683. *
  684. * @param fromResources The source resources.
  685. * @param toDir The destination directory.
  686. *
  687. * @return a Map with the out-of-date resources as keys and an
  688. * array of target file names as values.
  689. *
  690. * @since Ant 1.7
  691. */
  692. protected Map<Resource, String[]> scan(final Resource[] fromResources, final File toDir) {
  693. return buildMap(fromResources, toDir, getMapper());
  694. }
  695. /**
  696. * Add to a map of files/directories to copy.
  697. *
  698. * @param fromDir the source directory.
  699. * @param toDir the destination directory.
  700. * @param names a list of filenames.
  701. * @param mapper a <code>FileNameMapper</code> value.
  702. * @param map a map of source file to array of destination files.
  703. */
  704. protected void buildMap(final File fromDir, final File toDir, final String[] names,
  705. final FileNameMapper mapper, final Hashtable<String, String[]> map) {
  706. String[] toCopy = null;
  707. if (forceOverwrite) {
  708. final Vector<String> v = new Vector<String>();
  709. for (int i = 0; i < names.length; i++) {
  710. if (mapper.mapFileName(names[i]) != null) {
  711. v.addElement(names[i]);
  712. }
  713. }
  714. toCopy = new String[v.size()];
  715. v.copyInto(toCopy);
  716. } else {
  717. final SourceFileScanner ds = new SourceFileScanner(this);
  718. toCopy = ds.restrict(names, fromDir, toDir, mapper, granularity);
  719. }
  720. for (int i = 0; i < toCopy.length; i++) {
  721. final File src = new File(fromDir, toCopy[i]);
  722. final String[] mappedFiles = mapper.mapFileName(toCopy[i]);
  723. if (mappedFiles == null || mappedFiles.length == 0) {
  724. continue;
  725. }
  726. if (!enableMultipleMappings) {
  727. map.put(src.getAbsolutePath(),
  728. new String[] {new File(toDir, mappedFiles[0]).getAbsolutePath()});
  729. } else {
  730. // reuse the array created by the mapper
  731. for (int k = 0; k < mappedFiles.length; k++) {
  732. mappedFiles[k] = new File(toDir, mappedFiles[k]).getAbsolutePath();
  733. }
  734. map.put(src.getAbsolutePath(), mappedFiles);
  735. }
  736. }
  737. }
  738. /**
  739. * Create a map of resources to copy.
  740. *
  741. * @param fromResources The source resources.
  742. * @param toDir the destination directory.
  743. * @param mapper a <code>FileNameMapper</code> value.
  744. * @return a map of source resource to array of destination files.
  745. * @since Ant 1.7
  746. */
  747. protected Map<Resource, String[]> buildMap(final Resource[] fromResources, final File toDir,
  748. final FileNameMapper mapper) {
  749. final HashMap<Resource, String[]> map = new HashMap<Resource, String[]>();
  750. Resource[] toCopy = null;
  751. if (forceOverwrite) {
  752. final Vector<Resource> v = new Vector<Resource>();
  753. for (int i = 0; i < fromResources.length; i++) {
  754. if (mapper.mapFileName(fromResources[i].getName()) != null) {
  755. v.addElement(fromResources[i]);
  756. }
  757. }
  758. toCopy = new Resource[v.size()];
  759. v.copyInto(toCopy);
  760. } else {
  761. toCopy = ResourceUtils.selectOutOfDateSources(this, fromResources,
  762. mapper,
  763. new ResourceFactory() {
  764. public Resource getResource(final String name) {
  765. return new FileResource(toDir, name);
  766. }
  767. },
  768. granularity);
  769. }
  770. for (int i = 0; i < toCopy.length; i++) {
  771. final String[] mappedFiles = mapper.mapFileName(toCopy[i].getName());
  772. if (mappedFiles == null || mappedFiles.length == 0) {
  773. throw new BuildException("Can't copy a resource without a"
  774. + " name if the mapper doesn't"
  775. + " provide one.");
  776. }
  777. if (!enableMultipleMappings) {
  778. map.put(toCopy[i],
  779. new String[] {new File(toDir, mappedFiles[0]).getAbsolutePath()});
  780. } else {
  781. // reuse the array created by the mapper
  782. for (int k = 0; k < mappedFiles.length; k++) {
  783. mappedFiles[k] = new File(toDir, mappedFiles[k]).getAbsolutePath();
  784. }
  785. map.put(toCopy[i], mappedFiles);
  786. }
  787. }
  788. return map;
  789. }
  790. /**
  791. * Actually does the file (and possibly empty directory) copies.
  792. * This is a good method for subclasses to override.
  793. */
  794. protected void doFileOperations() {
  795. if (fileCopyMap.size() > 0) {
  796. log("Copying " + fileCopyMap.size()
  797. + " file" + (fileCopyMap.size() == 1 ? "" : "s")
  798. + " to " + destDir.getAbsolutePath());
  799. for (final Map.Entry<String, String[]> e : fileCopyMap.entrySet()) {
  800. final String fromFile = e.getKey();
  801. final String[] toFiles = e.getValue();
  802. for (int i = 0; i < toFiles.length; i++) {
  803. final String toFile = toFiles[i];
  804. if (fromFile.equals(toFile)) {
  805. log("Skipping self-copy of " + fromFile, verbosity);
  806. continue;
  807. }
  808. try {
  809. log("Copying " + fromFile + " to " + toFile, verbosity);
  810. final FilterSetCollection executionFilters =
  811. new FilterSetCollection();
  812. if (filtering) {
  813. executionFilters
  814. .addFilterSet(getProject().getGlobalFilterSet());
  815. }
  816. for (final FilterSet filterSet : filterSets) {
  817. executionFilters.addFilterSet(filterSet);
  818. }
  819. fileUtils.copyFile(new File(fromFile), new File(toFile),
  820. executionFilters,
  821. filterChains, forceOverwrite,
  822. preserveLastModified,
  823. /* append: */ false, inputEncoding,
  824. outputEncoding, getProject(),
  825. getForce());
  826. } catch (final IOException ioe) {
  827. String msg = "Failed to copy " + fromFile + " to " + toFile
  828. + " due to " + getDueTo(ioe);
  829. final File targetFile = new File(toFile);
  830. if (!(ioe instanceof
  831. ResourceUtils.ReadOnlyTargetFileException)
  832. && targetFile.exists() && !targetFile.delete()) {
  833. msg += " and I couldn't delete the corrupt " + toFile;
  834. }
  835. if (failonerror) {
  836. throw new BuildException(msg, ioe, getLocation());
  837. }
  838. log(msg, Project.MSG_ERR);
  839. }
  840. }
  841. }
  842. }
  843. if (includeEmpty) {
  844. int createCount = 0;
  845. for (final String[] dirs : dirCopyMap.values()) {
  846. for (int i = 0; i < dirs.length; i++) {
  847. final File d = new File(dirs[i]);
  848. if (!d.exists()) {
  849. if (!(d.mkdirs() || d.isDirectory())) {
  850. log("Unable to create directory "
  851. + d.getAbsolutePath(), Project.MSG_ERR);
  852. } else {
  853. createCount++;
  854. }
  855. }
  856. }
  857. }
  858. if (createCount > 0) {
  859. log("Copied " + dirCopyMap.size()
  860. + " empty director"
  861. + (dirCopyMap.size() == 1 ? "y" : "ies")
  862. + " to " + createCount
  863. + " empty director"
  864. + (createCount == 1 ? "y" : "ies") + " under "
  865. + destDir.getAbsolutePath());
  866. }
  867. }
  868. }
  869. /**
  870. * Actually does the resource copies.
  871. * This is a good method for subclasses to override.
  872. * @param map a map of source resource to array of destination files.
  873. * @since Ant 1.7
  874. */
  875. protected void doResourceOperations(final Map<Resource, String[]> map) {
  876. if (map.size() > 0) {
  877. log("Copying " + map.size()
  878. + " resource" + (map.size() == 1 ? "" : "s")
  879. + " to " + destDir.getAbsolutePath());
  880. for (final Map.Entry<Resource, String[]> e : map.entrySet()) {
  881. final Resource fromResource = e.getKey();
  882. for (final String toFile : e.getValue()) {
  883. try {
  884. log("Copying " + fromResource + " to " + toFile,
  885. verbosity);
  886. final FilterSetCollection executionFilters = new FilterSetCollection();
  887. if (filtering) {
  888. executionFilters
  889. .addFilterSet(getProject().getGlobalFilterSet());
  890. }
  891. for (final FilterSet filterSet : filterSets) {
  892. executionFilters.addFilterSet(filterSet);
  893. }
  894. ResourceUtils.copyResource(fromResource,
  895. new FileResource(destDir,
  896. toFile),
  897. executionFilters,
  898. filterChains,
  899. forceOverwrite,
  900. preserveLastModified,
  901. /* append: */ false,
  902. inputEncoding,
  903. outputEncoding,
  904. getProject(),
  905. getForce());
  906. } catch (final IOException ioe) {
  907. String msg = "Failed to copy " + fromResource
  908. + " to " + toFile
  909. + " due to " + getDueTo(ioe);
  910. final File targetFile = new File(toFile);
  911. if (!(ioe instanceof
  912. ResourceUtils.ReadOnlyTargetFileException)
  913. && targetFile.exists() && !targetFile.delete()) {
  914. msg += " and I couldn't delete the corrupt " + toFile;
  915. }
  916. if (failonerror) {
  917. throw new BuildException(msg, ioe, getLocation());
  918. }
  919. log(msg, Project.MSG_ERR);
  920. }
  921. }
  922. }
  923. }
  924. }
  925. /**
  926. * Whether this task can deal with non-file resources.
  927. *
  928. * <p>&lt;copy&gt; can while &lt;move&gt; can't since we don't
  929. * know how to remove non-file resources.</p>
  930. *
  931. * <p>This implementation returns true only if this task is
  932. * &lt;copy&gt;. Any subclass of this class that also wants to
  933. * support non-file resources needs to override this method. We
  934. * need to do so for backwards compatibility reasons since we
  935. * can't expect subclasses to support resources.</p>
  936. * @return true if this task supports non file resources.
  937. * @since Ant 1.7
  938. */
  939. protected boolean supportsNonFileResources() {
  940. return getClass().equals(Copy.class);
  941. }
  942. /**
  943. * Adds the given strings to a list contained in the given map.
  944. * The file is the key into the map.
  945. */
  946. private static void add(File baseDir, final String[] names, final Map<File, List<String>> m) {
  947. if (names != null) {
  948. baseDir = getKeyFile(baseDir);
  949. List<String> l = m.get(baseDir);
  950. if (l == null) {
  951. l = new ArrayList<String>(names.length);
  952. m.put(baseDir, l);
  953. }
  954. l.addAll(java.util.Arrays.asList(names));
  955. }
  956. }
  957. /**
  958. * Adds the given string to a list contained in the given map.
  959. * The file is the key into the map.
  960. */
  961. private static void add(final File baseDir, final String name, final Map<File, List<String>> m) {
  962. if (name != null) {
  963. add(baseDir, new String[] {name}, m);
  964. }
  965. }
  966. /**
  967. * Either returns its argument or a placeholder if the argument is null.
  968. */
  969. private static File getKeyFile(final File f) {
  970. return f == null ? NULL_FILE_PLACEHOLDER : f;
  971. }
  972. /**
  973. * returns the mapper to use based on nested elements or the
  974. * flatten attribute.
  975. */
  976. private FileNameMapper getMapper() {
  977. FileNameMapper mapper = null;
  978. if (mapperElement != null) {
  979. mapper = mapperElement.getImplementation();
  980. } else if (flatten) {
  981. mapper = new FlatFileNameMapper();
  982. } else {
  983. mapper = new IdentityMapper();
  984. }
  985. return mapper;
  986. }
  987. /**
  988. * Handle getMessage() for exceptions.
  989. * @param ex the exception to handle
  990. * @return ex.getMessage() if ex.getMessage() is not null
  991. * otherwise return ex.toString()
  992. */
  993. private String getMessage(final Exception ex) {
  994. return ex.getMessage() == null ? ex.toString() : ex.getMessage();
  995. }
  996. /**
  997. * Returns a reason for failure based on
  998. * the exception thrown.
  999. * If the exception is not IOException output the class name,
  1000. * output the message
  1001. * if the exception is MalformedInput add a little note.
  1002. */
  1003. private String getDueTo(final Exception ex) {
  1004. final boolean baseIOException = ex.getClass() == IOException.class;
  1005. final StringBuffer message = new StringBuffer();
  1006. if (!baseIOException || ex.getMessage() == null) {
  1007. message.append(ex.getClass().getName());
  1008. }
  1009. if (ex.getMessage() != null) {
  1010. if (!baseIOException) {
  1011. message.append(" ");
  1012. }
  1013. message.append(ex.getMessage());
  1014. }
  1015. if (ex.getClass().getName().indexOf("MalformedInput") != -1) {
  1016. message.append(LINE_SEPARATOR);
  1017. message.append(
  1018. "This is normally due to the input file containing invalid");
  1019. message.append(LINE_SEPARATOR);
  1020. message.append("bytes for the character encoding used : ");
  1021. message.append(
  1022. (inputEncoding == null
  1023. ? fileUtils.getDefaultEncoding() : inputEncoding));
  1024. message.append(LINE_SEPARATOR);
  1025. }
  1026. return message.toString();
  1027. }
  1028. }