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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2001 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.Enumeration;
  57. import java.util.Vector;
  58. import java.util.Date;
  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.FileSet;
  64. import org.apache.tools.ant.types.FileList;
  65. /**
  66. * A Task to record explicit dependencies. 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 target
  82. * are required.
  83. * <p>
  84. * This task will examine each of the source files against each of the target files.
  85. * If any target files are out of date with respect to any of the source files, all targets are removed.
  86. * If any files named in a (src or target) filelist do not exist, all targets are removed.
  87. * Hint: If missing files should be ignored, specify them as include patterns in filesets,
  88. * rather than using filelists.
  89. * </p><p>
  90. * This task attempts to optimize speed of dependency checking. It will stop after the first
  91. * out of date file is found and remove all targets, rather than exhaustively checking every
  92. * source vs target combination unnecessarily.
  93. * </p><p>
  94. * Example uses:
  95. * <ulist><li>
  96. * Record the fact that an XML file must be up to date
  97. * with respect to its XSD (Schema file), even though the XML file
  98. * itself includes no reference to its XSD.
  99. * </li><li>
  100. * Record the fact that an XSL stylesheet includes other
  101. * sub-stylesheets
  102. * </li><li>
  103. * Record the fact that java files must be recompiled if the ant build
  104. * file changes
  105. * </li></ulist>
  106. *
  107. * @author <a href="mailto:cstrong@arielpartners.com">Craeg Strong</a>
  108. * @version $Revision$ $Date$
  109. */
  110. public class DependSet extends MatchingTask {
  111. private Vector sourceFileSets = new Vector();
  112. private Vector sourceFileLists = new Vector();
  113. private Vector targetFileSets = new Vector();
  114. private Vector targetFileLists = new Vector();
  115. /**
  116. * Creates a new DependSet Task.
  117. **/
  118. public DependSet() {
  119. } //-- DependSet
  120. /**
  121. * Nested &lt;srcfileset&gt; element.
  122. */
  123. public void addSrcfileset(FileSet fs) {
  124. sourceFileSets.addElement(fs);
  125. }
  126. /**
  127. * Nested &lt;srcfilelist&gt; element.
  128. */
  129. public void addSrcfilelist(FileList fl) {
  130. sourceFileLists.addElement(fl);
  131. }
  132. /**
  133. * Nested &lt;targetfileset&gt; element.
  134. */
  135. public void addTargetfileset(FileSet fs) {
  136. targetFileSets.addElement(fs);
  137. }
  138. /**
  139. * Nested &lt;targetfilelist&gt; element.
  140. */
  141. public void addTargetfilelist(FileList fl) {
  142. targetFileLists.addElement(fl);
  143. }
  144. /**
  145. * Executes the task.
  146. */
  147. public void execute() throws BuildException {
  148. if ( (sourceFileSets.size() == 0) && (sourceFileLists.size() == 0) ) {
  149. throw new BuildException("At least one <srcfileset> or <srcfilelist> element must be set");
  150. }
  151. if ( (targetFileSets.size() == 0) && (targetFileLists.size() == 0) ) {
  152. throw new BuildException("At least one <targetfileset> or <targetfilelist> element must be set");
  153. }
  154. long now = (new Date()).getTime();
  155. /*
  156. If we're on Windows, we have to munge the time up to 2 secs to
  157. be able to check file modification times.
  158. (Windows has a max resolution of two secs for modification times)
  159. */
  160. if (Os.isFamily("windows")) {
  161. now += 2000;
  162. }
  163. //
  164. // Grab all the target files specified via filesets
  165. //
  166. Vector allTargets = new Vector();
  167. Enumeration enumTargetSets = targetFileSets.elements();
  168. while (enumTargetSets.hasMoreElements()) {
  169. FileSet targetFS = (FileSet) enumTargetSets.nextElement();
  170. DirectoryScanner targetDS = targetFS.getDirectoryScanner(project);
  171. String[] targetFiles = targetDS.getIncludedFiles();
  172. for (int i = 0; i < targetFiles.length; i++) {
  173. File dest = new File(targetFS.getDir(project), targetFiles[i]);
  174. allTargets.addElement(dest);
  175. if (dest.lastModified() > now) {
  176. log("Warning: "+targetFiles[i]+" modified in the future.",
  177. Project.MSG_WARN);
  178. }
  179. }
  180. }
  181. //
  182. // Grab all the target files specified via filelists
  183. //
  184. boolean upToDate = true;
  185. Enumeration enumTargetLists = targetFileLists.elements();
  186. while (enumTargetLists.hasMoreElements()) {
  187. FileList targetFL = (FileList) enumTargetLists.nextElement();
  188. String[] targetFiles = targetFL.getFiles(project);
  189. for (int i = 0; i < targetFiles.length; i++) {
  190. File dest = new File(targetFL.getDir(project), targetFiles[i]);
  191. if (!dest.exists()) {
  192. log(targetFiles[i]+ " does not exist.", Project.MSG_VERBOSE);
  193. upToDate = false;
  194. continue;
  195. }
  196. else {
  197. allTargets.addElement(dest);
  198. }
  199. if (dest.lastModified() > now) {
  200. log("Warning: "+targetFiles[i]+" modified in the future.",
  201. Project.MSG_WARN);
  202. }
  203. }
  204. }
  205. //
  206. // Check targets vs source files specified via filesets
  207. //
  208. if (upToDate) {
  209. Enumeration enumSourceSets = sourceFileSets.elements();
  210. while (upToDate && enumSourceSets.hasMoreElements()) {
  211. FileSet sourceFS = (FileSet) enumSourceSets.nextElement();
  212. DirectoryScanner sourceDS = sourceFS.getDirectoryScanner(project);
  213. String[] sourceFiles = sourceDS.getIncludedFiles();
  214. for (int i=0; upToDate && i < sourceFiles.length; i++) {
  215. File src = new File(sourceFS.getDir(project), sourceFiles[i]);
  216. if (src.lastModified() > now) {
  217. log("Warning: "+sourceFiles[i]+" modified in the future.",
  218. Project.MSG_WARN);
  219. }
  220. Enumeration enumTargets = allTargets.elements();
  221. while (upToDate && enumTargets.hasMoreElements()) {
  222. File dest = (File)enumTargets.nextElement();
  223. if (src.lastModified() > dest.lastModified()) {
  224. log(dest.getPath() + " is out of date with respect to " +
  225. sourceFiles[i], Project.MSG_VERBOSE);
  226. upToDate = false;
  227. }
  228. }
  229. }
  230. }
  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(project);
  240. int i = 0;
  241. do {
  242. File src = new File(sourceFL.getDir(project), sourceFiles[i]);
  243. if (src.lastModified() > now) {
  244. log("Warning: "+sourceFiles[i]+" modified in the future.",
  245. Project.MSG_WARN);
  246. }
  247. if (!src.exists()) {
  248. log(sourceFiles[i]+ " does not exist.", Project.MSG_VERBOSE);
  249. upToDate = false;
  250. break;
  251. }
  252. Enumeration enumTargets = allTargets.elements();
  253. while (upToDate && enumTargets.hasMoreElements()) {
  254. File dest = (File)enumTargets.nextElement();
  255. if (src.lastModified() > dest.lastModified()) {
  256. log(dest.getPath() + " is out of date with respect to " +
  257. sourceFiles[i], Project.MSG_VERBOSE);
  258. upToDate = false;
  259. }
  260. }
  261. } while (upToDate && (++i < sourceFiles.length) );
  262. }
  263. }
  264. if (!upToDate) {
  265. log("Deleting all target files. ", Project.MSG_VERBOSE);
  266. for (Enumeration e = allTargets.elements(); e.hasMoreElements(); ) {
  267. File fileToRemove = (File)e.nextElement();
  268. log("Deleting file " + fileToRemove.getAbsolutePath(), Project.MSG_VERBOSE);
  269. fileToRemove.delete();
  270. }
  271. }
  272. } //-- execute
  273. } //-- DependSet.java