4

This might be simple question but have confused me for sometime.

I'm developing an OSGi bundle which rely on google guice. Google guice has one main bundle and several fragment like assisted inject fragment. In my bundle I have used the assisted inject fragment and a class that is exported from that fragment.

However in Eclipse, I can only set bundle dependency on another bundel (in this case, the guice bundle) but not directly on a fragment (assisted inject), hence Eclipse complains cannot find the class exported from the fragment.

How can I have the dependency on a bundle's fragment?

Wudong
  • 2,320
  • 2
  • 32
  • 46

2 Answers2

7

Use an Import-Package dependency on the fragment's exported packages.

In fact you should use Import-Package for all your dependencies, and avoid Require-Bundle wherever possible.

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
  • Unfortunately there are a number of cases, where it cannot be avoided. E.g. if you're doing Eclipse plug-in that uses extension points, then you still need `Require-Bundle`. – Tonny Madsen Aug 22 '11 at 06:35
  • 1
    Hi Tonny. You do **not** need a Require-Bundle dependency when using eclipse extension points. In fact you don't need any dependency, unless you need to implement an API or interface. Unfortunately the PDE tooling *suggests* you add a Require-Bundle, but it is not necessary . – Neil Bartlett Aug 22 '11 at 07:29
  • I stand corrected. It works even without the declaration... Thanks. – Tonny Madsen Aug 22 '11 at 08:11
  • But the problem is when I tried to add the imported-packages in the MAINFEST.MF file using the PDE's editor, the package from the fragemnt doesn't show in the list! – Wudong Aug 22 '11 at 08:36
  • 1
    Wudong - you may also need to add `Eclipse-ExtensibleAPI: true` to the fragment host (that is, Guice itself). Note that this is a pure-tooling header used by PDE, not an Eclipse or OSGi runtime header. See http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fmisc%2Fbundle_manifest.html – Neil Bartlett Aug 22 '11 at 09:07
  • what's the reason to avoid Require-Bundle wherever possible? – Wudong Aug 22 '11 at 13:16
  • @Wudong - It creates explicit dependencies between bundles, which goes against standard OSGi practices. Imports have no such explicit dependency and allows the OSGi framework to resolve its required classes at runtime. This makes it easy to change the provider of any given class/package without impacting the bundle that requires it. – Robin Aug 22 '11 at 16:49
1

In addition to adding the required package to Import-Package, you might need to add Eclipse-ExtensibleAPI: true to the manifest to prevent unresolved dependency error in PDE.

vitaut
  • 49,672
  • 25
  • 199
  • 336