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.Ear;
28 import org.apache.tools.ant.taskdefs.Jar;
29 import org.apache.tools.ant.taskdefs.War;
30 import org.apache.tools.ant.types.EnumeratedAttribute;
31 import org.apache.tools.ant.types.FileSet;
32 import org.apache.tools.ant.types.ZipFileSet;
33
34 import java.io.File;
35 import java.util.Iterator;
36 import java.util.LinkedList;
37 import java.util.List;
38
39 /***
40 * Generates the distribution file(s) according to standard naming conventions.
41 * <PRE>
42 * <distgen appname="myapp" basedir="${build.dest.dir}" libdir="${lib.dir}"
43 * distdir="${dist.dir}" confdir="${conf.dir}" webdir="${web.src.dir}"
44 * jspdir="${jsp.src.dir}" type="war" versioned="true">
45 * <fileset />
46 * <metainf />
47 * </distgen></PRE>
48 * @author Keesjan van Bunningen - Finalist IT Group bv
49 * @version $Revision: 1.1 $, $Date: 2004/03/01 18:55:00 $
50 */
51 public class DistGenTask extends Task {
52
53 /*** Distribution type; war, ear or standalone */
54 private String type = "standalone";
55 /*** Application name */
56 private String appName;
57 /*** Embedded filesets */
58 private List filesets = new LinkedList();
59 /*** Embedded meta-inf */
60 private List metaInfs = new LinkedList();
61
62 private File baseDir, libDir, distDir, webDir, jspDir, confDir;
63 /*** Switch to include version number in distribution filename */
64
65 /*** For a simple property based on a string */
66 public void setAppname(String appName) {
67 this.appName = appName;
68 }
69
70
71 public void setBasedir(File baseDir) {
72
73 this.baseDir = baseDir;
74 }
75
76
77 public void setLibdir(File libDir) {
78
79 this.libDir = libDir;
80 }
81
82
83 public void setDistdir(File distDir) {
84
85 this.distDir = distDir;
86 }
87
88
89 public void setConfdir(File confDir) {
90
91 this.confDir = confDir;
92 }
93
94
95 public void setWebdir(File webDir) {
96
97 this.webDir = webDir;
98 }
99
100
101 public void setJspdir(File jspDir) {
102
103 this.jspDir = jspDir;
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128 public void addFileset(FileSet fs) {
129 filesets.add(fs);
130 }
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 /*** Enumerated attribute for project type; war, ear or standalone jar. */
176 public static class WarEarStandalone extends EnumeratedAttribute {
177 public String[] getValues() {
178 return new String[]{"war", "ear", "standalone"};
179 }
180 }
181
182
183 public void setType(WarEarStandalone type) {
184 this.type = type.getValue();
185 }
186
187
188 public void execute() throws BuildException {
189
190
191
192
193
194
195
196
197
198
199
200 if ("ear".equals(this.type)) {
201 Jar jar = (Jar) project.createTask("jar");
202 jar.setJarfile(new File(this.distDir, this.appName + "-ejb.jar"));
203 jar.setBasedir(this.baseDir);
204 jar.setIncludes("**/ejb/**,**/shared/**");
205 jar.setExcludes("*Test*.class");
206 jar.execute();
207 Jar interfaceJar = (Jar) project.createTask("jar");
208 interfaceJar.setJarfile(new File(this.distDir, this.appName + "-interfaces.jar"));
209 interfaceJar.setBasedir(this.baseDir);
210 interfaceJar.setIncludes("**/interfaces/**,**/value/**");
211 interfaceJar.setExcludes("*Test*.class");
212 interfaceJar.execute();
213 }
214
215
216
217
218
219 Jar clientJar = (Jar) project.createTask("jar");
220 String jarName = ("standalone".equals(this.type)) ? this.appName + ".jar" : this.appName + "-client.jar";
221 clientJar.setJarfile(new File(this.distDir, jarName));
222 clientJar.setBasedir(this.baseDir);
223 clientJar.setExcludes("*Test*.class,**/interfaces/**,**/ejb/**,**/value/**");
224 clientJar.execute();
225
226
227
228
229
230
231
232
233
234
235
236 if ("war".equals(this.type)) {
237
238 War war = (War) project.createTask("war");
239 war.setWarfile(new File(this.distDir, this.appName + ".war"));
240 war.setWebxml(new File(new File(this.confDir, "WEB-INF"), "web.xml"));
241
242 ZipFileSet clientZipFileSet = new ZipFileSet();
243 clientZipFileSet.setIncludesfile(new File(this.distDir, this.appName + "-client.jar"));
244
245 ZipFileSet interfaceZipFileSet = new ZipFileSet();
246 interfaceZipFileSet.setIncludesfile(new File(this.distDir, this.appName + "-interfaces.jar"));
247
248 ZipFileSet libZipFileSet = new ZipFileSet();
249 libZipFileSet.setDir(this.libDir);
250 libZipFileSet.setExcludesfile(new File(this.libDir, "j2ee.jar"));
251
252 log("Web dir: " + this.webDir);
253 log("Jsp dir: " + this.jspDir);
254
255 FileSet webFileSet = new FileSet();
256 webFileSet.setDir(this.webDir);
257 webFileSet.setIncludes("**/*");
258
259 FileSet jspFileSet = new FileSet();
260 jspFileSet.setDir(this.jspDir);
261 jspFileSet.setIncludes("**/*");
262
263 war.addLib(clientZipFileSet);
264 war.addLib(interfaceZipFileSet);
265 war.addLib(libZipFileSet);
266 war.addFileset(webFileSet);
267 war.addFileset(jspFileSet);
268 war.execute();
269 }
270
271
272
273
274
275
276
277
278
279
280
281 if ("ear".equals(this.type)) {
282 Ear ear = (Ear) project.createTask("ear");
283 ear.setEarfile(new File(this.distDir, this.appName + ".ear"));
284 ear.setAppxml(new File(new File(this.distDir, "META-INF"), "application.xml"));
285
286
287 Iterator fileIterator = filesets.iterator();
288 while (fileIterator.hasNext()) {
289 ZipFileSet zipFileSet = (ZipFileSet) fileIterator.next();
290 ear.addFileset(zipFileSet);
291 }
292
293
294 Iterator metaInfIterator = metaInfs.iterator();
295 while (metaInfIterator.hasNext()) {
296 ZipFileSet metaInf = (ZipFileSet) metaInfIterator.next();
297 ear.addMetainf(metaInf);
298 }
299 ear.execute();
300
301 }
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324 }
325
326 }