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

1.1. Using standard JAR files for Eclipse plug-ins

Java libraries are delivered as JAR files. Eclipse plug-ins and OSGi-based applications require additional metadata in the META-INF/MANIFEST.MF file of a library. If a Java library does not provide this metadata, you can convert the JAR file to a plug-in in two ways:

  • Use the Eclipse Maven integration (M2E) to add Maven libraries to your target platform

  • Manually convert the JAR file to a plug-in by adding the required metadata

The M2E-based approach is the easiest one. It converts non-OSGi Java libraries automatically, if they are part of the target platform. It also allows including dependencies and excluding certain transitive dependencies.

1.2. Adding a dependency to the target file

The following target definition adds a library as a dependency and excludes certain transitive libraries.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?pde version="3.8"?>
<target name="test" sequenceNumber="170">
    <locations>
        <location includeDependencyDepth="infinite" includeDependencyScopes="compile"
            includeSource="true" label="EclipseLink" missingManifest="generate" type="Maven">
            <feature id="eclipselink" label="EclipseLink" version="1.0.0.qualifier" />
            <dependencies>
                <dependency>
                    <groupId>org.eclipse.persistence</groupId>
                    <artifactId>org.eclipse.persistence.jpa</artifactId>
                    <version>4.0.0</version>
                    <type>jar</type>
                </dependency>
                <!-- For Lazy Loading
                Warning: Build fails if commented out:
                Missing requirement: org.eclipse.angus.mail 1.0.0 requires 'java.package; sun.security.util 0.0.0' but it could not be found
                <dependency>
                    <groupId>org.eclipse.persistence</groupId>
                    <artifactId>eclipselink</artifactId>
                    <version>4.0.0</version>
                    <type>jar</type>
                </dependency>
                -->
            </dependencies>
            <exclude>org.apache.ant:ant:1.10.12</exclude>
            <!-- PS: for org.eclipse.persistence:eclipselink the following is needed -->
            <exclude>jakarta.transaction:jakarta.transaction-api:2.0.1</exclude>
        </location>
    </locations>
</target>

2. Installation of the M2E-PDE integration

To use the Maven library integration in your target platform, the M2E PDE Integration must be installed in your IDE. If that extension is not installed, you can use the following update site to install it via Help  Install New Software.

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

Install M2E PDE Integration.

Install dialog filtered for pde with M2E - PDE Integration selected

Restart your IDE after the installation.

3. Prerequisites

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

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

4. Exercise: Using a Java library via Maven

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

4.1. Add Gson library from Maven to the target platform

Open your target platform definition file and press the Add…​ button on the first tab. Select Maven in the following dialog.

Add Content dialog listing the available content sources

Use the following Gson Maven coordinates.

<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.14.0</version>
</dependency>

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

The Dependencies depth and Dependencies scopes settings define whether dependent artifacts are added as well or only the artifact itself. Set Dependencies depth to infinite and select the compile scope. This adds Gson together with its compile dependencies, which is only the annotation library error_prone_annotations in this case. For a library with runtime-scoped dependencies, select the runtime scope as well.

Add a new Maven dependency page for com.google.code.gson

Press Finish. Gson already contains OSGi metadata. For an artifact without it, the Missing OSGi-Manifest setting generate creates the metadata, so you can use such libraries in your plug-ins as well.

Save the target definition file and click Reload Target Platform in the editor header, so that your workspace uses the new content.

Target Definition editor with the Maven location for Gson and the Reload Target Platform link

4.2. Validate availability of the library

To test that the library is available, open the manifest file of an existing plug-in and switch to the Dependencies tab. Press Add…​ and ensure that you can add the library as a dependency. Depending on your target platform, the dialog can list more than one version of com.google.gson, as the Eclipse platform ships Gson, too.

Plug-in Selection dialog filtered for gson with com.google.gson selected

If the MANIFEST.MF editor still does not show the library after the reload, restart the IDE.

4.3. Add the new plug-in to the product

Add the Gson plug-in to your product via your feature and start your application. Gson imports error_prone_annotations as optional, so the feature does not need it.

How to exclude a certain dependency if you include the dependencies of a library

You can exclude dependencies of libraries. The exclusion does not follow the Maven syntax, see https://github.com/eclipse-m2e/m2e-core/issues/286.

Target definition XML with an exclude element

This is for your information only, this exercise does not need an exclusion.

If you need more assistance we offer Online Training and Onsite training as well as consulting

Home Tutorials Training Consulting Books Company Contact us


Get more...