001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package anttasks;
018:
019: import java.io.File;
020: import java.io.FileWriter;
021: import java.io.IOException;
022:
023: import org.apache.tools.ant.BuildException;
024: import org.apache.tools.ant.Task;
025:
026: /**
027: * Creates Manifest file with the all the JARs and modification dates
028: * in the specified directory.
029: *
030: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
031: * @version CVS $Id: ManifestToolTask.java 433543 2006-08-22 06:22:54Z crossley $
032: */
033: public final class ManifestToolTask extends Task {
034:
035: private String directory;
036: private String manifest;
037:
038: public void setDirectory(String directory) {
039: this .directory = directory;
040: }
041:
042: public void setManifest(String manifest) {
043: this .manifest = manifest;
044: }
045:
046: public void execute() throws BuildException {
047: if (this .manifest == null) {
048: throw new BuildException("manifest attribute is required",
049: this .getLocation());
050: }
051: if (this .directory == null) {
052: throw new BuildException("directory attribute is required",
053: this .getLocation());
054: }
055:
056: try {
057: // process recursive
058: this .process(this .getProject().resolveFile(this .directory));
059: } catch (IOException ioe) {
060: throw new BuildException("IOException: " + ioe);
061: }
062: }
063:
064: /**
065: * Scan recursive
066: */
067: private void process(final File directoryFile) throws IOException,
068: BuildException {
069:
070: System.out.println("Writing: " + manifest);
071: FileWriter w = new FileWriter(this .getProject().resolveFile(
072: manifest));
073: w.write("Manifest-Version: 1.0\n");
074:
075: if (directoryFile.exists() && directoryFile.isDirectory()) {
076: w.write("Cocoon-Libs: ");
077:
078: final File[] files = directoryFile.listFiles();
079: for (int i = 0; i < files.length; i++) {
080: if (files[i].getName().endsWith(".jar")) {
081: w.write(files[i].getName());
082: w.write(" ");
083: }
084: }
085: w.write("\n");
086:
087: for (int i = 0; i < files.length; i++) {
088: if (files[i].getName().endsWith(".jar")) {
089: w.write("Cocoon-Lib-");
090: String s = files[i].getName().replace('.', '_');
091: w.write(s);
092: w.write(": ");
093: w.write(String.valueOf(files[i].lastModified()));
094: w.write("\n");
095: }
096: }
097: }
098: w.close();
099: }
100: }
|