This article describes how to use Java libraries for Eclipse plug-in and OSGi development.
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.
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.
Install m2e PDE Integration, this will also install the basic m2e functionality.
Restart your IDE after the installation.
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.
In this exercise you add a standard library to your target platform.
Open your target platform file and press Add….
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>
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.
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.
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.