1 package org.apache.maven.archiver; 2 3 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 ; 28 import java.io.FileOutputStream ; 29 import java.io.IOException ; 30 import java.io.OutputStream ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.Properties ; 35 import java.util.Set ; 36 37 41 public class MavenArchiver 42 { 43 private JarArchiver archiver; 44 45 private File archiveFile; 46 47 52 public Manifest getManifest( MavenProject project, ManifestConfiguration config ) 53 throws ManifestException, DependencyResolutionRequiredException 54 { 55 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 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 classpath = new StringBuffer (); 85 List artifacts = project.getRuntimeClasspathElements(); 86 String classpathPrefix = config.getClasspathPrefix(); 87 88 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) 89 { 90 File f = new File ( (String ) 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 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 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 if ( config.isAddExtensions() ) 147 { 148 StringBuffer extensionsList = new StringBuffer (); 149 Set artifacts = project.getArtifacts(); 150 151 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) 152 { 153 Artifact artifact = (Artifact) iter.next(); 154 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 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 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 name = artifact.getArtifactId() + "-Implementation-URL"; 188 String 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 outputFile ) 210 { 211 archiveFile = outputFile; 212 } 213 214 public void createArchive( MavenProject project, MavenArchiveConfiguration archiveConfiguration ) 215 throws ArchiverException, ManifestException, IOException , DependencyResolutionRequiredException 216 { 217 MavenProject workingProject = new MavenProject( project ); 220 221 File pomPropertiesFile = new File ( workingProject.getFile().getParentFile(), "pom.properties" ); 222 223 if ( archiveConfiguration.isAddMavenDescriptor() ) 224 { 225 236 if ( workingProject.getArtifact().isSnapshot() ) 237 { 238 workingProject.setVersion( workingProject.getArtifact().getVersion() ); 239 } 240 241 String groupId = workingProject.getGroupId(); 242 243 String artifactId = workingProject.getArtifactId(); 244 245 archiver.addFile( project.getFile(), "META-INF/maven/" + groupId + "/" + artifactId + "/pom.xml" ); 246 247 248 252 Properties p = new Properties (); 253 254 p.setProperty( "groupId", workingProject.getGroupId() ); 255 256 p.setProperty( "artifactId", workingProject.getArtifactId() ); 257 258 p.setProperty( "version", workingProject.getVersion() ); 259 260 OutputStream os = new FileOutputStream ( pomPropertiesFile ); 261 262 p.store( os, "Generated by Maven" ); 263 264 os.close(); 266 archiver.addFile( pomPropertiesFile, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" ); 267 } 268 269 273 File manifestFile = archiveConfiguration.getManifestFile(); 274 275 if ( manifestFile != null ) 276 { 277 archiver.setManifest( manifestFile ); 278 } 279 280 Manifest manifest = getManifest( workingProject, archiveConfiguration.getManifest() ); 281 282 if ( !archiveConfiguration.isManifestEntriesEmpty() ) 284 { 285 Map entries = archiveConfiguration.getManifestEntries(); 286 Set keys = entries.keySet(); 287 for ( Iterator iter = keys.iterator(); iter.hasNext(); ) 288 { 289 String key = (String ) iter.next(); 290 String value = (String ) entries.get( key ); 291 Manifest.Attribute attr = new Manifest.Attribute( key, value ); 292 manifest.addConfiguredAttribute( attr ); 293 } 294 } 295 296 if ( !archiveConfiguration.isManifestSectionsEmpty() ) 298 { 299 List sections = archiveConfiguration.getManifestSections(); 300 for ( Iterator 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 entries = section.getManifestEntries(); 308 Set keys = entries.keySet(); 309 for ( Iterator it = keys.iterator(); it.hasNext(); ) 310 { 311 String key = (String ) it.next(); 312 String value = (String ) 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 archiver.addConfiguredManifest( manifest ); 324 325 archiver.setCompress( archiveConfiguration.isCompress() ); 326 327 archiver.setIndex( archiveConfiguration.isIndex() ); 328 329 archiver.setDestFile( archiveFile ); 330 331 archiver.createArchive(); 333 334 if ( archiveConfiguration.isAddMavenDescriptor() ) 336 { 337 pomPropertiesFile.delete(); 338 } 339 } 340 } 341 | Popular Tags |