You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by rf...@apache.org on 2009/05/27 03:22:09 UTC

svn commit: r778954 - in /tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin: ArtifactManifest.java BundleUtil.java ModuleBundlesBuildMojo.java

Author: rfeng
Date: Wed May 27 01:22:08 2009
New Revision: 778954

URL: http://svn.apache.org/viewvc?rev=778954&view=rev
Log:
Add capability to override MANIFEST.MF 

Added:
    tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/ArtifactManifest.java
Modified:
    tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/BundleUtil.java
    tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/ModuleBundlesBuildMojo.java

Added: tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/ArtifactManifest.java
URL: http://svn.apache.org/viewvc/tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/ArtifactManifest.java?rev=778954&view=auto
==============================================================================
--- tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/ArtifactManifest.java (added)
+++ tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/ArtifactManifest.java Wed May 27 01:22:08 2009
@@ -0,0 +1,37 @@
+/*
+ * 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.tuscany.maven.bundle.plugin;
+
+import java.io.File;
+
+/**
+ *
+ */
+public class ArtifactManifest extends ArtifactMember {
+    private File manifestFile;
+
+    public File getManifestFile() {
+        return manifestFile;
+    }
+
+    public void setManifestFile(File manifestFile) {
+        this.manifestFile = manifestFile;
+    }
+}

Modified: tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/BundleUtil.java
URL: http://svn.apache.org/viewvc/tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/BundleUtil.java?rev=778954&r1=778953&r2=778954&view=diff
==============================================================================
--- tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/BundleUtil.java (original)
+++ tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/BundleUtil.java Wed May 27 01:22:08 2009
@@ -6,15 +6,15 @@
  * 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.    
+ * under the License.
  */
 
 package org.apache.tuscany.maven.bundle.plugin;
@@ -35,6 +35,7 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.HashSet;
+import java.util.Properties;
 import java.util.Set;
 import java.util.jar.Attributes;
 import java.util.jar.JarFile;
@@ -55,7 +56,7 @@
     private final static Logger logger = Logger.getLogger(BundleUtil.class.getName());
     /**
      * Returns the name of a bundle, or null if the given file is not a bundle.
-     *  
+     *
      * @param file
      * @return
      * @throws IOException
@@ -80,7 +81,7 @@
         }
         return bundleName;
     }
-    
+
     static Manifest getManifest(File file) throws IOException {
         if (!file.exists()) {
             return null;
@@ -103,32 +104,33 @@
 
     /**
      * Generate a Bundle manifest for a set of JAR files.
-     * 
+     *
      * @param jarFiles
      * @param name
      * @param symbolicName
      * @param version
-     * @param dir 
+     * @param dir
      * @return
      * @throws IllegalStateException
      */
     static Manifest libraryManifest(Set<File> jarFiles, String name, String symbolicName, String version, String dir)
     throws IllegalStateException {
-    	return libraryManifest(jarFiles, name, symbolicName, version, dir, null);
+    	return libraryManifest(jarFiles, name, symbolicName, version, dir, null, null);
     }
     /**
      * Generate a Bundle manifest for a set of JAR files.
-     * 
+     *
      * @param jarFiles
      * @param name
      * @param symbolicName
      * @param version
-     * @param dir 
-     * @param buddyPolicy 
+     * @param dir
+     * @param buddyPolicy
+     * @param env
      * @return
      * @throws IllegalStateException
      */
-    static Manifest libraryManifest(Set<File> jarFiles, String name, String symbolicName, String version, String dir, String buddyPolicy)
+    static Manifest libraryManifest(Set<File> jarFiles, String name, String symbolicName, String version, String dir, String buddyPolicy, String env)
         throws IllegalStateException {
         try {
 
@@ -143,11 +145,16 @@
                 addPackages(jarFile, exportedPackages, version);
                 if (dir != null) {
                     classpath.append(dir).append("/");
-                } 
+                }
                 classpath.append(jarFile.getName());
                 classpath.append(",");
             }
 
