When should I use Import-Package and when should I use Require-Bundle?

OSGi allows you to define dependencies through Import-Package , which simply connects one package (exported from any package) and Require-Bundle , which connects to a specific export of named packages.

When creating an OSGi application with a green field, what approach should be used to represent dependencies? Most packages will be internal, but there will be some dependencies on external (open-source) packages.

+44
osgi
Dec 08 '09 at 9:36
source share
6 answers

I believe the Require-Bundle is an Eclipse thing (which now made it into the OSGi specification for hosting Eclipse). OSGi's β€œclean” way is to use Import-Package , as it specifically separates the package from the supplied package. You should declare the dependencies on the functionality you need (the Java API provided by a specific version of a particular package) instead of where that functionality comes from (which doesn't matter to you). This preserves the flexibility of the beam composition.

JavaScript analogy. This is similar to detecting whether the web browser supports a specific API, or disabling the user agent string, what kind of browser.

Peter Criens of the OSGi Alliance talks more about this on the OSGi blog .

Probably the only case where you need to use the Require-Bundle is to separate packages, this is a package that is distributed across several packages. Split packages, of course, are very discouraged.

+44
Dec 08 '09 at 12:43
source share

Use import package on demand.

Require Package:

  • indicates the explicit package (and version) to use. If the requirde package needs to be reorganized and the package moves to another location, then dependents will need to change their MANIFEST.MF
  • gives you access to all export packages, regardless of what they are, and regardless of whether you need them. If the parts you don’t need have their own dependencies, you will need those that will
  • packages can be re-exported
  • although not recommended, allows the use of split packages, i.e. a package that spans multiple packages
  • can be used for non-code dependencies, for example: resources, Help, etc.

Import-Package:

  • weak connection, only the package (and version) is indicated, and the runtime finds the required package
  • Actual implementations may be replaced
  • Dependent packages can be moved to different packages by the package owner
  • But it requires more metadata supported (i.e.: each package name) at lower levels of detail
+12
Dec 07 '11 at 5:10
source share

I believe Import-Package gives you a weaker connection and should be preferred. I use it when declaring package dependencies that I don’t have, such as slf4j, and I can change the implementations as I wish. I use Require-Bundle when a dependency is something that I control, like my own packages, because any important change could pass through me anyway.

+3
Dec 08 '09 at 11:59
source share

Import-Package should be better, because, as already mentioned, you can move a package from one package to another without changing the existing MANIFEST.MF client

But...

There is a practical reason to use Require-Bundle if you use Eclipse to develop your packages:

Eclipse does not use packages as permission units. He uses ligaments. That is, if you use one package of a package, Eclipse compiles your package without reporting any problems using the rest of the packages not imported from this package.

You might (you are a person) think that everything is in order and download your deployment kit, but ... your package will break at runtime.

I am sure of this, because today this problem (for me!).

A good solution would be to change the container of the Eclipse class, but ... if this is not done ... you could decide to avoid such problems requiring packages rather than packages by paying the indicated price (there is no backward compatible code between bundles).

0
Oct 26 '16 at 15:18
source share

Avoid import package. Because packages provide many-to-many relationships between packages, they are subject to dependency cycles that are difficult to detect and avoid.

The Require-Bundle, on the other hand, refers to a single package, making the dependency graph a loop-safe trivial check of build time. With Require-Bundle, it is much easier to create a layered architecture with an isolated lower level of abstraction.

0
Nov 10 '16 at 5:02
source share

I'm not sure that using Import-Package is better, because my expectation by default when working with a package is to work with the corresponding public API. For this reason, the Require-Bundle makes more sense.

-one
Jul 21 2018-11-11T00:
source share



All Articles