OSGi package dependencies and exports

  Written by The Jahia Team
 
Developers
   Estimated reading time:

Before going any further, it is very important to understand what OSGi package dependencies are, how they work and how to use them properly.

The OSGi framework will only let you access a Java package located in another OSGi bundle if:

  • It is exported by an OSGi bundle
  • It is imported by your own bundle

OSGi imports and exports must be declared in the META-INF/MANIFEST.MF file, with the Import-Package and Export-Package headers. If an import or export is missing, or if versions of packages don’t match, no access to the package will be allowed by the bundle’s class loader. Management of the package imports and exports is the main learning curve involved in learning to use OSGi. Fortunately, Jahia offers tooling such as the Jahia Maven Plugin that helps generate dependencies for common module projects. There are also other OSGi plugins available on the Internet but usually the Felix Maven Bundle Plugin and the Jahia Maven plugin should be sufficient for most projects.

If your module is based on jahia-modules, the plugins are preconfigured to detect the required imports. You can manually add or modify packages by setting the property jahia.modules.importPackage in your pom.xml file. It is also possible to completely override the default configuration by setting the import-package property. If you need to expose a package to other modules, you must declare it explicitely by setting the export-package property :

    <properties>
        <jahia.modules.importPackage>org.jahia.defaults.config.spring,org.jahia.modules.external</jahia.modules.importPackage>
        <export-package>org.jahia.modules.external.users</export-package>
    </properties>

Import and exports can also specify versions and options - more documentation can be found here and here.