KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ManifestToolTask


1 /*
2  * Copyright 1999-2004 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 import java.io.File JavaDoc;
18 import java.io.FileWriter JavaDoc;
19 import java.io.IOException JavaDoc;
20
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.Task;
23
24 /**
25  * Creates Manifest file with the all the JARs and modification dates
26  * in the specified directory.
27  *
28  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
29  * @version CVS $Id: ManifestToolTask.java 30932 2004-07-29 17:35:38Z vgritsenko $
30  */

31 public final class ManifestToolTask extends Task {
32
33     private String JavaDoc directory;
34     private String JavaDoc manifest;
35
36     public void setDirectory(String JavaDoc directory) {
37         this.directory = directory;
38     }
39
40     public void setManifest(String JavaDoc manifest) {
41         this.manifest = manifest;
42     }
43
44     public void execute() throws BuildException {
45         if (this.manifest == null) {
46             throw new BuildException("manifest attribute is required", this.getLocation());
47         }
48         if (this.directory == null) {
49             throw new BuildException("directory attribute is required", this.getLocation());
50         }
51
52         try {
53             // process recursive
54
this.process(this.getProject().resolveFile(this.directory));
55         } catch (IOException JavaDoc ioe) {
56             throw new BuildException("IOException: " + ioe);
57         }
58     }
59
60     /**
61      * Scan recursive
62      */

63     private void process(final File JavaDoc directoryFile)
64     throws IOException JavaDoc, BuildException {
65
66         System.out.println("Writing: " + manifest);
67         FileWriter JavaDoc w = new FileWriter JavaDoc(this.getProject().resolveFile(manifest));
68         w.write("Manifest-Version: 1.0\n");
69
70         if (directoryFile.exists() && directoryFile.isDirectory() ) {
71             w.write("Cocoon-Libs: ");
72
73             final File JavaDoc[] files = directoryFile.listFiles();
74             for(int i = 0; i < files.length; i++) {
75                 if (files[i].getName().endsWith(".jar")) {
76                     w.write(files[i].getName());
77                     w.write(" ");
78                 }
79             }
80             w.write("\n");
81
82             for(int i = 0; i < files.length; i++) {
83                 if (files[i].getName().endsWith(".jar")) {
84                     w.write("Cocoon-Lib-");
85                     String JavaDoc s = files[i].getName().replace('.', '_');
86                     w.write(s);
87                     w.write(": ");
88                     w.write(String.valueOf(files[i].lastModified()));
89                     w.write("\n");
90                 }
91             }
92         }
93         w.close();
94     }
95 }
96
Popular Tags