How to Create a new OSGi Bundle
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"> <modelVersion>4.0.0</modelVersion> <artifactId>notes.domain</artifactId> <name>Notes domain</name> <packaging>bundle</packaging> <build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> </plugin> </plugins> </build> <parent> <groupId>org.opentaps</groupId> <version>2.0.1-SNAPSHOT</version> <artifactId>notes</artifactId> </parent> </project>
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:
<modules> <module>domain</module> <module>api</module> <module>impl</module> <module>rest</module> <module>rest_ru</module> <module>rest_fr</module> <module>eba</module> </modules>