+            if (env == null) {
+                env = "JavaSE-1.6";
+            }
+            exportedPackages.removeAll(getSystemPackages(env));
+
             // Generate export-package and import-package declarations
             StringBuffer exports = new StringBuffer();
             StringBuffer imports = new StringBuffer();
@@ -165,7 +172,7 @@
                     logger.warning("Duplicate package skipped: " + export);
                 }
             }
- 
+
             // Create a manifest
             Manifest manifest = new Manifest();
             Attributes attributes = manifest.getMainAttributes();
@@ -196,7 +203,7 @@
 
     /**
      * Write a bundle manifest.
-     * 
+     *
      * @param manifest
      * @param out
      * @throws IOException
@@ -219,7 +226,7 @@
 
     /**
      * Add packages to be exported out of a JAR file.
-     * 
+     *
      * @param jarFile
      * @param packages
      * @throws IOException
@@ -235,7 +242,7 @@
 
     /**
      * Write manifest attributes.
-     * 
+     *
      * @param attributes
      * @param key
      * @param dos
@@ -264,7 +271,7 @@
 
     /**
      * Strip an OSGi export, only retain the package name and version.
-     * 
+     *
      * @param export
      * @return
      */
@@ -289,7 +296,7 @@
 
     /**
      * Add all the packages out of a JAR.
-     * 
+     *
      * @param jarFile
      * @param packages
      * @param version
@@ -315,6 +322,22 @@
         is.close();
     }
 
+    private static Set<String> getSystemPackages(String env) throws IOException {
+        Set<String> sysPackages = new HashSet<String>();
+        InputStream is = BundleUtil.class.getResourceAsStream("/" + env + ".profile");
+        if (is != null) {
+            Properties props = new Properties();
+            props.load(is);
+            String pkgs = (String)props.get("org.osgi.framework.system.packages");
+            if (pkgs != null) {
+                for (String p : pkgs.split(",")) {
+                    sysPackages.add(p.trim());
+                }
+            }
+        }
+        return sysPackages;
+    }
+
     /**
      * Returns the name of the exported package in the given export.
      * @param export
@@ -330,7 +353,7 @@
 
     /**
      * Add the packages exported by a bundle.
-     *  
+     *
      * @param file
      * @param packages
      * @return
@@ -397,7 +420,7 @@
     }
 
     /**
-     * Convert the maven version into OSGi version 
+     * Convert the maven version into OSGi version
      * @param mavenVersion
      * @return
      */

Modified: tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/ModuleBundlesBuildMojo.java
URL: http://svn.apache.org/viewvc/tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/ModuleBundlesBuildMojo.java?rev=778954&r1=778953&r2=778954&view=diff
==============================================================================
--- tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/ModuleBundlesBuildMojo.java (original)
+++ tuscany/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/ModuleBundlesBuildMojo.java Wed May 27 01:22:08 2009
@@ -239,6 +239,11 @@
     private ArtifactAggregation[] artifactAggregations;
 
     /**
+     * @parameter
+     */
+    private ArtifactManifest[] artifactManifests;
+
+    /**
      * Inserts a generic Eclipse-BuddyPolicy header into generated artifacts manifests
      * @parameter
      */
@@ -305,6 +310,25 @@
         }
     }
 
+    private Manifest findManifest(Artifact artifact) throws IOException {
+        if (artifactManifests == null) {
+            return null;
+        }
+        for (ArtifactManifest m : artifactManifests) {
+            if (m.matches(artifact)) {
+                File mf = m.getManifestFile();
+                if (mf != null) {
+                    FileInputStream is = new FileInputStream(mf);
+                    Manifest manifest = new Manifest(is);
+                    is.close();
+                    getLog().info("MANIFEST.MF found for " + artifact + " (" + mf + ")");
+                    return manifest;
+                }
+            }
+        }
+        return null;
+    }
+
     public void execute() throws MojoExecutionException {
         Log log = getLog();
 
@@ -403,6 +427,8 @@
                     log.debug("Processing artifact: " + artifact);
                 }
 
+                Manifest customizedMF = findManifest(artifact);
+
                 // Get the bundle name if the artifact is an OSGi bundle
                 Manifest mf = null;
                 String bundleName = null;
