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.

UpToDate.java 7.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-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 org.apache.tools.ant.BuildException;
  56. import org.apache.tools.ant.DirectoryScanner;
  57. import org.apache.tools.ant.Project;
  58. import org.apache.tools.ant.taskdefs.condition.Condition;
  59. import org.apache.tools.ant.types.Mapper;
  60. import org.apache.tools.ant.types.FileSet;
  61. import org.apache.tools.ant.util.SourceFileScanner;
  62. import org.apache.tools.ant.util.FileNameMapper;
  63. import org.apache.tools.ant.util.MergingMapper;
  64. import java.io.File;
  65. import java.util.Enumeration;
  66. import java.util.Vector;
  67. /**
  68. * Will set the given property if the specified target has a timestamp
  69. * greater than all of the source files.
  70. *
  71. * @author William Ferguson <a href="mailto:williamf@mincom.com">williamf@mincom.com</a>
  72. * @author Hiroaki Nakamura <a href="mailto:hnakamur@mc.neweb.ne.jp">hnakamur@mc.neweb.ne.jp</a>
  73. * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  74. *
  75. * @ant.task category="control"
  76. */
  77. public class UpToDate extends MatchingTask implements Condition {
  78. private String _property;
  79. private String _value;
  80. private File _targetFile;
  81. private Vector sourceFileSets = new Vector();
  82. protected Mapper mapperElement = null;
  83. /**
  84. * The property to set if the target file is more up to date than each of
  85. * the source files.
  86. *
  87. * @param property the name of the property to set if Target is up to date.
  88. */
  89. public void setProperty(String property) {
  90. _property = property;
  91. }
  92. /**
  93. * The value to set the named property to if the target file is more up to
  94. * date than each of the source files. Defaults to 'true'.
  95. *
  96. * @param value the value to set the property to if Target is up to date
  97. */
  98. public void setValue(String value) {
  99. _value = value;
  100. }
  101. /**
  102. * Returns the value, or "true" if a specific value wasn't provided.
  103. */
  104. private String getValue() {
  105. return ( _value != null ) ? _value : "true";
  106. }
  107. /**
  108. * The file which must be more up to date than each of the source files
  109. * if the property is to be set.
  110. *
  111. * @param file the file which we are checking against.
  112. */
  113. public void setTargetFile(File file) {
  114. _targetFile = file;
  115. }
  116. /**
  117. * Nested &lt;srcfiles&gt; element.
  118. */
  119. public void addSrcfiles(FileSet fs) {
  120. sourceFileSets.addElement(fs);
  121. }
  122. /**
  123. * Defines the FileNameMapper to use (nested mapper element).
  124. */
  125. public Mapper createMapper() throws BuildException {
  126. if (mapperElement != null) {
  127. throw new BuildException("Cannot define more than one mapper",
  128. location);
  129. }
  130. mapperElement = new Mapper(project);
  131. return mapperElement;
  132. }
  133. /**
  134. * Evaluate all target and source files, see if the targets are up-to-date.
  135. */
  136. public boolean eval() {
  137. if (sourceFileSets.size() == 0) {
  138. throw new BuildException("At least one <srcfiles> element must be set");
  139. }
  140. if (_targetFile == null && mapperElement == null) {
  141. throw new BuildException("The targetfile attribute or a nested mapper element must be set");
  142. }
  143. // if not there then it can't be up to date
  144. if (_targetFile != null && !_targetFile.exists()) {
  145. return false;
  146. }
  147. Enumeration enum = sourceFileSets.elements();
  148. boolean upToDate = true;
  149. while (upToDate && enum.hasMoreElements()) {
  150. FileSet fs = (FileSet) enum.nextElement();
  151. DirectoryScanner ds = fs.getDirectoryScanner(project);
  152. upToDate = upToDate && scanDir(fs.getDir(project),
  153. ds.getIncludedFiles());
  154. }
  155. return upToDate;
  156. }
  157. /**
  158. * Sets property to true if target files have a more recent timestamp than
  159. * each of the corresponding source files.
  160. */
  161. public void execute() throws BuildException {
  162. boolean upToDate = eval();
  163. if (upToDate) {
  164. this.project.setProperty(_property, this.getValue());
  165. if (mapperElement == null) {
  166. log("File \"" + _targetFile.getAbsolutePath() + "\" is up to date.",
  167. Project.MSG_VERBOSE);
  168. } else {
  169. log("All target files have been up to date.",
  170. Project.MSG_VERBOSE);
  171. }
  172. }
  173. }
  174. protected boolean scanDir(File srcDir, String[] files) {
  175. SourceFileScanner sfs = new SourceFileScanner(this);
  176. FileNameMapper mapper = null;
  177. File dir = srcDir;
  178. if (mapperElement == null) {
  179. MergingMapper mm = new MergingMapper();
  180. mm.setTo(_targetFile.getAbsolutePath());
  181. mapper = mm;
  182. dir = null;
  183. } else {
  184. mapper = mapperElement.getImplementation();
  185. }
  186. return sfs.restrict(files, srcDir, dir, mapper).length == 0;
  187. }
  188. }