diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-05-27 01:22:08 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-05-27 01:22:08 +0000 |
commit | d8da679db3c406c9c56cc14d047645d0cebb7afd (patch) | |
tree | 9defab15ba1410fd8421b9eb479e7ab98369b255 /maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache | |
parent | 3426488b8797730981da3aa0deb6b0b9f02bd9d1 (diff) |
Add capability to override MANIFEST.MF
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@778954 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache')
3 files changed, 147 insertions, 44 deletions
diff --git a/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/ArtifactManifest.java b/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/ArtifactManifest.java new file mode 100644 index 0000000000..8c295700d4 --- /dev/null +++ b/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/ArtifactManifest.java @@ -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;
+ }
+}
diff --git a/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/BundleUtil.java b/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/BundleUtil.java index cb44d1bfdc..f8566b835b 100644 --- a/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/BundleUtil.java +++ b/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/BundleUtil.java @@ -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.IOException; 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 @@ final class BundleUtil { 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 @@ final class BundleUtil { } return bundleName; } - + static Manifest getManifest(File file) throws IOException { if (!file.exists()) { return null; @@ -103,32 +104,33 @@ final class BundleUtil { /** * 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 @@ final class BundleUtil { 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 @@ final class BundleUtil { logger.warning("Duplicate package skipped: " + export); } } - + // Create a manifest Manifest manifest = new Manifest(); Attributes attributes = manifest.getMainAttributes(); @@ -196,7 +203,7 @@ final class BundleUtil { /** * Write a bundle manifest. - * + * * @param manifest * @param out * @throws IOException @@ -219,7 +226,7 @@ final class BundleUtil { /** * Add packages to be exported out of a JAR file. - * + * * @param jarFile * @param packages * @throws IOException @@ -235,7 +242,7 @@ final class BundleUtil { /** * Write manifest attributes. - * + * * @param attributes * @param key * @param dos @@ -264,7 +271,7 @@ final class BundleUtil { /** * Strip an OSGi export, only retain the package name and version. - * + * * @param export * @return */ @@ -289,7 +296,7 @@ final class BundleUtil { /** * Add all the packages out of a JAR. - * + * * @param jarFile * @param packages * @param version @@ -315,6 +322,22 @@ final class BundleUtil { 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 @@ final class BundleUtil { /** * Add the packages exported by a bundle. - * + * * @param file * @param packages * @return @@ -397,7 +420,7 @@ final class BundleUtil { } /** - * Convert the maven version into OSGi version + * Convert the maven version into OSGi version * @param mavenVersion * @return */ diff --git a/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/ModuleBundlesBuildMojo.java b/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/ModuleBundlesBuildMojo.java index 4fa5762118..a13734e316 100644 --- a/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/ModuleBundlesBuildMojo.java +++ b/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/bundle/plugin/ModuleBundlesBuildMojo.java @@ -239,6 +239,11 @@ public class ModuleBundlesBuildMojo extends AbstractMojo { private ArtifactAggregation[] artifactAggregations; /** + * @parameter + */ + private ArtifactManifest[] artifactManifests; + + /** * Inserts a generic Eclipse-BuddyPolicy header into generated artifacts manifests * @parameter */ @@ -305,6 +310,25 @@ public class ModuleBundlesBuildMojo extends AbstractMojo { } } + 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 @@ public class ModuleBundlesBuildMojo extends AbstractMojo { 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 @@ public class ModuleBundlesBuildMojo extends AbstractMojo { 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 @@ public class ModuleBundlesBuildMojo extends AbstractMojo { 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 @@ public class ModuleBundlesBuildMojo extends AbstractMojo { // 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 @@ public class ModuleBundlesBuildMojo extends AbstractMojo { 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 @@ public class ModuleBundlesBuildMojo extends AbstractMojo { } + 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(); |