@@ -413,7 +439,7 @@
                     throw new MojoExecutionException(e.getMessage(), e);
                 }
 
-                if (bundleName != null) {
+                if (bundleName != null && customizedMF == null) {
 
                     // Exclude artifact if its file name is excluded
                     if (excludedFileNames.contains(artifactFile.getName())) {
@@ -432,16 +458,9 @@
                         jarNames.add(artifact, artifactFile.getName());
                     } else {
                         // Expanding the bundle into a folder
-                        
-                        // Add the Bundle-ClassPath
-                        String cp = mf.getMainAttributes().getValue(BUNDLE_CLASSPATH);
-                        if (cp == null) {
-                            cp = artifactFile.getName();
-                        } else {
-                            cp = cp + "," + artifactFile.getName();
-                        }
-                        mf.getMainAttributes().putValue(BUNDLE_CLASSPATH, cp);
-                        
+
+                        setBundleClassPath(mf, artifactFile);
+
                         int index = artifactFile.getName().lastIndexOf('.');
                         String dirName = artifactFile.getName().substring(0, index);
                         File dir = new File(root, dirName);
@@ -499,18 +518,30 @@
 
                     // Create a bundle directory for a non-OSGi JAR
                     log.info("Adding JAR artifact: " + artifact);
-                    String version = BundleUtil.osgiVersion(artifact.getVersion());
 
-                    Set<File> jarFiles = new HashSet<File>();
-                    jarFiles.add(artifactFile);
-                    String symbolicName = (artifact.getGroupId() + "." + artifact.getArtifactId());
-                    mf =
-                        BundleUtil.libraryManifest(jarFiles,
-                                                   symbolicName,
-                                                   symbolicName,
-                                                   version,
-                                                   null,
-                                                   this.eclipseBuddyPolicy);
+                    String symbolicName = null;
+                    if (customizedMF == null) {
+                        String version = BundleUtil.osgiVersion(artifact.getVersion());
+
+                        Set<File> jarFiles = new HashSet<File>();
+                        jarFiles.add(artifactFile);
+                        symbolicName = (artifact.getGroupId() + "." + artifact.getArtifactId());
+                        mf =
+                            BundleUtil.libraryManifest(jarFiles,
+                                                       symbolicName,
+                                                       symbolicName,
+                                                       version,
+                                                       null,
+                                                       this.eclipseBuddyPolicy,
+                                                       this.executionEnvironment);
+                    } else {
+                        mf = customizedMF;
+                        symbolicName = BundleUtil.getBundleSymbolicName(mf);
+                        if (symbolicName == null) {
+                            throw new MojoExecutionException("Invalid customized MANIFEST.MF for " + artifact);
+                        }
+                        setBundleClassPath(mf, artifactFile);
+                    }
                     File file = new File(dir, "META-INF");
                     file.mkdirs();
                     file = new File(file, "MANIFEST.MF");
@@ -549,7 +580,8 @@
                                                    symbolicName,
                                                    version,
                                                    null,
-                                                   this.eclipseBuddyPolicy);
+                                                   this.eclipseBuddyPolicy,
+                                                   this.executionEnvironment);
                     File file = new File(dir, "META-INF");
                     file.mkdirs();
                     file = new File(file, "MANIFEST.MF");
@@ -594,6 +626,17 @@
 
     }
 
+    private void setBundleClassPath(Manifest mf, File artifactFile) {
+        // Add the Bundle-ClassPath
+        String cp = mf.getMainAttributes().getValue(BUNDLE_CLASSPATH);
+        if (cp == null) {
+            cp = artifactFile.getName();
+        } else {
+            cp = cp + "," + artifactFile.getName();
+        }
+        mf.getMainAttributes().putValue(BUNDLE_CLASSPATH, cp);
+    }
+
     private void generateANTPath(ProjectSet jarNames, File root, Log log) throws FileNotFoundException, IOException {
         for (Map.Entry<String, Set<String>> e : jarNames.nameMap.entrySet()) {
             Set<String> jars = e.getValue();