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.

CopyPath.java 6.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 org.apache.tools.ant.BuildException;
  20. import org.apache.tools.ant.Project;
  21. import org.apache.tools.ant.Task;
  22. import org.apache.tools.ant.util.FileUtils;
  23. import org.apache.tools.ant.util.FileNameMapper;
  24. import org.apache.tools.ant.types.Path;
  25. import org.apache.tools.ant.types.Reference;
  26. import java.io.File;
  27. import java.io.IOException;
  28. /**
  29. * Copy the contents of a path to a destination, using the mapper of choice
  30. *
  31. * @since Ant 1.7
  32. *
  33. * @ant.task category="filesystem"
  34. */
  35. public class CopyPath extends Task {
  36. // Error messages
  37. /** No destdir attribute */
  38. public static final String ERROR_NO_DESTDIR = "No destDir specified";
  39. /** No path */
  40. public static final String ERROR_NO_PATH = "No path specified";
  41. /** No mapper */
  42. public static final String ERROR_NO_MAPPER = "No mapper specified";
  43. // fileutils
  44. private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
  45. // --- Fields --
  46. private FileNameMapper mapper;
  47. private Path path;
  48. private File destDir;
  49. // TODO not read, yet in a public setter
  50. private long granularity = FILE_UTILS.getFileTimestampGranularity();
  51. private boolean preserveLastModified = false;
  52. /**
  53. * The dest dir attribute.
  54. * @param destDir the value of the destdir attribute.
  55. */
  56. public void setDestDir(File destDir) {
  57. this.destDir = destDir;
  58. }
  59. /**
  60. * add a mapper
  61. *
  62. * @param newmapper the mapper to add.
  63. */
  64. public void add(FileNameMapper newmapper) {
  65. if (mapper != null) {
  66. throw new BuildException("Only one mapper allowed");
  67. }
  68. mapper = newmapper;
  69. }
  70. /**
  71. * Set the path to be used when running the Java class.
  72. *
  73. * @param s
  74. * an Ant Path object containing the path.
  75. */
  76. public void setPath(Path s) {
  77. createPath().append(s);
  78. }
  79. /**
  80. * Set the path to use by reference.
  81. *
  82. * @param r
  83. * a reference to an existing path.
  84. */
  85. public void setPathRef(Reference r) {
  86. createPath().setRefid(r);
  87. }
  88. /**
  89. * Create a path.
  90. *
  91. * @return a path to be configured.
  92. */
  93. public Path createPath() {
  94. if (path == null) {
  95. path = new Path(getProject());
  96. }
  97. return path;
  98. }
  99. /**
  100. * Set the number of milliseconds leeway to give before deciding a
  101. * target is out of date.
  102. * TODO: This is not yet used.
  103. * @param granularity the granularity used to decide if a target is out of
  104. * date.
  105. */
  106. public void setGranularity(long granularity) {
  107. this.granularity = granularity;
  108. }
  109. /**
  110. * Give the copied files the same last modified time as the original files.
  111. * @param preserveLastModified if true preserve the modified time;
  112. * default is false.
  113. */
  114. public void setPreserveLastModified(boolean preserveLastModified) {
  115. this.preserveLastModified = preserveLastModified;
  116. }
  117. /**
  118. * Ensure we have a consistent and legal set of attributes, and set any
  119. * internal flags necessary based on different combinations of attributes.
  120. *
  121. * @throws BuildException
  122. * if an error occurs.
  123. */
  124. protected void validateAttributes() throws BuildException {
  125. if (destDir == null) {
  126. throw new BuildException(ERROR_NO_DESTDIR);
  127. }
  128. if (mapper == null) {
  129. throw new BuildException(ERROR_NO_MAPPER);
  130. }
  131. if (path == null) {
  132. throw new BuildException(ERROR_NO_PATH);
  133. }
  134. }
  135. /**
  136. * This is a very minimal derivative of the nomal copy logic.
  137. *
  138. * @throws BuildException
  139. * if something goes wrong with the build.
  140. */
  141. public void execute() throws BuildException {
  142. validateAttributes();
  143. String[] sourceFiles = path.list();
  144. if (sourceFiles.length == 0) {
  145. log("Path is empty", Project.MSG_VERBOSE);
  146. return;
  147. }
  148. for (int sources = 0; sources < sourceFiles.length; sources++) {
  149. String sourceFileName = sourceFiles[sources];
  150. File sourceFile = new File(sourceFileName);
  151. String[] toFiles = (String[]) mapper.mapFileName(sourceFileName);
  152. for (int i = 0; i < toFiles.length; i++) {
  153. String destFileName = toFiles[i];
  154. File destFile = new File(destDir, destFileName);
  155. if (sourceFile.equals(destFile)) {
  156. log("Skipping self-copy of " + sourceFileName, Project.MSG_VERBOSE);
  157. continue;
  158. }
  159. if (sourceFile.isDirectory()) {
  160. log("Skipping directory " + sourceFileName);
  161. continue;
  162. }
  163. try {
  164. log("Copying " + sourceFile + " to " + destFile, Project.MSG_VERBOSE);
  165. FILE_UTILS.copyFile(sourceFile, destFile, null, null, false,
  166. preserveLastModified, null, null, getProject());
  167. } catch (IOException ioe) {
  168. String msg = "Failed to copy " + sourceFile + " to " + destFile + " due to "
  169. + ioe.getMessage();
  170. if (destFile.exists() && !destFile.delete()) {
  171. msg += " and I couldn't delete the corrupt " + destFile;
  172. }
  173. throw new BuildException(msg, ioe, getLocation());
  174. }
  175. }
  176. }
  177. }
  178. }