diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-10-08 19:04:44 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-10-08 19:04:44 +0000 |
commit | c65981080c258a2a7b0ce669bd8474f38a7accce (patch) | |
tree | 805b5b66fe68fe5add252981c1944fbe1077a752 /maven-plugins/trunk/maven-bundle-plugin/src/main/java/org | |
parent | 30d8c85609df305cada81f0068a7389777af646d (diff) |
Add compile dependencies to the .classpath file if it doesn't supply any packages to the OSGi import. This maks this plugin more closer to the mvn eclipse:eclipse behavior.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@823280 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'maven-plugins/trunk/maven-bundle-plugin/src/main/java/org')
2 files changed, 100 insertions, 68 deletions
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 6f7f6d245c..8c36cdaec2 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 @@ -37,6 +37,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; +import java.util.Collections; import java.util.HashSet; import java.util.Properties; import java.util.Set; @@ -59,7 +60,7 @@ import org.osgi.framework.Version; * * @version $Rev$ $Date$ */ -final class BundleUtil { +public final class BundleUtil { static final String META_INF_SERVICES = "META-INF.services;partial=true;mandatory:=partial"; private final static Logger logger = Logger.getLogger(BundleUtil.class.getName()); /** @@ -69,7 +70,7 @@ final class BundleUtil { * @return * @throws IOException */ - static String getBundleSymbolicName(File file) throws IOException { + public static String getBundleSymbolicName(File file) throws IOException { Manifest manifest = getManifest(file); return getBundleSymbolicName(manifest); } @@ -376,37 +377,68 @@ final class BundleUtil { * @return * @throws IOException */ - private static void addExportedPackages(File file, Set<String> packages) throws IOException { + public static Set<String> getExportedPackages(File file) throws IOException { if (!file.exists()) { - return; + return Collections.emptySet(); } + Set<String> packages = new HashSet<String>(); + Manifest manifest = getManifest(file); + // Read the export-package declaration and get a list of the packages available in a JAR - Set<String> existingPackages = null; + String bundleName = null; String exports = null; - if (file.isDirectory()) { - File mf = new File(file, "META-INF/MANIFEST.MF"); - if (mf.isFile()) { - Manifest manifest = new Manifest(new FileInputStream(mf)); - exports = manifest.getMainAttributes().getValue(EXPORT_PACKAGE); - } - } else { - JarFile jar = new JarFile(file, false); - Manifest manifest = jar.getManifest(); + + if (manifest != null) { exports = manifest.getMainAttributes().getValue(EXPORT_PACKAGE); - jar.close(); - existingPackages = new HashSet<String>(); - addAllPackages(file, existingPackages, ""); + bundleName = manifest.getMainAttributes().getValue(BUNDLE_SYMBOLICNAME); } - if (exports == null) { - return; + + if (bundleName == null) { + Set<String> allPackages = new HashSet<String>(); + addAllPackages(file, allPackages, ""); + for (String p : allPackages) { + packages.add(packageName(p)); + } + return packages; } + packages.addAll(parsePackages(exports)); + + return packages; + } + + public static Set<String> getImportedPackages(File file) throws IOException { + if (!file.exists()) { + return Collections.emptySet(); + } + + Manifest manifest = getManifest(file); + + // Read the export-package declaration and get a list of the packages available in a JAR + String bundleName = null; + String imports = null; + + if (manifest != null) { + imports = manifest.getMainAttributes().getValue(IMPORT_PACKAGE); + bundleName = manifest.getMainAttributes().getValue(BUNDLE_SYMBOLICNAME); + if (imports != null && bundleName != null) { + return parsePackages(imports); + } + } + return Collections.emptySet(); + } + + private static Set<String> parsePackages(String header) { + if (header == null) { + return Collections.emptySet(); + } + Set<String> packages = new HashSet<String>(); // Parse the export-package declaration, and extract the individual packages StringBuffer buffer = new StringBuffer(); boolean q = false; - for (int i = 0, n = exports.length(); i < n; i++) { - char c = exports.charAt(i); + for (int i = 0, n = header.length(); i < n; i++) { + char c = header.charAt(i); if (c == '\"') { q = !q; } @@ -416,9 +448,7 @@ final class BundleUtil { // Add the exported package to the set, after making sure it really exists in // the JAR String export = buffer.toString(); - if (existingPackages == null || existingPackages.contains(packageName(export))) { - packages.add(stripExport(export)); - } + packages.add(packageName(export)); buffer = new StringBuffer(); continue; } @@ -426,14 +456,10 @@ final class BundleUtil { buffer.append(c); } if (buffer.length() != 0) { - - // Add the exported package to the set, after making sure it really exists in - // the JAR String export = buffer.toString(); - if (existingPackages == null || existingPackages.contains(packageName(export))) { - packages.add(stripExport(export)); - } + packages.add(packageName(export)); } + return packages; } /** diff --git a/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/plugin/eclipse/AbstractIdeSupportMojo.java b/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/plugin/eclipse/AbstractIdeSupportMojo.java index 54200de166..6fc8295a4e 100644 --- a/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/plugin/eclipse/AbstractIdeSupportMojo.java +++ b/maven-plugins/trunk/maven-bundle-plugin/src/main/java/org/apache/tuscany/maven/plugin/eclipse/AbstractIdeSupportMojo.java @@ -31,10 +31,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeSet; -import java.util.jar.Attributes; -import java.util.jar.JarFile; import java.util.jar.Manifest; -import java.util.zip.ZipFile; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.factory.ArtifactFactory; @@ -65,6 +62,7 @@ import org.apache.maven.plugin.eclipse.Messages; import org.apache.maven.plugin.ide.IdeDependency; import org.apache.maven.plugin.ide.IdeUtils; import org.apache.maven.project.MavenProject; +import org.apache.tuscany.maven.bundle.plugin.BundleUtil; import org.codehaus.plexus.logging.LogEnabled; import org.codehaus.plexus.logging.Logger; @@ -541,6 +539,12 @@ public abstract class AbstractIdeSupportMojo if ( resolveDependencies ) { MavenProject project = getProject(); + Set<String> imported = Collections.emptySet(); + try { + imported = BundleUtil.getImportedPackages(project.getBasedir()); + } catch (IOException e1) { + throw new MojoExecutionException(e1.getMessage(), e1); + } ArtifactRepository localRepo = getLocalRepository(); List deps = getProject().getDependencies(); @@ -651,44 +655,21 @@ public abstract class AbstractIdeSupportMojo // we need to check the manifest, if "Bundle-SymbolicName" is there the artifact can be // considered // an osgi bundle + if ("pom".equals(art.getType())) { + continue; + } + File artifactFile = art.getFile(); + MavenProject reactorProject = getReactorProject(art); + if (reactorProject != null) { + artifactFile = reactorProject.getBasedir(); + } boolean isOsgiBundle = false; String osgiSymbolicName = null; - if ( art.getFile() != null ) - { - JarFile jarFile = null; - try - { - jarFile = new JarFile( art.getFile(), false, ZipFile.OPEN_READ ); - - Manifest manifest = jarFile.getManifest(); - if ( manifest != null ) - { - osgiSymbolicName = - manifest.getMainAttributes().getValue( - new Attributes.Name( - "Bundle-SymbolicName" ) ); - } - } - catch ( IOException e ) - { - getLog().info( "Unable to read jar manifest from " + art.getFile() ); - } - finally - { - if ( jarFile != null ) - { - try - { - jarFile.close(); - } - catch ( IOException e ) - { - // ignore - } - } - } + try { + osgiSymbolicName = BundleUtil.getBundleSymbolicName(artifactFile); + } catch (IOException e) { + getLog().error("Unable to read jar manifest from " + artifactFile, e); } - isOsgiBundle = osgiSymbolicName != null; IdeDependency dep = @@ -707,6 +688,31 @@ public abstract class AbstractIdeSupportMojo if (!(pde && (Artifact.SCOPE_COMPILE.equals(art.getScope()) || Artifact.SCOPE_PROVIDED .equals(art.getScope())))) { dependencies.add( dep ); + } else { + // Check this compile dependency is an OSGi package supplier + if (!imported.isEmpty()) { + Set<String> exported = Collections.emptySet(); + try { + exported = BundleUtil.getExportedPackages(artifactFile); + } catch (IOException e) { + getLog().error("Unable to read jar manifest from " + art.getFile(), e); + } + boolean matched = false; + for (String p : imported) { + if (exported.contains(p)) { + matched = true; + break; + } + } + if (!matched) { + dependencies.add(dep); + } else { + getLog() + .debug("Compile dependency is skipped as it is added through OSGi dependency: " + art); + } + } else { + dependencies.add(dep); + } } } } |