How to add library dependencies to your OSGi bundle app

Nearly all apps require functionality provided by third party libraries to do things like parse JSON, make HTTP requests, or do high-performance mathematical computations. This guide will explain how to add your library dependencies to your app.

/!\ Important: Do not depend on Cytoscape's third party libraries. Cytoscape's core bundles depend on third party libraries and are included with Cytoscape. You may find that your app also happens to depend on the same libraries. Your app should not depend on Cytoscape's third party libraries. Your app ought to include the library in its bundle even if the same library is provided by Cytoscape. Cytoscape's third party libraries are part of its implementation. These libraries could change in minor version updates of Cytoscape and would break your app.

Configuring your Maven pom file

We'll assume that you are using Maven to build your app and that you are listing all third party library dependencies (along with Cytoscape API dependencies) in your Maven pom file.

Tell maven-bundle-plugin to embed your dependencies

Configure maven-build-plugin (shown below) so that it embeds your dependencies with your bundle jar. By embedding, the dependencies will be included in your bundle jar in tact. This is different from shading, which combines jars into one single jar file.



The most important lines are the Embed-Dependency, Embed-Transitive, and Import-Package directives.

Update the scope for Cytoscape & OSGi API dependencies

Set the scope to provided for all Cytoscape and OSGi API dependencies, like so:


This will tell the maven-bundle-plugin to not include the Cytoscape and OSGi API dependencies with your bundle jar. Your app won't obviously need them when loaded with Cytoscape.

Mark your third party library dependencies as optional

Mark your third party library dependencies as optional, like so:


This tells OSGi that packages needed by transitive dependencies do not need to be fulfilled for your app to work.

At this point you should be able to run mvn clean install successfully and get an OSGi bundle app with your third party library dependencies included.


Ensure that library dependencies were included

After building your app, you can see if all the libraries you expect were included by running this command:

unzip -l target/MyApp.jar | grep jar

This command will print out the names of all libraries included in your app.

Debugging your app

If your app won't load in Cytoscape, you should see first if the issue is OSGi related. To do so:

  1. Run Cytoscape from the command line.
  2. After Cytoscape has started, the command line will have an OSGi prompt. Run the osgi:list command. This will print out all bundles and their ID numbers. If your app bundle's status is Active, that means OSGi was successfully able to load your app.

  3. If OSGi could not load your app, try reloading it: osgi:reload <id-number>. This will print an error message explaining the underlying problem.

  4. Try the osgi:headers <id-number> command. This will print out a list of all packages your app expects and whether the package requirement was met.

HowToAddLibraryDependencies (last edited 2016-01-23 00:07:09 by 192)

Funding for Cytoscape is provided by a federal grant from the U.S. National Institute of General Medical Sciences (NIGMS) of the Na tional Institutes of Health (NIH) under award number GM070743-01. Corporate funding is provided through a contract from Unilever PLC.

MoinMoin Appliance - Powered by TurnKey Linux