How to handle Import-Package entries which come from jars on the Bundle-Classpath?

岁酱吖の 提交于 2019-11-29 15:01:22

When you embed any jar via Embed-Dependency tag then the maven-bundle-plugin would analyze that jar also and add the referred packages to the host bundle Import-Package list. Mostly such packages are optional depending on the feature used. For example in your usecase the H2 jar has dependency on Lucene, Servlet API for certain features. if your usage scenario does not require these features then you can mark these packages as optional

<plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <version>2.3.5</version>
        <extensions>true</extensions>
        <configuration>
          <obrRepository>NONE</obrRepository>
          <instructions>
            <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
            ..
            <Embed-Dependency>
              h2
            </Embed-Dependency>
            <Import-Package>
              org.osgi.service.jdbc.*;
              org.apache.lucene.*;
              javax.transaction.*;resolution:=optional,
              *
            </Import-Package>
          </instructions>
        </configuration>
      </plugin>

In above config we mark such packages as optional. The main problem is to determine the package list depending on your usage

One quick and dirty way is to mark all as optional imports. Should be used as a last resort. As some valid imports would be marked optional and OSGi fwk would not be able to check them.

        <Import-Package>
          *;resolution:=optional
        </Import-Package>

Also note that H2 jar is a valid OSGi bundle so you can deploy it directly.

Shailesh Pratapwar

rule regarding jar files which are specified on the Bundle-Classpath ?

The files which are listed on the bundle-classpath are scanned by maven-bundle-plugin(mbp) to identify imports needed by each specified jar. Thereby, mbp will add the required imports in the main (your main bundle) manifest.mf. This does means that the packages should be exported by the outside bundles. If required packages are not found outside of the bundle then the bundle will not start.

You have 2 solutions to use the third party jars required by your application.

  1. Prepare the OSGi bundle of each and every third party jar. You may find the osgi bundle already created for spring jars and other open source projects at spring repository here. Just search it perfectely. you will find it.

  2. Use Bundle-classpath: with this you will have to put your third party dependencies (and "all" their transitive dependencies) in your bundle and specify each jar in Bundle-ClassPath header. With this case, mbp will analyze the bundle classpath jars and try to mess with your import-package header. You just avoid this by including your custom header in pom.xml. If you choose for your custom import-package , be careful to include the required packages from other (outside) bundles correctely in your import-package.

Thumb rule : If your app finds something in your bundle-classpath then it will not go for import-package.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!