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.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   * &lt;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"&gt;
45   *       &lt;fileset /&gt;
46   *       &lt;metainf /&gt;
47   * &lt;/distgen&gt;</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        // Note: baseDir will automatically be absolute (resolved from project basedir).
73        this.baseDir = baseDir;
74     }
75  
76  
77     public void setLibdir(File libDir) {
78        // Note: libDir will automatically be absolute (resolved from project basedir).
79        this.libDir = libDir;
80     }
81  
82  
83     public void setDistdir(File distDir) {
84        // Note: distDir will automatically be absolute (resolved from project basedir).
85        this.distDir = distDir;
86     }
87  
88  
89     public void setConfdir(File confDir) {
90        // Note: confDir will automatically be absolute (resolved from project basedir).
91        this.confDir = confDir;
92     }
93  
94  
95     public void setWebdir(File webDir) {
96        // Note: webDir will automatically be absolute (resolved from project basedir).
97        this.webDir = webDir;
98     }
99  
100 
101    public void setJspdir(File jspDir) {
102       // Note: jspDir will automatically be absolute (resolved from project basedir).
103       this.jspDir = jspDir;
104    }
105 
106    /* Custom nested elements:
107     public static class Nestme {
108         String val; // accessible from execute()
109         public void setVal(String s) {
110             val = s;
111         }
112     }
113     private List nestmes = new LinkedList(); // List<Nestme>
114     public Nestme createNestme() {
115         Nestme n = new Nestme();
116         nestmes.add(n);
117         return n;
118     }
119     // Or:
120     public void addNestme(Nestme n) {
121         nestmes.add(n);
122     }
123     // <customtask>
124     //     <nestme val="something"/>
125     // </customtask>
126     */
127 
128    public void addFileset(FileSet fs) {
129       filesets.add(fs);
130    }
131 
132 
133    /* For nested text:
134     private StringBuffer text;
135     public void addText(String t) {
136         t = t.trim();
137         if (text == null) {
138             text = new StringBuffer(t);
139         } else {
140             text.append(t);
141         }
142     }
143     // <customtask>
144     //     Some text...
145     // </customtask>
146     */
147 
148    /* Some sort of path (like classpath or similar):
149     private Path path;
150     public void setPath(Path p) {
151         if (path == null) {
152             path = p;
153         } else {
154             path.append(p);
155         }
156     }
157     public Path createPath () {
158         if (path == null) {
159             path = new Path(project);
160         }
161         return path.createPath();
162     }
163     public void setPathRef(Reference r) {
164         createPath().setRefid(r);
165     }
166     // <customtask path="foo:bar"/>
167     // <customtask>
168     //     <path>
169     //         <pathelement location="foo"/>
170     //     </path>
171     // </customtask>
172     // Etc.
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 //     <target name="generate.ejb.jars.internal"
190 //             if="is.ear.project">
191 //       <jar jarfile="${dist.dir}/${application.name}-ejb.jar"
192 //           basedir="${build.dest.dir}"
193 //          includes="**/ejb/**,**/shared/**"
194 //          excludes="*Test*.class" />
195 //       <jar jarfile="${dist.dir}/${application.name}-interfaces.jar"
196 //           basedir="${build.dest.dir}"
197 //          includes="**/interfaces/**,**/value/**"
198 //          excludes="*Test*.class" />
199 //     </target>
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 //     <target name="generate.jars.internal" depends="generate.ejb.jars.internal">
215 //       <jar jarfile="${dist.dir}/${application.name}-client.jar"
216 //           basedir="${build.dest.dir}"
217 //          excludes="*Test*.class,**/interfaces/**,**/ejb/**,**/value/**" />
218 //     </target>
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 //     <target name="generate.war.internal" if="is.war.project">
227 //       <war warfile="${dist.dir}/${application.name}.war"
228 //           webxml="${conf.dir}/WEB-INF/web.xml" >
229 //        <lib file="${dist.dir}/${application.name}-client.jar"/>
230 //        <lib file="${dist.dir}/${application.name}-interfaces.jar"/>
231 //        <lib dir="${lib.dir}" excludes="j2ee.jar" />
232 //        <fileset dir="${web.src.dir}"/>
233 //        <fileset dir="${jsp.src.dir}"/>
234 //       </war>
235 //     </target>
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 //     <target name="generate.ear.internal" if="is.ear.project">
272 //       <ear earfile="${dist.dir}/${application.name}.ear"
273 //           appxml="${conf.dir}/META-INF/application.xml">
274 //        <!-- add libraries necessary for ejbs (do not put them in the appserver/lib) -->
275 //        <fileset />
276 //        <!-- add config files for ejb project (typically jboss.xml, etc.) -->
277 //        <metainf />
278 //      </ear>
279 //     </target>
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          // iterate through filesets
287          Iterator fileIterator = filesets.iterator();
288          while (fileIterator.hasNext()) {
289             ZipFileSet zipFileSet = (ZipFileSet) fileIterator.next();
290             ear.addFileset(zipFileSet);
291          }
292 
293          // iterate through meta-inf
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       // What the task actually does:
303       // WRITEME
304 
305       // To log something:
306       // log("Some message");
307       // log("Serious message", Project.MSG_WARN);
308       // log("Minor message", Project.MSG_VERBOSE);
309 
310       // To signal an error:
311       // throw new BuildException("Problem", location);
312       // throw new BuildException(someThrowable, location);
313       // throw new BuildException("Problem", someThrowable, location);
314 
315       // You can call other tasks too:
316       // Zip zip = (Zip)project.createTask("zip");
317       // zip.setZipfile(zipFile);
318       // FileSet fs = new FileSet();
319       // fs.setDir(baseDir);
320       // zip.addFileset(fs);
321       // zip.init();
322       // zip.setLocation(location);
323       // zip.execute();
324    }
325 
326 }