tags:

views:

2570

answers:

4

I’m trying to embed some JARs into single OSGi bundle using the feature of maven-bundle-plugin

The thing that worries me is that all packages of embedded JARs are put into the Import-Package header of the generated MANIFEST.MF.

If I specify explicitly to use only the packages I need, like in the following snippet:

Import-Package: org.osgi.framework

The build fails with BND error (unresolved references).

So, the question here is how can I build the bundle with embedded JARs with "Import-Package" header I need?

+3  A: 

All the packages that are imported in your classes will be imported by bnd. Perhaps you do not want those packages imported because you know that at runtime you won't be needing them. If you cannot stop bnd from importing them, you can make them optional so that your bundle will still resolve even if they are not supplied by another bundle (at wire time). Try to add this:

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

To your maven bnd configuration in maven.

omerkudat
I don’t want them to be imported for two reasons: 1) they are in the same bundle (in embedded JARs), so as I understand, I don’t need to import them at all! So it’s just adds some garbage to the MANIFEST.MF, which I want to be clean. 2) Don’t want them to be accidentally imported from another bundle.
Ivan Dubrov
+2  A: 

One possible reason why you are seeing "unexpected" packages in Import-Package header is the following:

A general good practice that supports collaboration model in OSGi is to import all packages that you export -- see this blog post by Peter Kriens for detailed explanation why. Bnd (and hence also maven-bundle-plugin) follows this practice by default and automatically imports all exported packages. Therefore you should first check your Export-Package header and make sure that you export only the packages you want.

Also if you want to export packages from the embedded dependencies then you should be careful to avoid duplication inside your bundle -- see section Embed-Dependency and Export-Package of the maven-bundle-plugin documentation.

Pavol Juhos
Maybe you are right... Will check next time I hit this. For now we decided not to go with auto-generated manifests.
Ivan Dubrov
A: 

You should use Bundle-ClassPath if you want to make classes available inside a bundle that contains JARs e.g.

Bundle-ClassPath: foo.jar,other.jar
Import-Package: org.osgi.framework,org.other.imported

You'll need to list the classes that foo.jar and other.jar import/use, but you won't need to list any of the packages in foo.jar or other.jar unless you're actually exporting them.

AlBlue
Right. And the problem is that maven OSGi plug-in (which is built on top of bnd tool) does create Import-Package for every package inside foo.jar and other.jar!
Ivan Dubrov
You can configure bnd not to generate import statements for certain packages. You might also like to try raising a bug against BND for this example.
AlBlue
+1  A: 

You can remove some packages from import-package scope when you embed a JAR into your bundle:

<Import-Package>![package_name9]<Import-Package>

inside pom.xml or if you use external *.bnd files:

Import-Package: ![package_name]
Dmytro Pishchukhin