1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.finalist.tools.ant.taskdefs;
19
20
21
22
23
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 * <checklib dir="${lib.dir}">
41 * <lib version="v1_4_1" file="shared/jakarta/ant/ant.jar" />
42 * <lib version="v1_4_1" file="shared/jakarta/ant/optional.jar" />
43 * </checklib>
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();
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
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
79
80
81
82
83 public void execute() throws BuildException {
84 if (this.directory == null) {
85 this.directory = new File("lib");
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 }
116 }
117 tmpDir.delete();
118 }
119
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139