问题
When building an Eclipse plugin (lato sensu) which consists of multiple plugins and one feature, I have two ways of specifying dependencies:
- in the plugins themselves, using
Require-Bundle
andImport-Package
inMETA-INF/MANIFEST.MF
; - in the
feature.xml
file from the feature.
In my understanding, it should be enough to declare the dependencies at the lower level, i.e. in the plugins. Why do we still have the feature.xml
requires
mechanism?
Update: feature.xml description in the Eclipse Help
回答1:
It is incorrect to categorize feature-to-feature dependency mechanism as legacy. While it is certainly true that with advent of p2, dependencies specified via bundle manifest Require-Bundle or Import-Package will be installed, the result may not be what you expect.
Consider the case where you are building an extension to JDT. Say you only depend on JDT core api (no UI extensions). If you only rely on OSGi dependencies, when your plugin is installed, p2 will dutifully install JDT core bundle, but not the UI bundle. Perfectly fine from OSGi perspective, but probably not what you intended.
I recommend sticking with feature import to describe your high level dependencies to make sure that they are installed in full. Relying only on OSGi dependencies works best for free-floating bundles that aren't part of something bigger that should be installed as a unit.
回答2:
Eclipse feature is the notion to manage the plug-ins to provide higher abstract.
For example, there are more than twenty plug-ins for C/C++ development tools, so CDT has several features to organize those plug-ins in higher level abstract, for core functionality, ui, build and so on.
It also helps simplify the installation process, users only need know the top feature(with friendly name) of CDT. P2 API or classical install manage could find the plug-ins and sub-features including by the top feature, then install them.
However feature could NOT help you creating high module system, because itself doesn't provide any functionality. It's not a part of OSGi spec, it's inherited from Eclipse 2.x or even older version.
Bundle is the essential of OSGi. According to the spec any bundle couldn't use other classes besides declaring the wire with the package of classes. Require-Bundle and Import-Package are the ways to create the wires.
In a short word, feature and plug-in are totally different notions. Feature including plug-ins is doing different things comparing to Import-Package of bundle.
Update:
The requires tag of feature.xml is the legacy. The intend is that defining the dependencies of this feature with other plug-ins/features to help update manager to find the broken dependencies when installing or updating that feature. It does define the dependencies of that feature with other plug-ins, but it's used by update manager for installing or updating. Import-Package of bundle declares the actual and real dependency between your bundle and another module in the OSGi system.
Since eclipse uses p2 as provisioning manager, it's not necessary to declare the 'requires' tag in your feature.xml. P2 would recognize the dependencies between your bundle and other modules that are declared by 'Import-Package' or 'Require-Bundle'. P2 won't install or update your feature if any dependency is not satisfied.
来源:https://stackoverflow.com/questions/4535762/require-bundle-and-import-package-versus-feature-xml-requires