Skip to content

Create OSGi bundle and use it in Equinox app

Andrey Hihlovskiy edited this page Apr 22, 2014 · 32 revisions

Provided that we already prepared Equinox app for multiproject build, we can now create OSGi-bundle and use it in Equinox app.

  1. Create folder "tutorials/MyBundle", create file "build.gradle" in it, insert code:
apply plugin: 'java'
apply plugin: 'osgi-bundle'
  1. Create folder "tutorials/MyBundle/src/main/java/mybundle", create file "HelloWorld.java" in it, insert code:
package mybundle;

public class HelloWorld {

  public static void sayHello() {
    System.out.println("Hello, world!");
  }
}
  1. Edit file "tutorials/settings.gradle", insert code:
include 'MyBundle'

so that there are two includes - "MyEquinoxApp" and "MyBundle".

  1. Edit file "tutorials/MyEquinoxApp/build.gradle", insert code:
dependencies {
  compile project(':MyBundle')
}
  1. Edit file "tutorials/MyEquinoxApp/src/main/java/myequinoxapp/Application.java", replace line containing System.out.println with mybundle.HelloWorld.sayHello(); so that the file looks like this:
package myequinoxapp;

import java.io.IOException;

import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;

public class Application implements IApplication {

  @Override
  public Object start(IApplicationContext ctx) throws Exception {
    mybundle.HelloWorld.sayHello();
    return IApplication.EXIT_OK;
  }

  @Override
  public void stop() {
    // From eclipse doc:
    // This method will not be called if an application exits normally from the start(IApplicationContext) method. 
  }
}
  1. Invoke on command line in "tutorials" folder:
gradle build

CHECK: folder "tutorials/MyBundle/build/libs" contains file "MyBundle-1.0.0.0.jar", which is proper OSGi bundle with automatically generated manifest.

CHECK: Each product in "tutorials/MyEquinoxApp/build/output" contains "MyBundle" and "MyEquinoxApp" bundles in "plugins" subfolder and in "configuration/config.ini".

CHECK: The product matching your OS/architecture is runnable and prints "Hello, world!" to the console.

The example code corresponding to this page is located in tutorialExamples/EquinoxApp-4.

Now we can move on to create first RCP app.

Clone this wiki locally