KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > archiver > MavenArchiver


1 package org.apache.maven.archiver;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.artifact.DependencyResolutionRequiredException;
21 import org.apache.maven.project.MavenProject;
22 import org.codehaus.plexus.archiver.ArchiverException;
23 import org.codehaus.plexus.archiver.jar.JarArchiver;
24 import org.codehaus.plexus.archiver.jar.Manifest;
25 import org.codehaus.plexus.archiver.jar.ManifestException;
26
27 import java.io.File JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Properties JavaDoc;
35 import java.util.Set JavaDoc;
36
37 /**
38  * @author <a HREF="evenisse@apache.org">Emmanuel Venisse</a>
39  * @version $Revision: 355355 $ $Date: 2005-12-08 23:06:31 -0500 (Thu, 08 Dec 2005) $
40  */

41 public class MavenArchiver
42 {
43     private JarArchiver archiver;
44
45     private File JavaDoc archiveFile;
46     
47     /**
48      * Return a pre-configured manifest
49      *
50      * @todo Add user attributes list and user groups list
51      */

52     public Manifest getManifest( MavenProject project, ManifestConfiguration config )
53         throws ManifestException, DependencyResolutionRequiredException
54     {
55         // Added basic entries
56
Manifest m = new Manifest();
57         Manifest.Attribute buildAttr = new Manifest.Attribute( "Built-By", System.getProperty( "user.name" ) );
58         m.addConfiguredAttribute( buildAttr );
59         Manifest.Attribute createdAttr = new Manifest.Attribute( "Created-By", "Apache Maven" );
60         m.addConfiguredAttribute( createdAttr );
61
62 /* TODO: rethink this, it wasn't working
63         Artifact projectArtifact = project.getArtifact();
64
65         if ( projectArtifact.isSnapshot() )
66         {
67             Manifest.Attribute buildNumberAttr = new Manifest.Attribute( "Build-Number", "" +
68                 project.getSnapshotDeploymentBuildNumber() );
69             m.addConfiguredAttribute( buildNumberAttr );
70         }
71
72 */

73         if ( config.getPackageName() != null )
74         {
75             Manifest.Attribute packageAttr = new Manifest.Attribute( "Package", config.getPackageName() );
76             m.addConfiguredAttribute( packageAttr );
77         }
78
79         Manifest.Attribute buildJdkAttr = new Manifest.Attribute( "Build-Jdk", System.getProperty( "java.version" ) );
80         m.addConfiguredAttribute( buildJdkAttr );
81
82         if ( config.isAddClasspath() )
83         {
84             StringBuffer JavaDoc classpath = new StringBuffer JavaDoc();
85             List JavaDoc artifacts = project.getRuntimeClasspathElements();
86             String JavaDoc classpathPrefix = config.getClasspathPrefix();
87
88             for ( Iterator JavaDoc iter = artifacts.iterator(); iter.hasNext(); )
89             {
90                 File JavaDoc f = new File JavaDoc( (String JavaDoc) iter.next() );
91                 if ( f.isFile() )
92                 {
93                     if ( classpath.length() > 0 )
94                     {
95                         classpath.append( " " );
96                     }
97
98                     classpath.append( classpathPrefix );
99                     classpath.append( f.getName() );
100                 }
101             }
102
103             if ( classpath.length() > 0 )
104             {
105                 Manifest.Attribute classpathAttr = new Manifest.Attribute( "Class-Path", classpath.toString() );
106                 m.addConfiguredAttribute( classpathAttr );
107             }
108         }
109
110         // Added supplementary entries
111
Manifest.Attribute extensionNameAttr = new Manifest.Attribute( "Extension-Name", project.getArtifactId() );
112         m.addConfiguredAttribute( extensionNameAttr );
113
114         if ( project.getDescription() != null )
115         {
116             Manifest.Attribute specificationTitleAttr = new Manifest.Attribute( "Specification-Title",
117                                                                                 project.getDescription() );
118             m.addConfiguredAttribute( specificationTitleAttr );
119         }
120
121         if ( project.getOrganization() != null )
122         {
123             Manifest.Attribute specificationVendor = new Manifest.Attribute( "Specification-Vendor",
124                                                                              project.getOrganization().getName() );
125             m.addConfiguredAttribute( specificationVendor );
126             Manifest.Attribute implementationVendorAttr = new Manifest.Attribute( "Implementation-Vendor",
127                                                                                   project.getOrganization().getName() );
128             m.addConfiguredAttribute( implementationVendorAttr );
129         }
130
131         Manifest.Attribute implementationTitleAttr = new Manifest.Attribute( "Implementation-Title",
132                                                                              project.getArtifactId() );
133         m.addConfiguredAttribute( implementationTitleAttr );
134         Manifest.Attribute implementationVersionAttr = new Manifest.Attribute( "Implementation-Version",
135                                                                                project.getVersion() );
136         m.addConfiguredAttribute( implementationVersionAttr );
137
138         String JavaDoc mainClass = config.getMainClass();
139         if ( mainClass != null && !"".equals( mainClass ) )
140         {
141             Manifest.Attribute mainClassAttr = new Manifest.Attribute( "Main-Class", mainClass );
142             m.addConfiguredAttribute( mainClassAttr );
143         }
144
145         // Added extensions
146
if ( config.isAddExtensions() )
147         {
148             StringBuffer JavaDoc extensionsList = new StringBuffer JavaDoc();
149             Set JavaDoc artifacts = project.getArtifacts();
150
151             for ( Iterator JavaDoc iter = artifacts.iterator(); iter.hasNext(); )
152             {
153                 Artifact artifact = (Artifact) iter.next();
154                 // TODO: type of ejb should be added too?
155
if ( "jar".equals( artifact.getType() ) )
156                 {
157                     if ( extensionsList.length() > 0 )
158                     {
159                         extensionsList.append( " " );
160                     }
161                     extensionsList.append( artifact.getArtifactId() );
162                 }
163             }
164
165             if ( extensionsList.length() > 0 )
166             {
167                 Manifest.Attribute extensionsListAttr = new Manifest.Attribute( "Extension-List",
168                                                                                 extensionsList.toString() );
169                 m.addConfiguredAttribute( extensionsListAttr );
170             }
171
172             for ( Iterator JavaDoc iter = artifacts.iterator(); iter.hasNext(); )
173             {
174                 Artifact artifact = (Artifact) iter.next();
175                 if ( "jar".equals( artifact.getType() ) )
176                 {
177                     Manifest.Attribute archExtNameAttr = new Manifest.Attribute(
178                         artifact.getArtifactId() + "-Extension-Name", artifact.getArtifactId() );
179                     m.addConfiguredAttribute( archExtNameAttr );
180                     String JavaDoc name = artifact.getArtifactId() + "-Implementation-Version";
181                     Manifest.Attribute archImplVersionAttr = new Manifest.Attribute( name, artifact.getVersion() );
182                     m.addConfiguredAttribute( archImplVersionAttr );
183
184                     if ( artifact.getRepository() != null )
185                     {
186                         // TODO: is this correct
187
name = artifact.getArtifactId() + "-Implementation-URL";
188                         String JavaDoc url = artifact.getRepository().getUrl() + "/" + artifact.toString();
189                         Manifest.Attribute archImplUrlAttr = new Manifest.Attribute( name, url );
190                         m.addConfiguredAttribute( archImplUrlAttr );
191                     }
192                 }
193             }
194         }
195
196         return m;
197     }
198
199     public JarArchiver getArchiver()
200     {
201         return archiver;
202     }
203
204     public void setArchiver( JarArchiver archiver )
205     {
206         this.archiver = archiver;
207     }
208
209     public void setOutputFile( File JavaDoc outputFile )
210     {
211         archiveFile = outputFile;
212     }
213
214     public void createArchive( MavenProject project, MavenArchiveConfiguration archiveConfiguration )
215         throws ArchiverException, ManifestException, IOException JavaDoc, DependencyResolutionRequiredException
216     {
217         // we have to clone the project instance so we can write out the pom with the deployment version,
218
// without impacting the main project instance...
219
MavenProject workingProject = new MavenProject( project );
220
221         File JavaDoc pomPropertiesFile = new File JavaDoc( workingProject.getFile().getParentFile(), "pom.properties" );
222
223         if ( archiveConfiguration.isAddMavenDescriptor() )
224         {
225             // ----------------------------------------------------------------------
226
// We want to add the metadata for the project to the JAR in two forms:
227
//
228
// The first form is that of the POM itself. Applications that wish to
229
// access the POM for an artifact using maven tools they can.
230
//
231
// The second form is that of a properties file containing the basic
232
// top-level POM elements so that applications that wish to access
233
// POM information without the use of maven tools can do so.
234
// ----------------------------------------------------------------------
235

236             if ( workingProject.getArtifact().isSnapshot() )
237             {
238                 workingProject.setVersion( workingProject.getArtifact().getVersion() );
239             }
240
241             String JavaDoc groupId = workingProject.getGroupId();
242
243             String JavaDoc artifactId = workingProject.getArtifactId();
244
245             archiver.addFile( project.getFile(), "META-INF/maven/" + groupId + "/" + artifactId + "/pom.xml" );
246             
247             
248             // ----------------------------------------------------------------------
249
// Create pom.properties file
250
// ----------------------------------------------------------------------
251

252             Properties JavaDoc p = new Properties JavaDoc();
253
254             p.setProperty( "groupId", workingProject.getGroupId() );
255
256             p.setProperty( "artifactId", workingProject.getArtifactId() );
257
258             p.setProperty( "version", workingProject.getVersion() );
259
260             OutputStream JavaDoc os = new FileOutputStream JavaDoc( pomPropertiesFile );
261
262             p.store( os, "Generated by Maven" );
263
264             os.close(); // stream is flushed but not closed by Properties.store()
265

266             archiver.addFile( pomPropertiesFile, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" );
267         }
268
269         // ----------------------------------------------------------------------
270
// Create the manifest
271
// ----------------------------------------------------------------------
272

273         File JavaDoc manifestFile = archiveConfiguration.getManifestFile();
274
275         if ( manifestFile != null )
276         {
277             archiver.setManifest( manifestFile );
278         }
279
280         Manifest manifest = getManifest( workingProject, archiveConfiguration.getManifest() );
281
282         // any custom manifest entries in the archive configuration manifest?
283
if ( !archiveConfiguration.isManifestEntriesEmpty() )
284         {
285             Map JavaDoc entries = archiveConfiguration.getManifestEntries();
286             Set JavaDoc keys = entries.keySet();
287             for ( Iterator JavaDoc iter = keys.iterator(); iter.hasNext(); )
288             {
289                 String JavaDoc key = (String JavaDoc) iter.next();
290                 String JavaDoc value = (String JavaDoc) entries.get( key );
291                 Manifest.Attribute attr = new Manifest.Attribute( key, value );
292                 manifest.addConfiguredAttribute( attr );
293             }
294         }
295
296         // any custom manifest sections in the archive configuration manifest?
297
if ( !archiveConfiguration.isManifestSectionsEmpty() )
298         {
299             List JavaDoc sections = archiveConfiguration.getManifestSections();
300             for ( Iterator JavaDoc iter = sections.iterator(); iter.hasNext(); )
301             {
302                 ManifestSection section = (ManifestSection) iter.next();
303                 Manifest.Section theSection = new Manifest.Section();
304                 theSection.setName( section.getName() );
305                 
306                 if( !section.isManifestEntriesEmpty() ) {
307                     Map JavaDoc entries = section.getManifestEntries();
308                     Set JavaDoc keys = entries.keySet();
309                     for ( Iterator JavaDoc it = keys.iterator(); it.hasNext(); )
310                     {
311                         String JavaDoc key = (String JavaDoc) it.next();
312                         String JavaDoc value = (String JavaDoc) entries.get( key );
313                         Manifest.Attribute attr = new Manifest.Attribute( key, value );
314                         theSection.addConfiguredAttribute( attr );
315                     }
316                 }
317                 
318                 manifest.addConfiguredSection( theSection );
319             }
320         }
321         
322         // Configure the jar
323
archiver.addConfiguredManifest( manifest );
324
325         archiver.setCompress( archiveConfiguration.isCompress() );
326
327         archiver.setIndex( archiveConfiguration.isIndex() );
328
329         archiver.setDestFile( archiveFile );
330
331         // create archive
332
archiver.createArchive();
333
334         // Cleanup
335
if ( archiveConfiguration.isAddMavenDescriptor() )
336         {
337             pomPropertiesFile.delete();
338         }
339     }
340 }
341
Popular Tags