KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > tools > packageplugin > PackageAssemblyMojo


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: PackageAssemblyMojo.java 16:56:43 dutoo $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.tools.packageplugin;
23
24 import java.io.DataInputStream JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.jar.Attributes JavaDoc;
31 import java.util.jar.JarEntry JavaDoc;
32 import java.util.jar.JarOutputStream JavaDoc;
33 import java.util.jar.Manifest JavaDoc;
34
35 import org.apache.commons.io.FileUtils;
36 import org.apache.maven.artifact.repository.ArtifactRepository;
37 import org.apache.maven.artifact.resolver.ArtifactResolver;
38 import org.apache.maven.model.Dependency;
39 import org.apache.maven.plugin.AbstractMojo;
40 import org.apache.maven.plugin.MojoExecutionException;
41 import org.apache.maven.project.MavenProject;
42
43 import org.objectweb.petals.tools.packageplugin.utils.CopyFileSetUtil;
44
45 /**
46  * Assemble the Petals distribution
47  *
48  * @goal assembly
49  * @description Package the Petals distribution
50  * @aggregator
51  * @requiresDependencyResolution runtime
52  *
53  * @author ddesjardins - eBMWebsourcing
54  * @author Marc Dutoo - Open Wide
55  */

56 public class PackageAssemblyMojo extends AbstractMojo {
57
58     /**
59      * @component
60      */

61     protected ArtifactResolver artifactResolver;
62
63     /**
64      * Local maven repository.
65      *
66      * @parameter expression="${localRepository}"
67      * @required
68      * @readonly
69      */

70     protected ArtifactRepository localRepository;
71
72     /**
73      * The Maven Project.
74      *
75      * @parameter expression="${project}"
76      * @required
77      * @readonly
78      */

79     protected MavenProject project;
80
81     /**
82      * Base directory
83      *
84      * @parameter expression="${basedir}"
85      * @required
86      * @readonly
87      */

88     protected File JavaDoc baseDir;
89
90     /**
91      * Directory of the petals assembly
92      *
93      * @parameter
94      * @required
95      */

96     protected File JavaDoc petalsAssemblyDirectory;
97
98     
99     /**
100      * Execute the plugin
101      */

102     public void execute() throws MojoExecutionException {
103         try {
104             // Prepare the assembly directory
105
prepare();
106
107             // Copy the ressources
108
copyRessources();
109
110             // Copy the dependencies
111
copyLib();
112
113             // Jar the server
114
makeServerJar();
115  
116             // Make extras dir
117
fillExtrasDir();
118         } catch (IOException JavaDoc e) {
119             throw new MojoExecutionException(e.getLocalizedMessage(), e);
120         }
121     }
122
123     /**
124      * Copy petals-component-common lib and all its dependencies into extras dir
125      *
126      */

127     protected void fillExtrasDir() throws IOException JavaDoc {
128         // Component common project file
129
File JavaDoc componentCommonProjectDir = new File JavaDoc(baseDir + File.separator
130                 + ".." + File.separator + ".." + File.separator + "components"
131                 + File.separator + "common");
132
133         File JavaDoc extrasDir = new File JavaDoc(petalsAssemblyDirectory + File.separator
134                 + "extras");
135         File JavaDoc componentCommonExtraDir = new File JavaDoc(extrasDir, "component-common");
136
137         // Copy component common lib
138
CopyFileSetUtil.copyFileSetToDirectory(new File JavaDoc(
139                 componentCommonProjectDir, "target"), componentCommonExtraDir,
140                 "*.jar", ".svn", true);
141
142         // Copy component common dependencies
143
CopyFileSetUtil.copyFileSetToDirectory(new File JavaDoc(
144                 componentCommonProjectDir, "target" + File.separator
145                         + "dependencies"), componentCommonExtraDir, "*.jar",
146                 ".svn", true);
147
148     }
149
150     
151     /**
152      * Copy the libraries
153      *
154      * @throws IOException
155      */

156     protected void copyLib() throws IOException JavaDoc {
157         List JavaDoc dependencies = project.getDependencies();
158         for (Object JavaDoc object : dependencies) {
159             Dependency dependency = (Dependency) object;
160             if (dependency.getScope() == null
161                     || (dependency.getScope() != null && !dependency.getScope()
162                             .equals("test"))) {
163                 String JavaDoc jarName = dependency.getArtifactId() + "-"
164                         + dependency.getVersion() + ".jar";
165                 FileUtils.copyFileToDirectory(new File JavaDoc(localRepository
166                         .getBasedir()
167                         + File.separator
168                         + dependency.getGroupId().replace(".", File.separator)
169                         + File.separator
170                         + dependency.getArtifactId()
171                         + File.separator
172                         + dependency.getVersion()
173                         + File.separator + jarName), new File JavaDoc(
174                         petalsAssemblyDirectory + File.separator + "lib"));
175             }
176         }
177     }
178
179     /**
180      * Copy the ressources
181      *
182      * @throws IOException
183      *
184      */

185     protected void copyRessources() throws IOException JavaDoc {
186         // Copy the scripts to the bin dir
187
CopyFileSetUtil.copyFileSetToDirectory(new File JavaDoc(baseDir
188             + File.separator + "src" + File.separator + "scripts"), new File JavaDoc(
189             petalsAssemblyDirectory + File.separator + "bin"), "**"
190             + File.separator + "*.*", ".svn", true);
191         // Copy configuration files into /conf
192
CopyFileSetUtil.copyFileSetToDirectory(new File JavaDoc(baseDir
193                 + File.separator + "src" + File.separator + "conf"), new File JavaDoc(
194                 petalsAssemblyDirectory + File.separator + "conf"), "**"
195                 + File.separator + "*.*", ".svn", true);
196
197         // Copy fractal architecture definitions to the conf/adl
198
CopyFileSetUtil.copyFileSetToDirectory(new File JavaDoc(baseDir
199                 + File.separator + "src" + File.separator + "fractal"),
200                 new File JavaDoc(petalsAssemblyDirectory + File.separator + "conf"
201                         + File.separator + "adl"), "**" + File.separator
202                         + "*.*", ".svn", true);
203
204         // Copy XML Schemas files into /schemas directory
205
CopyFileSetUtil.copyFileSetToDirectory(new File JavaDoc(baseDir
206                 + File.separator + "src" + File.separator + "schema"),
207                 new File JavaDoc(petalsAssemblyDirectory + File.separator + "schema"),
208                 "**" + File.separator + "*.*", ".svn", true);
209
210         // Copy samples components files into /samples directory
211
CopyFileSetUtil.copyFileSetToDirectory(new File JavaDoc(baseDir
212                 + File.separator + ".." + File.separator + ".."
213                 + File.separator + "components"), new File JavaDoc(
214                 petalsAssemblyDirectory + File.separator + "components"), "**"
215                 + File.separator + "target" + File.separator + "*.zip", false);
216         // Add all txt files to the binary
217
CopyFileSetUtil.copyFileSetToDirectory(baseDir,
218                 petalsAssemblyDirectory, "*.txt", true);
219
220         // Copy Demos files from every demo directory's subfolders
221
File JavaDoc rootDemoDir = new File JavaDoc(baseDir + File.separator + ".."
222                 + File.separator + ".." + File.separator + "demos");
223         for (File JavaDoc demoDir : rootDemoDir.listFiles()) {
224             if (!demoDir.isDirectory() || demoDir.getName().startsWith(".")) {
225                 // filtering out .svn, pom.xml etc.
226
continue;
227             }
228             String JavaDoc demoAssemblyDirName = demoDir.getName();
229             CopyFileSetUtil.copyFileSetToDirectory(demoDir, new File JavaDoc(
230                     petalsAssemblyDirectory + File.separator + "demos"
231                             + File.separator + demoAssemblyDirName), "**"
232                     + File.separator + "target" + File.separator + "*.zip",
233                     false);
234             CopyFileSetUtil.copyFileSetToDirectory(demoDir, new File JavaDoc(
235                     petalsAssemblyDirectory + File.separator + "demos"
236                             + File.separator + demoAssemblyDirName), "**"
237                     + File.separator + "target" + File.separator + "*.war",
238                     false);
239         }
240
241         // Copy ant files
242
try {
243             File JavaDoc antProjectDir = new File JavaDoc(baseDir + File.separator + ".."
244                     + File.separator + ".." + File.separator + "tools"
245                     + File.separator + "ant");
246             File JavaDoc antDistribDir = new File JavaDoc(petalsAssemblyDirectory
247                     + File.separator + "ant");
248
249             CopyFileSetUtil.copyFileSetToDirectory(new File JavaDoc(antProjectDir,
250                     "target"), new File JavaDoc(antDistribDir, "lib"), "*.jar", true);
251             FileUtils.copyFileToDirectory(new File JavaDoc(antProjectDir, "src"
252                     + File.separator + "distribution" + File.separator
253                     + "README"), antDistribDir);
254             FileUtils.copyFileToDirectory(
255                     new File JavaDoc(antProjectDir, "src" + File.separator
256                             + "distribution" + File.separator + "TODO"),
257                     antDistribDir);
258             FileUtils.copyFileToDirectory(new File JavaDoc(antProjectDir, "src"
259                     + File.separator + "distribution" + File.separator
260                     + "ant-sample.xml"), antDistribDir);
261             FileUtils.copyFileToDirectory(new File JavaDoc(antProjectDir, "src"
262                     + File.separator + "distribution" + File.separator
263                     + "taskdef.xml"), antDistribDir);
264             CopyFileSetUtil.copyFileSetToDirectory(new File JavaDoc(antProjectDir,
265                     "target"), new File JavaDoc(antDistribDir, "lib"), "*.jar", true);
266
267             // Copy mortgage demo auto-install ant file
268
FileUtils.copyFileToDirectory(new File JavaDoc(baseDir + File.separator
269                     + ".." + File.separator + ".." + File.separator + "demos"
270                     + File.separator + "mortgage" + File.separator + "workflow"
271                     + File.separator + "auto-install.xml"), new File JavaDoc(
272                     petalsAssemblyDirectory + File.separator + "demos"
273                             + File.separator + "mortgage"));
274
275             // Copy mortgage demo auto-install ant file
276
FileUtils.copyFileToDirectory(new File JavaDoc(baseDir + File.separator
277                     + ".." + File.separator + ".." + File.separator + "demos"
278                     + File.separator + "mortgage" + File.separator + "workflow"
279                     + File.separator + "auto-install.xml"), new File JavaDoc(
280                     petalsAssemblyDirectory + File.separator + "demos"
281                             + File.separator + "mortgage"));
282
283         } catch (Exception JavaDoc e) {
284             // Do nothing
285
// The platform is assembled without the ant tool
286
}
287     }
288
289     /**
290      * Make the server jar
291      *
292      * @throws IOException
293      */

294     protected void makeServerJar() throws IOException JavaDoc {
295         Manifest JavaDoc manifest = new Manifest JavaDoc();
296         Attributes JavaDoc attributes = manifest.getMainAttributes();
297         attributes.putValue(Attributes.Name.MANIFEST_VERSION.toString(), "1.0");
298         attributes.putValue("Created-By", "Petals Assembly");
299         attributes.putValue("Built-By", System.getProperty("user.name"));
300         attributes.putValue("Package", "org.objectweb.petals");
301         attributes.putValue("Build-Jdk", System.getProperty("java.version"));
302
303         String JavaDoc classpath = "../conf/ ../conf/adl/ ../lib-ext/";
304         for (File JavaDoc file : new File JavaDoc(petalsAssemblyDirectory + File.separator
305                 + "lib").listFiles()) {
306             // classpath += " .." + File.separator + "common" + File.separator
307
// + file.getName();
308
classpath += " ../lib/" + file.getName();
309         }
310         attributes.putValue("Class-Path", classpath);
311
312         attributes.putValue("Extension-Name", "petals");
313         attributes.putValue("Specification-Title", "Petals Services Platform");
314         attributes.putValue("Specification-Vendor", "ObjectWeb Consortium");
315         attributes.putValue("Implementation-Title", "org.objectweb.petals");
316         attributes.putValue("Implementation-Vendor", "ObjectWeb Consortium");
317
318         String JavaDoc version = petalsAssemblyDirectory.getName();
319         if (version.indexOf("-") > -1) {
320             version = version.substring(version.indexOf("-") + 1);
321         }
322         attributes.putValue("Implementation-Version", version);
323         attributes.putValue("Main-Class",
324                 "org.objectweb.petals.system.launch.Launcher");
325
326         JarOutputStream JavaDoc jarOutputStream = new JarOutputStream JavaDoc(
327                 new FileOutputStream JavaDoc(petalsAssemblyDirectory + File.separator
328                         + "bin" + File.separator + "server.jar"), manifest);
329
330         // insert the license
331
JarEntry JavaDoc license = new JarEntry JavaDoc("META-INF" + File.separator
332                 + "LICENSE.txt");
333         jarOutputStream.putNextEntry(license);
334         DataInputStream JavaDoc dis = new DataInputStream JavaDoc(new FileInputStream JavaDoc(baseDir
335                 + File.separator + "LICENSE.txt"));
336         byte[] content = new byte[dis.available()];
337         dis.readFully(content);
338         jarOutputStream.write(content);
339         jarOutputStream.closeEntry();
340
341         jarOutputStream.flush();
342         jarOutputStream.close();
343     }
344
345     /**
346      * Prepare the assembly
347      *
348      * @throws IOException
349      *
350      */

351     protected void prepare() throws IOException JavaDoc {
352         FileUtils.deleteDirectory(petalsAssemblyDirectory);
353         petalsAssemblyDirectory.mkdirs();
354
355         // Log directory
356
new File JavaDoc(petalsAssemblyDirectory + File.separator + "logs").mkdirs();
357         // Bin directory
358
new File JavaDoc(petalsAssemblyDirectory + File.separator + "bin").mkdirs();
359         // Conf directory
360
new File JavaDoc(petalsAssemblyDirectory + File.separator + "conf").mkdirs();
361         // Fractal directory
362
new File JavaDoc(petalsAssemblyDirectory + File.separator + "conf"
363                 + File.separator + "adl").mkdirs();
364         // Schema directory
365
new File JavaDoc(petalsAssemblyDirectory + File.separator + "schema").mkdirs();
366         // Server lib directory
367
new File JavaDoc(petalsAssemblyDirectory + File.separator + "lib").mkdirs();
368        
369         // Samples directory
370
new File JavaDoc(petalsAssemblyDirectory + File.separator + "components")
371                 .mkdirs();
372
373         // Demos directory
374
new File JavaDoc(petalsAssemblyDirectory + File.separator + "demos").mkdirs();
375
376        
377         // Ant directory
378
new File JavaDoc(petalsAssemblyDirectory + File.separator + "ant").mkdirs();
379         new File JavaDoc(petalsAssemblyDirectory + File.separator + "ant"
380                 + File.separator + "lib").mkdirs();
381
382         // Joram directory
383
new File JavaDoc(petalsAssemblyDirectory + File.separator + "joram").mkdirs();
384
385         // Extras directories
386
new File JavaDoc(petalsAssemblyDirectory + File.separator + "extras"
387                 + File.separator + "component-common").mkdirs();
388
389         // Installs directories
390
new File JavaDoc(petalsAssemblyDirectory + File.separator + "install").mkdirs();
391         new File JavaDoc(petalsAssemblyDirectory + File.separator + "installed")
392                 .mkdirs();
393         new File JavaDoc(petalsAssemblyDirectory + File.separator + "uninstalled")
394                 .mkdirs();
395         new File JavaDoc(petalsAssemblyDirectory + File.separator + "work").mkdirs();
396         new File JavaDoc(petalsAssemblyDirectory + File.separator + "lost+found")
397                 .mkdirs();
398         String JavaDoc repositoryDirectory = petalsAssemblyDirectory + File.separator
399                 + "repository";
400         new File JavaDoc(repositoryDirectory).mkdirs();
401         new File JavaDoc(repositoryDirectory + File.separator + "components").mkdirs();
402         new File JavaDoc(repositoryDirectory + File.separator + "shared-libraries").mkdirs();
403         new File JavaDoc(repositoryDirectory + File.separator + "service-assemblies")
404                 .mkdirs();
405
406         
407     }
408
409 }
410
Popular Tags