Difference between revisions of "How to Create a new OSGi Bundle"

From Opentaps Wiki
Jump to navigationJump to search
Line 55: Line 55:
Now my <tt>notes/domain</tt> bundle is set up.  I create the directory structure and move my <tt>Note.java</tt> over:
Now my <tt>notes/domain</tt> bundle is set up.  I create the directory structure and move my <tt>Note.java</tt> over:
$ git mv modules/notes/api/services/src/main/java/org/opentaps/notes/domain/Note.java modules/notes/domain/src/main/java/
$ git mv modules/notes/api/services/src/main/java/org/opentaps/notes/domain/Note.java modules/notes/domain/src/main/java/org/opentaps/notes/domain/

Revision as of 22:59, 24 February 2012

While writing Customizing opentaps 2: an OSGi Tutorial, I realized that one of the Java classes was in the wrong place. Note.java, 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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


    <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 Note.java over:

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