View Javadoc

1   /*   Copyright (C) 2004 Finalist IT Group
2    *
3    *   This file is part of JAG - the Java J2EE Application Generator
4    *
5    *   JAG is free software; you can redistribute it and/or modify
6    *   it under the terms of the GNU General Public License as published by
7    *   the Free Software Foundation; either version 2 of the License, or
8    *   (at your option) any later version.
9    *   JAG is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   *   GNU General Public License for more details.
13   *   You should have received a copy of the GNU General Public License
14   *   along with JAG; if not, write to the Free Software
15   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   */
17  
18  package com.finalist.tools.ant.taskdefs;
19  
20  // IMPORTANT! You may need to mount ant.jar before this class will
21  // compile. So mount the JAR modules/ext/ant.jar (NOT modules/ant.jar)
22  // from your IDE installation directory in your Filesystems before
23  // continuing to ensure that it is in your classpath.
24  
25  import org.apache.tools.ant.BuildException;
26  import org.apache.tools.ant.Task;
27  import org.apache.tools.ant.taskdefs.Available;
28  import org.apache.tools.ant.taskdefs.Cvs;
29  
30  import java.io.File;
31  import java.util.Iterator;
32  import java.util.LinkedList;
33  import java.util.List;
34  
35  /***
36   * Checks if an external library exists in the specified directory and has
37   * the correct version, or otherwise checks out the correct library from CVS.
38   * This ant target has the following format:
39   * <PRE>
40   * &lt;checklib dir="${lib.dir}"&gt;
41   *   &lt;lib version="v1_4_1" file="shared/jakarta/ant/ant.jar" /&gt;
42   *   &lt;lib version="v1_4_1" file="shared/jakarta/ant/optional.jar" /&gt;
43   * &lt;/checklib&gt;
44   * </PRE>
45   * Where 'version' is the required CVS revision tag and 'file' the relative path
46   * in CVS of the library file. If the attribute 'dir' of the checklib target is
47   * not specified, the default value "lib" is used.
48   *
49   * @author Keesjan van Bunningen - Finalist IT Group bv
50   * @version $Revision: 1.1 $, $Date: 2004/03/01 18:55:00 $
51   */
52  public class LibAvailableTask extends Task {
53  
54     private static final String TMP_DIR_NAME = "tmp";
55     /*** destination directory */
56     private File directory;
57     private List libs = new LinkedList(); // List<Lib>
58  
59  
60     /***
61      * Optional destination directory, defaults to "lib".
62      * <PRE><checklib dir="foo/bar"/></PRE>
63      * @param directory  the directory containing the library.
64      */
65     public void setDir(File directory) {
66        // Note: f will automatically be absolute (resolved from project basedir).
67        this.directory = directory;
68     }
69  
70  
71     /***
72      * Add a nested jar element as required external library.
73      * @param lib  the external library file
74      */
75     public void addLib(Lib lib) {
76        libs.add(lib);
77     }
78     // <customtask>
79     //     <nestme val="something"/>
80     // </customtask>
81  
82  
83     public void execute() throws BuildException {
84        if (this.directory == null) {
85           this.directory = new File("lib"); // default
86        }
87        if (!this.directory.exists()) {
88           this.directory.mkdirs();
89        }
90        if (!directory.isDirectory()) {
91           throw new BuildException("Destination directory not properly set.");
92        }
93        File tmpDir = new File(TMP_DIR_NAME);
94        if (!tmpDir.exists()) {
95           tmpDir.mkdirs();
96        }
97        Iterator libIterator = this.libs.iterator();
98        while (libIterator.hasNext()) {
99           Lib lib = (Lib) libIterator.next();
100          String filename = lib.getFile().substring(lib.getFile().lastIndexOf('/') + 1);
101          Available available = (Available) project.createTask("available");
102          available.setProperty("found." + filename);
103          available.setFile(new File(this.directory, filename));
104          available.execute();
105          if (!available.eval()) {
106             log("Exporting " + filename + " to " + TMP_DIR_NAME);
107             Cvs cvs = (Cvs) project.createTask("cvs");
108             cvs.setCommand("export -r " + lib.getVersion() + " -d " + TMP_DIR_NAME + " " + lib.getFile());
109             cvs.execute();
110             File tmpFile = new File(TMP_DIR_NAME, filename);
111             if (tmpFile.exists()) {
112                log("Copying " + TMP_DIR_NAME + File.separator + filename + " to " + this.directory.getPath() + File.separator + filename);
113                tmpFile.renameTo(new File(this.directory.getPath() + File.separator + filename));
114             }
115          }//end else
116       }//end while
117       tmpDir.delete();
118    }//end execute()
119 
120 }
121 
122 /*
123  * $Log: LibAvailableTask.java,v $
124  * Revision 1.1  2004/03/01 18:55:00  ekkelenkamp
125  * initial version
126  *
127  * Revision 1.3  2003/10/03 16:09:03  m.oconnor
128  * corrected javadoc & unused imports/methods/fields
129  *
130  * Revision 1.2  2003/04/18 10:55:44  hillebrand
131  * new anttasks added for checking out libs and including them in war and ear files
132  *
133  * Revision 1.1  2002/07/08 14:35:21  keesjan
134  * Separate submodule for custom Ant tasks
135  *
136  * Revision 1.3  2002/06/28 15:17:36  keesjan
137  * Moved to tools/statementexecutor
138  *
139  */