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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.util.Iterator;
  21. import java.util.Vector;
  22. import org.apache.tools.ant.BuildException;
  23. import org.apache.tools.ant.Project;
  24. import org.apache.tools.ant.types.Path;
  25. import org.apache.tools.ant.types.FileSet;
  26. import org.apache.tools.ant.types.FileList;
  27. import org.apache.tools.ant.types.Resource;
  28. import org.apache.tools.ant.types.TimeComparison;
  29. import org.apache.tools.ant.types.ResourceCollection;
  30. import org.apache.tools.ant.types.resources.Sort;
  31. import org.apache.tools.ant.types.resources.Union;
  32. import org.apache.tools.ant.types.resources.Restrict;
  33. import org.apache.tools.ant.types.resources.Resources;
  34. import org.apache.tools.ant.types.resources.FileResource;
  35. import org.apache.tools.ant.types.resources.selectors.Not;
  36. import org.apache.tools.ant.types.resources.selectors.Exists;
  37. import org.apache.tools.ant.types.resources.selectors.ResourceSelector;
  38. import org.apache.tools.ant.types.resources.comparators.Reverse;
  39. import org.apache.tools.ant.types.resources.comparators.ResourceComparator;
  40. /**
  41. * Examines and removes out of date target files. If any of the target files
  42. * are out of date with respect to any of the source files, all target
  43. * files are removed. This is useful where dependencies cannot be
  44. * computed (for example, dynamically interpreted parameters or files
  45. * that need to stay in synch but are not directly linked) or where
  46. * the ant task in question could compute them but does not (for
  47. * example, the linked DTD for an XML file using the XSLT task).
  48. *
  49. * nested arguments:
  50. * <ul>
  51. * <li>sources (resource union describing the source resources to examine)
  52. * <li>srcfileset (fileset describing the source files to examine)
  53. * <li>srcfilelist (filelist describing the source files to examine)
  54. * <li>targets (path describing the target files to examine)
  55. * <li>targetfileset (fileset describing the target files to examine)
  56. * <li>targetfilelist (filelist describing the target files to examine)
  57. * </ul>
  58. * At least one of both source and target entities is required.
  59. * <p>
  60. * This task will examine each of the sources against each of the target files. If
  61. * any target files are out of date with respect to any of the sources, all targets
  62. * are removed. If any sources or targets do not exist, all targets are removed.
  63. * Hint: If missing files should be ignored, specify them as include patterns
  64. * in filesets, rather than using filelists.
  65. * </p><p>
  66. * This task attempts to optimize speed of dependency checking
  67. * by comparing only the dates of the oldest target file and the newest source.
  68. * </p><p>
  69. * Example uses:
  70. * <ul><li>
  71. * Record the fact that an XML file must be up to date with respect to its XSD
  72. * (Schema file), even though the XML file itself includes no reference to its XSD.
  73. * </li><li>
  74. * Record the fact that an XSL stylesheet includes other sub-stylesheets
  75. * </li><li>
  76. * Record the fact that java files must be recompiled if the ant build file changes
  77. * </li></ul>
  78. *
  79. * @ant.task category="filesystem"
  80. * @since Ant 1.4
  81. */
  82. public class DependSet extends MatchingTask {
  83. private static final ResourceSelector NOT_EXISTS = new Not(new Exists());
  84. private static final ResourceComparator DATE_ASC
  85. = new org.apache.tools.ant.types.resources.comparators.Date();
  86. private static final ResourceComparator DATE_DESC = new Reverse(DATE_ASC);
  87. private static class NonExistent extends Restrict {
  88. private NonExistent(ResourceCollection rc) {
  89. super.add(rc);
  90. super.add(NOT_EXISTS);
  91. }
  92. }
  93. private static class Xest extends Sort {
  94. private Xest(ResourceCollection rc, ResourceComparator c) {
  95. super.add(c);
  96. super.add(rc);
  97. }
  98. }
  99. private static class Oldest extends Xest {
  100. private Oldest(ResourceCollection rc) {
  101. super(rc, DATE_ASC);
  102. }
  103. }
  104. private static class Newest extends Xest {
  105. private Newest(ResourceCollection rc) {
  106. super(rc, DATE_DESC);
  107. }
  108. }
  109. private static class HideMissingBasedir implements ResourceCollection {
  110. private FileSet fs;
  111. private HideMissingBasedir(FileSet fs) {
  112. this.fs = fs;
  113. }
  114. public Iterator iterator() {
  115. return basedirExists() ? fs.iterator() : Resources.EMPTY_ITERATOR;
  116. }
  117. public int size() {
  118. return basedirExists() ? fs.size() : 0;
  119. }
  120. public boolean isFilesystemOnly() {
  121. return true;
  122. }
  123. private boolean basedirExists() {
  124. File basedir = fs.getDir();
  125. //trick to evoke "basedir not set" if null:
  126. return basedir == null || basedir.exists();
  127. }
  128. }
  129. private Union sources = null;
  130. private Path targets = null;
  131. /**
  132. * Create a nested sources element.
  133. * @return a Union instance.
  134. */
  135. public synchronized Union createSources() {
  136. sources = (sources == null) ? new Union() : sources;
  137. return sources;
  138. }
  139. /**
  140. * Add a set of source files.
  141. * @param fs the FileSet to add.
  142. */
  143. public void addSrcfileset(FileSet fs) {
  144. createSources().add(fs);
  145. }
  146. /**
  147. * Add a list of source files.
  148. * @param fl the FileList to add.
  149. */
  150. public void addSrcfilelist(FileList fl) {
  151. createSources().add(fl);
  152. }
  153. /**
  154. * Create a nested targets element.
  155. * @return a Union instance.
  156. */
  157. public synchronized Path createTargets() {
  158. targets = (targets == null) ? new Path(getProject()) : targets;
  159. return targets;
  160. }
  161. /**
  162. * Add a set of target files.
  163. * @param fs the FileSet to add.
  164. */
  165. public void addTargetfileset(FileSet fs) {
  166. createTargets().add(new HideMissingBasedir(fs));
  167. }
  168. /**
  169. * Add a list of target files.
  170. * @param fl the FileList to add.
  171. */
  172. public void addTargetfilelist(FileList fl) {
  173. createTargets().add(fl);
  174. }
  175. /**
  176. * Execute the task.
  177. * @throws BuildException if errors occur.
  178. */
  179. public void execute() throws BuildException {
  180. if (sources == null) {
  181. throw new BuildException(
  182. "At least one set of source resources must be specified");
  183. }
  184. if (targets == null) {
  185. throw new BuildException(
  186. "At least one set of target files must be specified");
  187. }
  188. //no sources = nothing to compare; no targets = nothing to delete:
  189. if (sources.size() > 0 && targets.size() > 0 && !uptodate(sources, targets)) {
  190. log("Deleting all target files.", Project.MSG_VERBOSE);
  191. Delete delete = new Delete();
  192. delete.bindToOwner(this);
  193. delete.add(targets);
  194. delete.perform();
  195. }
  196. }
  197. private boolean uptodate(ResourceCollection src, ResourceCollection target) {
  198. org.apache.tools.ant.types.resources.selectors.Date datesel
  199. = new org.apache.tools.ant.types.resources.selectors.Date();
  200. datesel.setMillis(System.currentTimeMillis());
  201. datesel.setWhen(TimeComparison.AFTER);
  202. logFuture(targets, datesel);
  203. int neTargets = new NonExistent(targets).size();
  204. if (neTargets > 0) {
  205. log(neTargets + " nonexistent targets", Project.MSG_VERBOSE);
  206. return false;
  207. }
  208. FileResource oldestTarget = (FileResource) (new Oldest(targets).iterator().next());
  209. log(oldestTarget + " is oldest target file", Project.MSG_VERBOSE);
  210. logFuture(sources, datesel);
  211. int neSources = new NonExistent(sources).size();
  212. if (neSources > 0) {
  213. log(neSources + " nonexistent sources", Project.MSG_VERBOSE);
  214. return false;
  215. }
  216. Resource newestSource = (Resource) (new Newest(sources).iterator().next());
  217. log(newestSource.toLongString() + " is newest source", Project.MSG_VERBOSE);
  218. return oldestTarget.getLastModified() >= newestSource.getLastModified();
  219. }
  220. private void logFuture(ResourceCollection rc, ResourceSelector rsel) {
  221. Restrict r = new Restrict();
  222. r.add(rsel);
  223. r.add(rc);
  224. for (Iterator i = r.iterator(); i.hasNext();) {
  225. log("Warning: " + i.next() + " modified in the future.", Project.MSG_WARN);
  226. }
  227. }
  228. }