Embed thirdparty JAR using BND
Asked Answered
V

2

8

I have an OSGi bundle that is built using ANT and the classic BND tool. My bundle uses a library (JAR) internally, which is not available as a bundle within my OSGi container (Apache Felix). So, I am trying to embed it within my bundle, for access at runtime.

How can I embed such a library/JAR using ANT+BND? (Note : I cannot use MAVEN, using which this could have been a lot easier)

Vardhamana answered 26/9, 2011 at 11:22 Comment(0)
L
13

You need two instructions in your bnd descriptor. First use Include-Resource to include the target JAR into your bundle:

Include-Resource: foo.jar

Then you need to specify that foo.jar needs to be on the bundle classpath. I assume that the bundle contents itself also needs to be part of the bundle classpath, so we need to include it as well with a dot:

Bundle-ClassPath: ., foo.jar

Note that @seh's answer about slurping the JAR's packages into your bundle with Private-Package is also correct (in that case the JAR would need to be visible on the build-time classpath). I would never use Export-Package for this though, because I think bundles should keep tight control over how much they export.

Lawtun answered 26/9, 2011 at 12:52 Comment(3)
"need to be visible on the build-time classpath" - what does that really mean? Could you please shed more light? Thanks.Vardhamana
It means that it needs to be on the classpath supplied to the ANT task. Bnd pulls packages from the build classpath, and it doesn't really care whether they are 3rd party packages or your code. For example if I did something silly like Private-Package: javax.* I would include most of the JRE in my bundle! But if I say Private-Package: org.some.library.* then I will only get the org.some.library packages IF they are on the classpath.Lawtun
I didn't know about the Bundle-Classpath header, never having used it. For reference, it's described in section 3.2.1.4 of the OSGi Core Specification v4.3.Pteropod
P
0

There is a BND-supplied Ant task called "bndwrap". It is not well documented. When I've tried to use it, I had to read the Java code to see what it was doing. (See the bnd#doWrap() method here as well.)

I recall that it's also possible to "embed" a depended-upon Jar file another way: not directly as a Jar-within-a-Jar, but by slurping all of its classes into your bundle, simply by declaring in your Private-Package BND directive that the packages provided by the other Jar should be included in yours. Alternately, you can mention those packages in an Export-Package directive to get them both included and exported.

Pteropod answered 26/9, 2011 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.