Generating bundles automatically
As we've said before, it's a sad fact that there are many useful Java libraries that aren't
yet available as OSG i bundles. You now know how to turn these JAR s into bundles by hand
by updating the manifest with the right headers, but it's not particularly easy. You might
be able to work out which packages to export, based on a published API , but how do you
know what to import? If it was easy to work out a bundle's dependencies by looking at
it, setting a normal Java classpath wouldn't be such a nuisance! See figure 12.1.
Rather than suffer through the slow process of converting every library JAR by
hand, a few OSG i developers turned their attention to automating the process. Read-
ing through this section, you'll see that there are a few problems with automatic bun-
dle conversion, but that for the most part it dramatically speeds up the process of
building an OSG i bundle. A number of tools are available for you to use, too many for
us to describe all of them in detail. We'll cover the most popular options.
In section 8.2.1., we first introduced you to bnd as a useful build-time tool for packag-
ing OSG i bundles. One of the most useful functions of bnd has nothing to do with
building. Bnd can use the same logic it uses to generate manifests at compile time to
generate manifests for existing JAR s.
To demonstrate bnd, you'll need the command line version of bnd itself ( http://
www.aqute.biz/Bnd/Download ) and a simple demonstration JAR . Rather than trying
to use a real library, which would probably be too complicated, you'll use the Fancy
Foods superstore chocolate department bundle from back in chapter 3. This may
sound odd—after all, the chocolate department is already an OSG i bundle—but
remember that a bundle is still, at its heart, a JAR file.
How to run bnd
Bnd is a multipurpose tool that integrates with lots of other build tools and runtimes.
As a result, there are dozens of ways that you can use and launch bnd. To keep things
simple, you'll use bnd from the command line, but there are plenty of other options
described in bnd's documentation.
Figure 12.1 Tools can take a conventional Java JAR and produce sensible OSGi bundles more
reliably than converting them by hand.