Menu

Showing posts with label OSGi fragment. Show all posts
Showing posts with label OSGi fragment. Show all posts

How to create an OSGi fragment project?

Hello readers, today we are going to explain how to create an OSGi fragment project using Eclipse IDE.

The first step to create OSGi fragment is to understand the need of the OSGi fragment and select a host bundle using which this OSGi fragment is going to access and resolved within your application. To understand the OSGi fragment please go through by our previous OSGi fragment posts.

Once we have the clear picture about fragment and you have opted the host bundle to register the fragment then, follow the below steps to create OSGi fragment project. For example we have a sample OSGi bundle with name "OSGitestBundle" and for this bundle we will create an OSGi fragment which helps this bundle to work as an extension of host bundle and provide additional information to host bundle. 

Step 1: Go to File >> New >> Other >> Plug-in Development >> Fragment Project and then click on the next button. 

Select OSGi fragment project from other project section in Eclipse
Select OSGi fragment project from other project section in Eclipse

Step 2: Now put a valid name OSGi fragment name in Project name text-box, and click on the next button. for example we have given project  name OSGiFragment

OSGI fragment project name
OSGi fragment project name


Step 3: In this step you will see, few fields are auto filled with the name of your OSGi fragment project, as you have given the project name in the previous step. In this step you have to add the plug-in ID information or host name information of this fragment, so this get merge with host bundle. here we will put the project name OSGitestBundle and then click on the finish button.

Plug in or host name information OSGi fragment
Plug in or host name information OSGi fragment

Step 4: Now open the project explorer in your Eclipse and open the manifest file "MANIFEST.MF" at location [Fragment-PROJECT-NAME] >> META-INF >> MANIFEST.MF
Manifest file stored at directory
Manifest file location


In manifest file you will see the following entries:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: OSGiFragmen
Bundle-SymbolicName: OSGiFragmen
Bundle-Version: 1.0.0.qualifier
Fragment-Host: OSGitestBundle
Automatic-Module-Name: OSGiFragment
Bundle-RequiredExecutionEnvironment: JavaSE-1.8

Here Fragment-Host will hold the host bundle name.

OSGi fragment bundle

What is OSGi fragment?

OSGi fragment is the java archive file or jar file or bundle, which have some classes, resource and header. The purpose of fragment to develop an extension of a bundle, which add on additional functionality and merge those functionality with host bundle at the time of host bundle is getting resolve. Before host bundle get resolved fragment appended to host. Following are the key points which we need to keep in mind while working with OSGi fragment.


  • Fragment is treated as part of the host bundles. 
  • A fragment cannot have its own class loader or bundle activator. 
  • Fragment bundle cannot override the information present in the host bundles. 
  • Both host and fragment share the same class loader from host bundle. 
  • An OSGi fragment doesn't have it life-cycle and that means we can't activate, start, stop a fragment. 
  • All classes and definition of fragment are merged with host definition when the host bundle get resolved. If the fragment dependencies cannot be resolved, the fragment does not attach to the host bundle. 
  • If any information from host and fragment conflicts then fragment will not attach and merge with host bundle. 
  • Fragments extend host bundles with resources, classes, and permitted headers. 
  • Fragments can only add new files, and does not replace or override existing files from host. 
  • OSGi fragment JAR file contains manifest file which have host bundle information. 
  • Fragment jar cannot be use independently. 
  • Fragments bundles could you attached with one or many host bundles. 


Why do we have fragments and what is the role of fragments and why we use? 


  • To provide the extra classes or additional functionality to host bundle. 
  • Add configuration files and properties.

You can create a fragment project using eclipse.

To create an OSGi fragment project in Eclipse follow the below steps:
  1. Go to File >> New >> Other >> Plug-in Development >> Fragment Project
  2. Select Fragment project and click on the next.
  3. Put a valid project name for your fragment and then click on next button.
  4. Now under the Host plun-in section, put the host bundle name in plug-in ID field.

Here is the complete step by step guide to create an OSGi fragment project using Eclipse. >> https://rashidjorvee.blogspot.com/2019/03/how-to-create-osgi-fragment-project.html