FELIX-442: Add manifest entries from maven-jar-plugin configuration to final bundle

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@607025 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/bundleplugin/pom.xml b/bundleplugin/pom.xml
index 8bd01c4..c24a7a9 100644
--- a/bundleplugin/pom.xml
+++ b/bundleplugin/pom.xml
@@ -74,6 +74,11 @@
    <version>2.0.7</version>
   </dependency>
   <dependency>
+   <groupId>org.apache.maven</groupId>
+   <artifactId>maven-archiver</artifactId>
+   <version>2.2</version>
+  </dependency>
+  <dependency>
    <groupId>org.apache.maven.shared</groupId>
    <artifactId>maven-dependency-tree</artifactId>
    <version>1.1</version>
diff --git a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java
index 57db3f5..b187c40 100644
--- a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java
+++ b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java
@@ -34,6 +34,8 @@
 import java.util.jar.Manifest;
 import java.util.zip.ZipException;
 
+import org.apache.maven.archiver.MavenArchiveConfiguration;
+import org.apache.maven.archiver.MavenArchiver;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
 import org.apache.maven.model.License;
@@ -46,6 +48,7 @@
 import org.codehaus.plexus.archiver.UnArchiver;
 import org.codehaus.plexus.archiver.manager.ArchiverManager;
 import org.codehaus.plexus.util.DirectoryScanner;
+import org.codehaus.plexus.util.StringInputStream;
 
 import aQute.lib.osgi.Analyzer;
 import aQute.lib.osgi.Builder;
@@ -317,6 +320,32 @@
                 }
             }
 
+            try
+            {
+                /*
+                 * Grab customized manifest entries from the maven-jar-plugin configuration
+                 */
+                MavenArchiveConfiguration archiveConfig = JarPluginConfiguration.getArchiveConfiguration( project );
+                String mavenManifestText = new MavenArchiver().getManifest( project, archiveConfig ).toString();
+
+                Manifest mavenManifest = new Manifest();
+                mavenManifest.read( new StringInputStream( mavenManifestText ) );
+                Manifest bundleManifest = jar.getManifest();
+
+                /*
+                 * Overlay customized Maven manifest with the generated bundle manifest
+                 */
+                mavenManifest.getMainAttributes().putAll( bundleManifest.getMainAttributes() );
+                mavenManifest.getMainAttributes().putValue( "Created-By", "Apache Maven Bundle Plugin" );
+                mavenManifest.getEntries().putAll( bundleManifest.getEntries() );
+
+                jar.setManifest( mavenManifest );
+            }
+            catch (Exception e)
+            {
+                getLog().warn( "Unable to merge Maven manifest" );
+            }
+            
             jarFile.getParentFile().mkdirs();
             builder.getJar().write(jarFile);
             Artifact bundleArtifact = project.getArtifact();
diff --git a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/JarPluginConfiguration.java b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/JarPluginConfiguration.java
new file mode 100644
index 0000000..f0ab2bb
--- /dev/null
+++ b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/JarPluginConfiguration.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.bundleplugin;
+
+import org.apache.maven.archiver.MavenArchiveConfiguration;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.component.configurator.converters.composite.ObjectWithFieldsConverter;
+import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
+import org.codehaus.plexus.component.configurator.converters.lookup.DefaultConverterLookup;
+import org.codehaus.plexus.component.configurator.expression.DefaultExpressionEvaluator;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
+import org.codehaus.plexus.util.xml.Xpp3Dom;
+
+/**
+ * Provide access to the archive configuration from the jar plugin
+ * 
+ * @author stuart.mcculloch@jayway.net (Stuart McCulloch)
+ */
+public class JarPluginConfiguration
+{
+    public static MavenArchiveConfiguration getArchiveConfiguration( MavenProject project )
+    {
+        MavenArchiveConfiguration archiveConfig = new MavenArchiveConfiguration();
+
+        try
+        {
+            ObjectWithFieldsConverter converter = new ObjectWithFieldsConverter();
+            ClassLoader loader = JarPluginConfiguration.class.getClassLoader();
+            ExpressionEvaluator evaluator = new DefaultExpressionEvaluator();
+            ConverterLookup converters = new DefaultConverterLookup();
+
+            PlexusConfiguration pluginSettings = getCorePluginConfiguration( project, "jar" );
+            PlexusConfiguration archiveSettings = pluginSettings.getChild( "archive" );
+
+            converter.processConfiguration( converters, archiveConfig, loader, archiveSettings, evaluator, null );
+        }
+        catch( Exception e )
+        {
+            // ignore and return empty configuration...
+        }
+
+        return archiveConfig;
+    }
+
+    private static PlexusConfiguration getCorePluginConfiguration( MavenProject project, String pluginName )
+    {
+        return getPluginConfiguration( project, "org.apache.maven.plugins", "maven-" + pluginName + "-plugin" );
+    }
+
+    private static PlexusConfiguration getPluginConfiguration( MavenProject project, String groupId, String artifactId )
+    {
+        Xpp3Dom pluginConfig = project.getGoalConfiguration( groupId, artifactId, null, null );
+
+        return new XmlPlexusConfiguration( pluginConfig );
+    }
+}