How to include a jar of dependency in an OSGi bund

2019-04-09 23:40发布

I have an OSGi compliant bundle(jar), in which I want to add a jar of a dependency. The dependecy I want to add is of a Database Driver. That jar is not present in the lib folder of the Karaf container which I am using, and there is no way of adding it there manually. I have access only to the deploy folder, where I can deploy my bundles. I am using maven bundle plugin to package my bundle. So, I wanted to know whether there is a way to add the dependency jar in my bundle. Currently, I am adding the jar manually to the bundle by opening the bundle in 7zip and adding the jar by copying it in the jar and it works fine. I tried using the <embed-dependency> tag, but after doing that the bundle doesn't gets deployed. Is there any way to do it ?

The following is the dependency in pom.xml which I want to add in the bundle:

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.3.158</version>
    </dependency>

The following is the build tag in pom.xml:

<build>
    <defaultGoal>install</defaultGoal>
    <plugins>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Export-Package>
                        com.ct.service.userService.*,
                        org.h2.*
                    </Export-Package>
                    <Import-Package>
                        *,
                        org.codehaus.jackson.jaxrs
                    </Import-Package>
                    <Embed-Dependency>h2</Embed-Dependency>
                </instructions>
            </configuration>
        </plugin>
    </plugins>
</build>

I get the following error when I try to deploy it:

ERROR: Bundle com.ge.dsp.userService [205] Error starting file:D:Karaf/deploy/userService-0.0.1-SNAPSHOT.jar (org.osgi.framework.BundleException: Unresolved constraint in bundle com.ge.dsp.userService [205]: Unable to resolve 205.2: missing requirement [205.2] osgi.wiring.package; (osgi.wiring.package=org.apache.lucene.analysis))

org.osgi.framework.BundleException: Unresolved constraint in bundle com.ct.service.userService [205]: Unable to resolve 205.2: missing requirement [205.2] osgi.wiring.package; (osgi.wiring.package=org.apache.lucene.analysis)
    at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:3826)
    at org.apache.felix.framework.Felix.startBundle(Felix.java:1868)
    at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1191)
    at org.apache.felix.framework.FrameworkStartLevelImpl.run(FrameworkStartLevelImpl.java:295)
    at java.lang.Thread.run(Thread.java:662)

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-04-10 00:10

It seems like I needed to deploy the h2-1.3.158.jar along with my bundle and add the make some edits in the pom.xml as follows:

<build>
<defaultGoal>install</defaultGoal>
<plugins>
    <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <extensions>true</extensions>
        <configuration>
            <instructions>
                <Export-Package>
                    com.ct.service.userService.*,
                    <!--org.h2.*    No need to export these dependency -->
                </Export-Package>
                <Import-Package>
                    *,
                    org.codehaus.jackson.jaxrs,
                    org.h2               <!-- Needed to import the dependencies. -->
                </Import-Package>
                <!--<Embed-Dependency>h2</Embed-Dependency> No need of embedding -->
            </instructions>
        </configuration>
    </plugin>
</plugins>

查看更多
登录 后发表回答