This article describes how to use Java libraries for Eclipse plug-in and OSGi development.

1. Using Java libraries in Eclipse plug-ins and OSGi bundles

Java libraries are delivered as JAR files. Eclipse plug-ins and OSGi based applications require additional meta-data in the META-INF/MANIFEST.MF file of libraries which they consume. In case a Java library does not provide the additional meta-data, you can add it via multiple ways. As the time of the writing, the easiest is to use the Eclipse Maven (M2E) extension to add such libraries to your target platform. M2E allows to convert non OSGi Java libraries automatically if they are part of the target platform.

You have several ways to convert a JAR file to an plug-in:

  • Use the Eclipse Maven M2e extension to add Maven libraries to your target platform

  • Manually convert the JAR file to a plug-in via an Eclipse wizard

  • Use Gradle plug-ins to converts JARs

  • Use Maven plug-ins to convert JARs

This article has been updated to describe only the M2E extension for the usage of the target platform, as this is the easiest and best A target platform containing m2e extensions is also supported by the Maven Tycho build as of version 2.2.

2. Installation

Ensure that the m2e feature is installed into your IDE. At least version 1.17.0 is required.

You can use the following update site to install it via Help  Install New Software.

https://download.eclipse.org/technology/m2e/releases/latest/

Install m2e PDE Integration, this will also install the basic m2e functionality.

java libraries osgi10

Restart your IDE after the installation.

3. Prerequisites

The following assumes that you already setup a target platform for development.

Perform Setting up a target platform if you have not yet done so.

4. Exercise: Using the GSON library for plug-in development

In this exercise you add a standard library to your target platform.

4.1. Add Maven Java library to your target platform

Open your target platform file and press Add…​.

add java library to targetplatform10

Select Maven.

add java library to targetplatform20

Add your Maven coordinates, you can use https://search.maven.org/ for that. We use Gson as example, but other libraries also work.

The Maven coordinates we use are listed below.

<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.8.6</version>
</dependency>
add java library to targetplatform30

If you copy a Maven dependency into the clipboard before pressing the add button, the information is used to populate the wizard.

Select Compile scope and press the Finish button.

The Dependencies Scope simply defines if dependent artifacts are included or only the master artifact itself.

You can now use the library for your plug-in development, even if the original artifact did not contain the OSGi meta-data.

4.2. Validate availability of the library

To test if the library is available for you to develop, open one manifest file for an existing plug-in and switch to the dependency tab. Press Add…​ and ensure that you can add the library as dependency.

add java library to targetplatform40

To use the library at runtime you also need to add it to your runtime (in OSGi applications) or your product via your features (in your RCP application).

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