NOW Hiring

Quick links

Share

This article describes the manual process of converting a Java library delivered as JAR files to an Eclipse plug-ins (OSGi bundles).

1. JAR files without OSGi meta-data

If a JAR file does not contain the OSGi meta-data in the META-INF/MANIFEST.MF file, it cannot be directly consumed by other Eclipse plug-ins.

If you want to use such standard Java libraries in other Eclipse plug-ins you have to convert them also into a plug-in. After the conversion the resulting JAR file can still be used in a non OSGi runtime, e.g., a Java webserver. The Java runtime ignores the additional OSGi meta-data.

Eclipse provides a wizard to convert a JAR file with OSGi meta-data to a plug-in. The usage of this wizard is demonstrated in Creating a plug-in project for your jar.

If you repackage a JAR into a plug-in it is wise to check if the license allows this. You should also try to contact the author of the software and ask if he can integrate the OSGi meta-data directly in his library.

2. Integrating external jars / third party libraries

The following gives an example how to convert a standard Java JAR to an Eclipse plug-in via the user interface.

2.1. Creating a plug-in project for your jar

Create a new plug-in project by selecting File ▸ New ▸ Project ▸ Plug-in Development ▸ Plug-in from Existing JAR Archives.

externaljars20

Add the JAR files you want to have in this new plug-in. Press next.

externaljars25

Enter a name and a version for your new plug-in. Uncheck the Unzip the JAR archive into the project flag. Unchecking this flag prevents that the class files are extracted from the JAR file which is not necessary to use them.

Afterwards press the Finish button in the wizard.

externaljars30

You have created a new plug-in for the selected JAR files. Open the MANIFEST.MF file and validate that all required packages are exported on the Runtime tab. All the packages from your JAR files should be included in the exported packages. Otherwise OSGi prevents other plug-ins from accessing them.

2.2. Using the new plug-in project

In the plug-in project which should access the library, open the MANIFEST.MF file and select the Dependencies tab. Add the new plug-in as dependency.

externaljars40

3. Convert JAR files to OSGi bundles with Gradle

You can also use the Gradle build system to convert JARs to OSGi bundles. For example, the following build.gradle file converts the EasyMock library and its dependencies to OSGi bundles. Under the hood, it uses the bnd tooling.

buildscript {
        repositories {
                mavenCentral()
        }
        dependencies {
                classpath 'org.standardout:bnd-platform:1.2.0'
        }
}

apply plugin: 'org.standardout.bnd-platform'

repositories {
        mavenCentral()
}

platform {
        // eclipseHome only required for the "updateSite" task
        eclipseHome = new File('/home/vogella/dev/eclipse46-2016-01-25/eclipse')
        // list of JARs to convert to OSGi
        bundle "org.powermock:powermock-easymock-release-full:1.6.1"
}

Run gradle bundles to create OSGi bundles from the specified JAR files. Use the gradle updateSite task to create a p2 update site.

4. About this website

Nothing listed.

5.1. vogella GmbH training and consulting support

TRAINING SERVICE & SUPPORT

The vogella company provides comprehensive training and education services from experts in the areas of Eclipse RCP, Android, Git, Java, Gradle and Spring. We offer both public and inhouse training. Whichever course you decide to take, you are guaranteed to experience what many before you refer to as “The best IT class I have ever attended”.

The vogella company offers expert consulting services, development support and coaching. Our customers range from Fortune 100 corporations to individual developers.

Copyright © 2012-2016 vogella GmbH. Free use of the software examples is granted under the terms of the EPL License. This tutorial is published under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Germany license.

See Licence.