Search code examples
mavenosgiapache-felixosgi-bundlemaven-bundle-plugin

OSGI bundle of JARs with maven-bundle-plugin


I am trying to embed some JARs into an OSGI bundle which do not have a corresponding OSGI bundle so that other OSGI bundles can reference them.

I have followed many suggestions but nothing worked. My last attempt is based on: <Export-Package> for all resources using maven-bundle-plugin

When I examine the resulting OSGI package it does not contain the expected embedded jar:

META-INF
META-INF\MANIFEST.MF
META-INF\maven
META-INF\maven\com.mycompany
META-INF\maven\com.mycompany\osgi-bundle-test
META-INF\maven\com.mycompany\osgi-bundle-test\pom.properties
META-INF\maven\com.mycompany\osgi-bundle-test\pom.xml

Here is my POM.XML:

<dependencies>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>18.0</version>
    </dependency>
</dependencies>

<build>
    <defaultGoal>install</defaultGoal>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.8</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <version>3.0.1</version>
            <extensions>true</extensions>
            <configuration>
                <manifestLocation>META-INF</manifestLocation>
                <instructions>
                    <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                    <Bundle-Version>${project.version}</Bundle-Version>
                    <Export-Package>*</Export-Package>
                    <Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
                    <Embed-Directory>target/dependency</Embed-Directory>
                    <Embed-StripGroup>true</Embed-StripGroup>
                    <Embed-Transitive>true</Embed-Transitive>
                </instructions>
            </configuration>
        </plugin>
    </plugins>
</build>

Here is a snippet from mvn clean install:

[INFO] --- maven-dependency-plugin:2.8:copy-dependencies (copy-dependencies) @ osgi-bundle-test ---
[INFO] Copying guava-18.0.jar to <local-dir>\target\dependency\guava-18.0.jar
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ osgi-bundle-test ---
[INFO] Building jar: <local-dir>\target\osgi-bundle-test-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ osgi-bundle-test ---
[INFO] Installing <local-dir>\target\osgi-bundle-test-0.0.1-SNAPSHOT.jar to <repo-dir>\com\mycompany\osgi-bundle-test\0.0.1-SNAPSHOT\osgi-bundle-test-0.0.1-SNAPSHOT.jar
[INFO] Installing <local-dir>\pom.xml to <repo-dir>\com\mycompany\osgi-bundle-test\0.0.1-SNAPSHOT\osgi-bundle-test-0.0.1-SNAPSHOT.pom

Content of MANIFEST.MF:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: xxxx
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_65

Please help.


Solution

  • This worked:

    <modelVersion>4.0.0</modelVersion>
        <groupId>com.mycompany</groupId>
        <artifactId>osgi-bundle-test-v2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>bundle</packaging>
    
        <dependencies>
            <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>18.0</version>
                <scope>runtime</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.felix</groupId>
                    <artifactId>maven-bundle-plugin</artifactId>
                    <version>3.0.1</version>
                    <extensions>true</extensions>
                    <configuration>
                        <instructions>
                            <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                            <Bundle-Version>${project.version}</Bundle-Version>
                            <Export-Package>*</Export-Package>
                            <Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
                            <Embed-Directory>target/dependency</Embed-Directory>
                        </instructions>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    

    Resulting bundle contains target\dependency\guava-18.0.jar

    Manifest:

    Manifest-Version: 1.0
    Bnd-LastModified: 1460052892382
    Build-Jdk: 1.7.0_79
    Built-By: myId
    Bundle-ClassPath: .,target/dependency/guava-18.0.jar
    Bundle-ManifestVersion: 2
    Bundle-Name: osgi-bundle-test-v2
    Bundle-SymbolicName: osgi-bundle-test-v2
    Bundle-Version: 0.0.1.SNAPSHOT
    Created-By: Apache Maven Bundle Plugin
    Embed-Dependency: *;scope=compile|runtime
    Embed-Directory: target/dependency
    Embedded-Artifacts: target/dependency/guava-18.0.jar;g="com.google.guava
     ";a="guava";v="18.0"
    Export-Package: com.google.common.annotations;version="18.0.0",com.googl
     e.common.base;version="18.0.0";uses:="javax.annotation",com.google.comm
     on.base.internal;version="0.0.1.SNAPSHOT",com.google.common.cache;versi
     on="18.0.0";uses:="com.google.common.base,com.google.common.collect,com
     .google.common.util.concurrent,javax.annotation",com.google.common.coll
     ect;version="18.0.0";uses:="com.google.common.base,javax.annotation",co
     m.google.common.escape;version="18.0.0";uses:="com.google.common.base,j
     avax.annotation",com.google.common.eventbus;version="18.0.0",com.google
     .common.hash;version="18.0.0";uses:="com.google.common.base,javax.annot
     ation",com.google.common.html;version="18.0.0";uses:="com.google.common
     .escape",com.google.common.io;version="18.0.0";uses:="com.google.common
     .base,com.google.common.collect,com.google.common.hash,javax.annotation
     ",com.google.common.math;version="18.0.0",com.google.common.net;version
     ="18.0.0";uses:="com.google.common.base,com.google.common.collect,com.g
     oogle.common.escape,javax.annotation",com.google.common.primitives;vers
     ion="18.0.0";uses:="com.google.common.base,javax.annotation",com.google
     .common.reflect;version="18.0.0";uses:="com.google.common.collect,javax
     .annotation",com.google.common.util.concurrent;version="18.0.0";uses:="
     com.google.common.base,com.google.common.collect,javax.annotation",com.
     google.common.xml;version="18.0.0";uses:="com.google.common.escape",com
     .google.thirdparty.publicsuffix;version="0.0.1.SNAPSHOT";uses:="com.goo
     gle.common.collect",target.dependency;version="0.0.1.SNAPSHOT"
    Import-Package: com.google.common.base;version="[18.0,19)",com.google.co
     mmon.cache;version="[18.0,19)",com.google.common.collect;version="[18.0
     ,19)",com.google.common.escape;version="[18.0,19)",com.google.common.ha
     sh;version="[18.0,19)",com.google.common.io;version="[18.0,19)",com.goo
     gle.common.math;version="[18.0,19)",com.google.common.primitives;versio
     n="[18.0,19)",com.google.common.reflect;version="[18.0,19)",com.google.
     common.util.concurrent;version="[18.0,19)",com.google.thirdparty.public
     suffix;version="[0.0,1)",javax.annotation,sun.misc
    Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.6))"
    Tool: Bnd-3.0.0.201509101326