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.

DependSet.java 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2001-2002 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 java.io.File;
  56. import java.util.Date;
  57. import java.util.Enumeration;
  58. import java.util.Vector;
  59. import org.apache.tools.ant.BuildException;
  60. import org.apache.tools.ant.DirectoryScanner;
  61. import org.apache.tools.ant.Project;
  62. import org.apache.tools.ant.taskdefs.condition.Os;
  63. import org.apache.tools.ant.types.FileList;
  64. import org.apache.tools.ant.types.FileSet;
  65. /**
  66. * Examines and removes out of date target files. If any of the target files
  67. * are out of date with respect to any of the source files, all target
  68. * files are removed. This is useful where dependencies cannot be
  69. * computed (for example, dynamically interpreted parameters or files
  70. * that need to stay in synch but are not directly linked) or where
  71. * the ant task in question could compute them but does not (for
  72. * example, the linked DTD for an XML file using the style task).
  73. *
  74. * nested arguments:
  75. * <ul>
  76. * <li>srcfileset (fileset describing the source files to examine)
  77. * <li>srcfilelist (filelist describing the source files to examine)
  78. * <li>targetfileset (fileset describing the target files to examine)
  79. * <li>targetfilelist (filelist describing the target files to examine)
  80. * </ul>
  81. * At least one instance of either a fileset or filelist for both source and
  82. * target are required.
  83. * <p>
  84. * This task will examine each of the source files against each of the target
  85. * files. If any target files are out of date with respect to any of the source
  86. * files, all targets are removed. If any files named in a (src or target)
  87. * filelist do not exist, all targets are removed.
  88. * Hint: If missing files should be ignored, specify them as include patterns
  89. * in filesets, rather than using filelists.
  90. * </p><p>
  91. * This task attempts to optimize speed of dependency checking. It will stop
  92. * after the first out of date file is found and remove all targets, rather
  93. * than exhaustively checking every source vs target combination unnecessarily.
  94. * </p><p>
  95. * Example uses:
  96. * <ul><li>
  97. * Record the fact that an XML file must be up to date
  98. * with respect to its XSD (Schema file), even though the XML file
  99. * itself includes no reference to its XSD.
  100. * </li><li>
  101. * Record the fact that an XSL stylesheet includes other
  102. * sub-stylesheets
  103. * </li><li>
  104. * Record the fact that java files must be recompiled if the ant build
  105. * file changes
  106. * </li></ul>
  107. *
  108. * @author <a href="mailto:cstrong@arielpartners.com">Craeg Strong</a>
  109. * @ant.task category="filesystem"
  110. * @version $Revision$ $Date$
  111. * @since Ant 1.4
  112. */
  113. public class DependSet extends MatchingTask {
  114. private Vector sourceFileSets = new Vector();
  115. private Vector sourceFileLists = new Vector();
  116. private Vector targetFileSets = new Vector();
  117. private Vector targetFileLists = new Vector();
  118. /**
  119. * Creates a new DependSet Task.
  120. **/
  121. public DependSet() {
  122. } //-- DependSet
  123. /**
  124. * Add a set of source files.
  125. */
  126. public void addSrcfileset(FileSet fs) {
  127. sourceFileSets.addElement(fs);
  128. }
  129. /**
  130. * Add a list of source files.
  131. */
  132. public void addSrcfilelist(FileList fl) {
  133. sourceFileLists.addElement(fl);
  134. }
  135. /**
  136. * Add a set of target files.
  137. */
  138. public void addTargetfileset(FileSet fs) {
  139. targetFileSets.addElement(fs);
  140. }
  141. /**
  142. * Add a list of target files.
  143. */
  144. public void addTargetfilelist(FileList fl) {
  145. targetFileLists.addElement(fl);
  146. }
  147. /**
  148. * Executes the task.
  149. */
  150. public void execute() throws BuildException {
  151. if ((sourceFileSets.size() == 0) && (sourceFileLists.size() == 0)) {
  152. throw new BuildException("At least one <srcfileset> or <srcfilelist>"
  153. + " element must be set");
  154. }
  155. if ((targetFileSets.size() == 0) && (targetFileLists.size() == 0)) {
  156. throw new BuildException("At least one <targetfileset> or"
  157. + " <targetfilelist> element must be set");
  158. }
  159. long now = (new Date()).getTime();
  160. /*
  161. If we're on Windows, we have to munge the time up to 2 secs to
  162. be able to check file modification times.
  163. (Windows has a max resolution of two secs for modification times)
  164. */
  165. if (Os.isFamily("windows")) {
  166. now += 2000;
  167. }
  168. //
  169. // Grab all the target files specified via filesets
  170. //
  171. Vector allTargets = new Vector();
  172. long oldestTargetTime = 0;
  173. File oldestTarget = null;
  174. Enumeration enumTargetSets = targetFileSets.elements();
  175. while (enumTargetSets.hasMoreElements()) {
  176. FileSet targetFS = (FileSet) enumTargetSets.nextElement();
  177. if (!targetFS.getDir(getProject()).exists()) {
  178. // this is the same as if it was empty, no target files found
  179. continue;
  180. }
  181. DirectoryScanner targetDS = targetFS.getDirectoryScanner(getProject());
  182. String[] targetFiles = targetDS.getIncludedFiles();
  183. for (int i = 0; i < targetFiles.length; i++) {
  184. File dest = new File(targetFS.getDir(getProject()), targetFiles[i]);
  185. allTargets.addElement(dest);
  186. if (dest.lastModified() > now) {
  187. log("Warning: " + targetFiles[i] + " modified in the future.",
  188. Project.MSG_WARN);
  189. }
  190. if (oldestTarget == null ||
  191. dest.lastModified() < oldestTargetTime) {
  192. oldestTargetTime = dest.lastModified();
  193. oldestTarget = dest;
  194. }
  195. }
  196. }
  197. //
  198. // Grab all the target files specified via filelists
  199. //
  200. boolean upToDate = true;
  201. Enumeration enumTargetLists = targetFileLists.elements();
  202. while (enumTargetLists.hasMoreElements()) {
  203. FileList targetFL = (FileList) enumTargetLists.nextElement();
  204. String[] targetFiles = targetFL.getFiles(getProject());
  205. for (int i = 0; i < targetFiles.length; i++) {
  206. File dest = new File(targetFL.getDir(getProject()), targetFiles[i]);
  207. if (!dest.exists()) {
  208. log(targetFiles[i] + " does not exist.", Project.MSG_VERBOSE);
  209. upToDate = false;
  210. continue;
  211. } else {
  212. allTargets.addElement(dest);
  213. }
  214. if (dest.lastModified() > now) {
  215. log("Warning: " + targetFiles[i] + " modified in the future.",
  216. Project.MSG_WARN);
  217. }
  218. if (oldestTarget == null ||
  219. dest.lastModified() < oldestTargetTime) {
  220. oldestTargetTime = dest.lastModified();
  221. oldestTarget = dest;
  222. }
  223. }
  224. }
  225. if (oldestTarget != null) {
  226. log(oldestTarget + " is oldest target file", Project.MSG_VERBOSE);
  227. } else {
  228. // no target files, then we cannot remove any target files and
  229. // skip the following tests right away
  230. upToDate = false;
  231. }
  232. //
  233. // Check targets vs source files specified via filelists
  234. //
  235. if (upToDate) {
  236. Enumeration enumSourceLists = sourceFileLists.elements();
  237. while (upToDate && enumSourceLists.hasMoreElements()) {
  238. FileList sourceFL = (FileList) enumSourceLists.nextElement();
  239. String[] sourceFiles = sourceFL.getFiles(getProject());
  240. for (int i = 0; upToDate && i < sourceFiles.length; i++) {
  241. File src = new File(sourceFL.getDir(getProject()), sourceFiles[i]);
  242. if (src.lastModified() > now) {
  243. log("Warning: " + sourceFiles[i]
  244. + " modified in the future.", Project.MSG_WARN);
  245. }
  246. if (!src.exists()) {
  247. log(sourceFiles[i] + " does not exist.",
  248. Project.MSG_VERBOSE);
  249. upToDate = false;
  250. break;
  251. }
  252. if (src.lastModified() > oldestTargetTime) {
  253. upToDate = false;
  254. log(oldestTarget + " is out of date with respect to " +
  255. sourceFiles[i], Project.MSG_VERBOSE);
  256. }
  257. }
  258. }
  259. }
  260. //
  261. // Check targets vs source files specified via filesets
  262. //
  263. if (upToDate) {
  264. Enumeration enumSourceSets = sourceFileSets.elements();
  265. while (upToDate && enumSourceSets.hasMoreElements()) {
  266. FileSet sourceFS = (FileSet) enumSourceSets.nextElement();
  267. DirectoryScanner sourceDS = sourceFS.getDirectoryScanner(getProject());
  268. String[] sourceFiles = sourceDS.getIncludedFiles();
  269. for (int i = 0; upToDate && i < sourceFiles.length; i++) {
  270. File src = new File(sourceFS.getDir(getProject()), sourceFiles[i]);
  271. if (src.lastModified() > now) {
  272. log("Warning: " + sourceFiles[i]
  273. + " modified in the future.", Project.MSG_WARN);
  274. }
  275. if (src.lastModified() > oldestTargetTime) {
  276. upToDate = false;
  277. log(oldestTarget + " is out of date with respect to " +
  278. sourceFiles[i], Project.MSG_VERBOSE);
  279. }
  280. }
  281. }
  282. }
  283. if (!upToDate) {
  284. log("Deleting all target files. ", Project.MSG_VERBOSE);
  285. for (Enumeration e = allTargets.elements(); e.hasMoreElements();) {
  286. File fileToRemove = (File) e.nextElement();
  287. log("Deleting file " + fileToRemove.getAbsolutePath(),
  288. Project.MSG_VERBOSE);
  289. fileToRemove.delete();
  290. }
  291. }
  292. } //-- execute
  293. } //-- DependSet.java