You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ca...@apache.org on 2007/02/09 22:36:37 UTC

svn commit: r505511 - in /maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin: BundlePlugin.java ManifestPlugin.java RecursiveBundlePlugin.java

Author: carlos
Date: Fri Feb  9 13:36:36 2007
New Revision: 505511

URL: http://svn.apache.org/viewvc?view=rev&rev=505511
Log:
Implement recursive bundle using manifest plugin

Modified:
    maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/BundlePlugin.java
    maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/ManifestPlugin.java
    maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/RecursiveBundlePlugin.java

Modified: maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/BundlePlugin.java
URL: http://svn.apache.org/viewvc/maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/BundlePlugin.java?view=diff&rev=505511&r1=505510&r2=505511
==============================================================================
--- maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/BundlePlugin.java (original)
+++ maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/BundlePlugin.java Fri Feb  9 13:36:36 2007
@@ -108,10 +108,6 @@
  }
 
  protected void execute(MavenProject project, Map instructions, Properties properties, Jar[] classpath) throws MojoExecutionException {
-  execute( project, instructions, properties, classpath, null );
- }
-
- protected void execute(MavenProject project, Map instructions, Properties properties, Jar[] classpath, Jar[] classpathForExpansion) throws MojoExecutionException {
   try {
    File jarFile = new File(getBuildDirectory(), getBundleName(project));
 
@@ -128,7 +124,6 @@
    builder.setBase(baseDir);
    builder.setProperties(properties);
    builder.setClasspath(classpath);
-   //builder.setClasspathForExpansion(classpathForExpansion);
  
    builder.build();
    Jar jar = builder.getJar();

Modified: maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/ManifestPlugin.java
URL: http://svn.apache.org/viewvc/maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/ManifestPlugin.java?view=diff&rev=505511&r1=505510&r2=505511
==============================================================================
--- maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/ManifestPlugin.java (original)
+++ maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/ManifestPlugin.java Fri Feb  9 13:36:36 2007
@@ -19,7 +19,6 @@
 package org.apache.felix.tools.maven2.bundleplugin;
 
 import java.io.File;
-import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.Map;
@@ -53,6 +52,37 @@
     protected void execute( MavenProject project, Map instructions, Properties properties, Jar[] classpath )
         throws MojoExecutionException
     {
+        Manifest manifest;
+        try
+        {
+            manifest = getManifest( project, properties, classpath );
+        }
+        catch ( IOException e )
+        {
+            throw new MojoExecutionException( "Error trying to generate Manifest", e );
+        }
+
+        File outputFile = new File( getBuildDirectory(), manifestLocation + "/MANIFEST.MF" );
+
+        try
+        {
+            writeManifest( manifest, outputFile );
+        }
+        catch ( IOException e )
+        {
+            throw new MojoExecutionException( "Error trying to write Manifest to file " + outputFile, e );
+        }
+    }
+
+    public Manifest getManifest( MavenProject project, Jar[] classpath )
+        throws IOException
+    {
+        return getManifest( project, null, classpath );
+    }
+
+    public Manifest getManifest( MavenProject project, Properties properties, Jar[] classpath )
+        throws IOException
+    {
         Analyzer analyzer = new Analyzer();
 
         analyzer.setProperties( getDefaultProperties( project ) );
@@ -62,51 +92,38 @@
         }
 
         Manifest manifest;
-        try
-        {
-            analyzer.setJar( outputDirectory );
+        analyzer.setJar( project.getFile() );
 
-            if ( analyzer.getProperty( Analyzer.IMPORT_PACKAGE ) == null )
-                analyzer.setProperty( Analyzer.IMPORT_PACKAGE, "*" );
+        if ( analyzer.getProperty( Analyzer.IMPORT_PACKAGE ) == null )
+            analyzer.setProperty( Analyzer.IMPORT_PACKAGE, "*" );
 
-            if ( analyzer.getProperty( Analyzer.EXPORT_PACKAGE ) == null )
-            {
-                String export = analyzer.calculateExportsFromContents();
-                analyzer.setProperty( Analyzer.EXPORT_PACKAGE, export );
-            }
+        if ( analyzer.getProperty( Analyzer.EXPORT_PACKAGE ) == null )
+        {
+            String export = analyzer.calculateExportsFromContents();
+            analyzer.setProperty( Analyzer.EXPORT_PACKAGE, export );
+        }
 
-            if ( classpath != null )
-                analyzer.setClasspath( classpath );
+        if ( classpath != null )
+            analyzer.setClasspath( classpath );
 
-            analyzer.mergeManifest();
+        analyzer.mergeManifest();
 
-            analyzer.calcManifest();
-            manifest = analyzer.getJar().getManifest();
-        }
-        catch ( IOException e )
-        {
-            throw new MojoExecutionException( "Error trying to generate Manifest", e );
-        }
+        analyzer.calcManifest();
+        manifest = analyzer.getJar().getManifest();
 
-        File manifestFile = new File( getBuildDirectory(), manifestLocation + "/MANIFEST.MF" );
-        manifestFile.getParentFile().mkdirs();
+        return manifest;
+    }
+
+    public void writeManifest( Manifest manifest, File outputFile )
+        throws IOException
+    {
+        outputFile.getParentFile().mkdirs();
 
         FileOutputStream os;
-        try
-        {
-            os = new FileOutputStream( manifestFile );
-        }
-        catch ( FileNotFoundException e )
-        {
-            throw new MojoExecutionException( "Error trying to open file " + manifestFile + " for writing", e );
-        }
+        os = new FileOutputStream( outputFile );
         try
         {
             manifest.write( os );
-        }
-        catch ( IOException e )
-        {
-            throw new MojoExecutionException( "Error writing Manifest to file " + manifestFile, e );
         }
         finally
         {

Modified: maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/RecursiveBundlePlugin.java
URL: http://svn.apache.org/viewvc/maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/RecursiveBundlePlugin.java?view=diff&rev=505511&r1=505510&r2=505511
==============================================================================
--- maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/RecursiveBundlePlugin.java (original)
+++ maven/sandbox/plugins/maven-bundle-plugin/src/main/java/org/apache/felix/tools/maven2/bundleplugin/RecursiveBundlePlugin.java Fri Feb  9 13:36:36 2007
@@ -19,13 +19,12 @@
 package org.apache.felix.tools.maven2.bundleplugin;
 
 import java.io.File;
-import java.io.IOException;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.Properties;
 import java.util.Set;
+import java.util.jar.Manifest;
 
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.factory.ArtifactFactory;
@@ -52,7 +51,7 @@
  * @description build an OSGi bundle jar for all transitive dependencies
  */
 public class RecursiveBundlePlugin
-    extends BundlePlugin
+    extends ManifestPlugin
 {
 
     /**
@@ -154,21 +153,23 @@
         {
             Map instructions = new HashMap();
             instructions.put( Analyzer.EXPORT_PACKAGE, "*" );
-            execute( project, instructions, artifact );
+
+            project.setFile( getFile( artifact ) );
+            Manifest manifest = getManifest( project, getClasspath( this.project ) );
+            Jar osgiJar = new Jar( project.getArtifactId(), project.getFile() );
+            osgiJar.setManifest( manifest );
+            File outputFile = getOutputFile( artifact );
+            outputFile.getParentFile().mkdirs();
+            osgiJar.write( outputFile );
         }
-        catch ( IOException e )
+        /* too bad Jar.write throws Exception */
+        catch ( Exception e )
         {
-            throw new MojoExecutionException( "Error calculating classpath for project " + project, e );
+            throw new MojoExecutionException( "Error generating OSGi bundle for project "
+                + getArtifactKey( project.getArtifact() ), e );
         }
     }
 
-    private void execute( MavenProject project, Map instructions, Artifact artifact )
-        throws MojoExecutionException, IOException
-    {
-        Properties properties = new Properties();
-        execute( project, instructions, properties, getClasspath( this.project ), new Jar[] { getJar( artifact ) } );
-    }
-
     private Map createMapOfArtifacts( MavenProject project )
     {
         Set artifacts = project.getArtifacts();
@@ -181,26 +182,6 @@
         return map;
     }
 
-    private Jar getJar( Artifact artifact )
-        throws IOException
-    {
-        Artifact a = (Artifact) mapOfArtifacts.get( getArtifactKey( artifact ) );
-
-        if ( ( a == null ) )
-        {
-            throw new RuntimeException( "The artifact " + artifact
-                + " from the dependency tree is not in the dependency set" );
-        }
-
-        String name = a.getArtifactId();
-        File jarFile = getFile( a );
-        if ( ( name == null ) || ( jarFile == null ) )
-        {
-            throw new RuntimeException( "The artifact " + artifact + " has no name or file couldn't be determined" );
-        }
-        return new Jar( name, jarFile );
-    }
-
     private String getArtifactKey( Artifact artifact )
     {
         return artifact.getGroupId() + ":" + artifact.getArtifactId();
@@ -225,26 +206,22 @@
     protected File getFile( Artifact artifact )
     {
         /* if bundle was already built use it instead of jar from repo */
-        File bundle = new File( getBuildDirectory(), getBundleName( artifact ) );
+        File bundle = getOutputFile( artifact );
         if ( bundle.exists() )
         {
             return bundle;
         }
-        return super.getFile( artifact );
+        File file = super.getFile( artifact );
+        if ( file == null )
+        {
+            Artifact a = (Artifact) mapOfArtifacts.get( getArtifactKey( artifact ) );
+            file = a.getFile();
+        }
+        return file;
     }
 
-    private String packagesAsString( Jar jar )
+    protected File getOutputFile( Artifact artifact )
     {
-        StringBuffer sb = new StringBuffer();
-        for ( Iterator it = getPackages( jar ).iterator(); it.hasNext(); )
-        {
-            String packageName = (String) it.next();
-            sb.append( packageName );
-            if ( it.hasNext() )
-            {
-                sb.append( "," );
-            }
-        }
-        return sb.toString();
+        return new File( getBuildDirectory(), getBundleName( artifact ) );
     }
 }