source: release-kits/lirk3/resources/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/DependSet.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 10.6 KB
Line 
1/*
2 * Copyright 2001-2005 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.ant.taskdefs;
19
20import java.io.File;
21import java.util.Date;
22import java.util.Enumeration;
23import java.util.Vector;
24import org.apache.tools.ant.BuildException;
25import org.apache.tools.ant.DirectoryScanner;
26import org.apache.tools.ant.Project;
27import org.apache.tools.ant.types.FileList;
28import org.apache.tools.ant.types.FileSet;
29import org.apache.tools.ant.util.FileUtils;
30
31/**
32 * Examines and removes out of date target files. If any of the target files
33 * are out of date with respect to any of the source files, all target
34 * files are removed. This is useful where dependencies cannot be
35 * computed (for example, dynamically interpreted parameters or files
36 * that need to stay in synch but are not directly linked) or where
37 * the ant task in question could compute them but does not (for
38 * example, the linked DTD for an XML file using the style task).
39 *
40 * nested arguments:
41 * <ul>
42 * <li>srcfileset (fileset describing the source files to examine)
43 * <li>srcfilelist (filelist describing the source files to examine)
44 * <li>targetfileset (fileset describing the target files to examine)
45 * <li>targetfilelist (filelist describing the target files to examine)
46 * </ul>
47 * At least one instance of either a fileset or filelist for both source and
48 * target are required.
49 * <p>
50 * This task will examine each of the source files against each of the target
51 * files. If any target files are out of date with respect to any of the source
52 * files, all targets are removed. If any files named in a (src or target)
53 * filelist do not exist, all targets are removed.
54 * Hint: If missing files should be ignored, specify them as include patterns
55 * in filesets, rather than using filelists.
56 * </p><p>
57 * This task attempts to optimize speed of dependency checking. It will stop
58 * after the first out of date file is found and remove all targets, rather
59 * than exhaustively checking every source vs target combination unnecessarily.
60 * </p><p>
61 * Example uses:
62 * <ul><li>
63 * Record the fact that an XML file must be up to date
64 * with respect to its XSD (Schema file), even though the XML file
65 * itself includes no reference to its XSD.
66 * </li><li>
67 * Record the fact that an XSL stylesheet includes other
68 * sub-stylesheets
69 * </li><li>
70 * Record the fact that java files must be recompiled if the ant build
71 * file changes
72 * </li></ul>
73 *
74 * @ant.task category="filesystem"
75 * @since Ant 1.4
76 */
77public class DependSet extends MatchingTask {
78
79 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
80
81 private Vector sourceFileSets = new Vector();
82 private Vector sourceFileLists = new Vector();
83 private Vector targetFileSets = new Vector();
84 private Vector targetFileLists = new Vector();
85
86 /**
87 * Creates a new DependSet Task.
88 **/
89 public DependSet() {
90 } //-- DependSet
91
92 /**
93 * Add a set of source files.
94 * @param fs the FileSet to add.
95 */
96 public void addSrcfileset(FileSet fs) {
97 sourceFileSets.addElement(fs);
98 }
99
100 /**
101 * Add a list of source files.
102 * @param fl the FileList to add.
103 */
104 public void addSrcfilelist(FileList fl) {
105 sourceFileLists.addElement(fl);
106 }
107
108 /**
109 * Add a set of target files.
110 * @param fs the FileSet to add.
111 */
112 public void addTargetfileset(FileSet fs) {
113 targetFileSets.addElement(fs);
114 }
115
116 /**
117 * Add a list of target files.
118 * @param fl the FileList to add.
119 */
120 public void addTargetfilelist(FileList fl) {
121 targetFileLists.addElement(fl);
122 }
123
124 /**
125 * Executes the task.
126 * @throws BuildException if errors occur.
127 */
128 public void execute() throws BuildException {
129
130 if ((sourceFileSets.size() == 0) && (sourceFileLists.size() == 0)) {
131 throw new BuildException("At least one <srcfileset> or <srcfilelist>"
132 + " element must be set");
133 }
134 if ((targetFileSets.size() == 0) && (targetFileLists.size() == 0)) {
135 throw new BuildException("At least one <targetfileset> or"
136 + " <targetfilelist> element must be set");
137 }
138 long now = (new Date()).getTime();
139 /*
140 We have to munge the time to allow for the filesystem time
141 granularity.
142 */
143 now += FILE_UTILS.getFileTimestampGranularity();
144
145 // Grab all the target files specified via filesets:
146 Vector allTargets = new Vector();
147 long oldestTargetTime = 0;
148 File oldestTarget = null;
149 Enumeration enumTargetSets = targetFileSets.elements();
150 while (enumTargetSets.hasMoreElements()) {
151
152 FileSet targetFS = (FileSet) enumTargetSets.nextElement();
153 if (!targetFS.getDir(getProject()).exists()) {
154 // this is the same as if it was empty, no target files found
155 continue;
156 }
157 DirectoryScanner targetDS = targetFS.getDirectoryScanner(getProject());
158 String[] targetFiles = targetDS.getIncludedFiles();
159
160 for (int i = 0; i < targetFiles.length; i++) {
161
162 File dest = new File(targetFS.getDir(getProject()), targetFiles[i]);
163 allTargets.addElement(dest);
164
165 if (dest.lastModified() > now) {
166 log("Warning: " + targetFiles[i] + " modified in the future.",
167 Project.MSG_WARN);
168 }
169 if (oldestTarget == null
170 || dest.lastModified() < oldestTargetTime) {
171 oldestTargetTime = dest.lastModified();
172 oldestTarget = dest;
173 }
174 }
175 }
176 // Grab all the target files specified via filelists:
177 boolean upToDate = true;
178 Enumeration enumTargetLists = targetFileLists.elements();
179 while (enumTargetLists.hasMoreElements()) {
180
181 FileList targetFL = (FileList) enumTargetLists.nextElement();
182 String[] targetFiles = targetFL.getFiles(getProject());
183
184 for (int i = 0; i < targetFiles.length; i++) {
185
186 File dest = new File(targetFL.getDir(getProject()), targetFiles[i]);
187 if (!dest.exists()) {
188 log(targetFiles[i] + " does not exist.", Project.MSG_VERBOSE);
189 upToDate = false;
190 continue;
191 } else {
192 allTargets.addElement(dest);
193 }
194 if (dest.lastModified() > now) {
195 log("Warning: " + targetFiles[i] + " modified in the future.",
196 Project.MSG_WARN);
197 }
198 if (oldestTarget == null
199 || dest.lastModified() < oldestTargetTime) {
200 oldestTargetTime = dest.lastModified();
201 oldestTarget = dest;
202 }
203 }
204 }
205 if (oldestTarget != null) {
206 log(oldestTarget + " is oldest target file", Project.MSG_VERBOSE);
207 } else {
208 // no target files, then we cannot remove any target files and
209 // skip the following tests right away
210 upToDate = false;
211 }
212 // Check targets vs source files specified via filelists:
213 if (upToDate) {
214 Enumeration enumSourceLists = sourceFileLists.elements();
215 while (upToDate && enumSourceLists.hasMoreElements()) {
216
217 FileList sourceFL = (FileList) enumSourceLists.nextElement();
218 String[] sourceFiles = sourceFL.getFiles(getProject());
219
220 for (int i = 0; upToDate && i < sourceFiles.length; i++) {
221 File src = new File(sourceFL.getDir(getProject()), sourceFiles[i]);
222
223 if (src.lastModified() > now) {
224 log("Warning: " + sourceFiles[i]
225 + " modified in the future.", Project.MSG_WARN);
226 }
227 if (!src.exists()) {
228 log(sourceFiles[i] + " does not exist.",
229 Project.MSG_VERBOSE);
230 upToDate = false;
231 break;
232 }
233 if (src.lastModified() > oldestTargetTime) {
234 upToDate = false;
235 log(oldestTarget + " is out of date with respect to "
236 + sourceFiles[i], Project.MSG_VERBOSE);
237 }
238 }
239 }
240 }
241 // Check targets vs source files specified via filesets:
242 if (upToDate) {
243 Enumeration enumSourceSets = sourceFileSets.elements();
244 while (upToDate && enumSourceSets.hasMoreElements()) {
245
246 FileSet sourceFS = (FileSet) enumSourceSets.nextElement();
247 DirectoryScanner sourceDS = sourceFS.getDirectoryScanner(getProject());
248 String[] sourceFiles = sourceDS.getIncludedFiles();
249
250 for (int i = 0; upToDate && i < sourceFiles.length; i++) {
251 File src = new File(sourceFS.getDir(getProject()), sourceFiles[i]);
252
253 if (src.lastModified() > now) {
254 log("Warning: " + sourceFiles[i]
255 + " modified in the future.", Project.MSG_WARN);
256 }
257 if (src.lastModified() > oldestTargetTime) {
258 upToDate = false;
259 log(oldestTarget + " is out of date with respect to "
260 + sourceFiles[i], Project.MSG_VERBOSE);
261 }
262 }
263 }
264 }
265 if (!upToDate) {
266 log("Deleting all target files. ", Project.MSG_VERBOSE);
267 for (Enumeration e = allTargets.elements(); e.hasMoreElements();) {
268 File fileToRemove = (File) e.nextElement();
269 log("Deleting file " + fileToRemove.getAbsolutePath(),
270 Project.MSG_VERBOSE);
271 fileToRemove.delete();
272 }
273 }
274 }
275}
Note: See TracBrowser for help on using the repository browser.