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

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