How to Create a new OSGi Bundle

From Opentaps Wiki
Revision as of 22:58, 24 February 2012 by Sichen (talk | contribs)
Jump to navigationJump to search

While writing Customizing opentaps 2: an OSGi Tutorial, I realized that one of the Java classes was in the wrong place., which is the object representation of a note, is a domain class under the Domain Driven Architecture. Since domain classes represent data, not actions, it shouldn't be part of the services API bundle, so I'm going to move it into a new bundle for a domain class. In the process, I thought this would be a good tutorial on how to create an OSGi bundle.

First, I create a directory for my bundle. Under the opentaps-2/modules/notes, I will add a new directory called domain, parallel to api and impl.

Next, I create a directory within this bundle for my Java files: src/main/java under the opentaps-2/modules/notes/domain directory. This is where the Java source files go. If there were other types of source files, like web.xml for webapp configuration or .properties files for localizations, then I would also need a src/main/resources, but I don't have those right now, so I'm not going to create this.

Next, I need a pom.xml for maven in the domain/ directory. I will borrow from the one for the api bundle and modify it:

<project xmlns="" xmlns:xsi=""


    <name>Notes domain</name>



Let's look at this pom.xml. There are a few things you should notice:

  • The packaging is bundle because we're trying to get a bundle built, whether it's a jar or war file. If we're just using this to manage other bundles, then it would be pom.
  • It does not have its own groupId or version because they are the same as the one for the parent. Putting these tags again would cause an error when you try to build.

We'll also want to add our new bundle to its parent, notes/pom.xml:


Now my notes/domain bundle is set up. I create the directory structure and move my over:

$ git mv modules/notes/api/services/src/main/java/org/opentaps/notes/domain/ modules/notes/domain/src/main/java/