Even though the title mentions about WSO2 Carbon, the steps that I've explained can be used to develop generic OSGi bundles which are compliant with OSGi R4 specification. WSO2 Carbon is the platform that I am using here to deploy the bundles. WSO2 Carbon is the industry's first SOA middleware platform based on OSGi.
If you read this post till the end, you will understand how to develop OSGi bundles using Maven bundle plugin and deploying them in WSO2 Carbon.
Obviously, the first step is to generate a Maven 2 project. One option is to create from the scratch and the second option is to use the Maven archetype plugin. Since the former is time-consuming, I am using the latter here.
Generate the Maven 2 project
mvn archetype:create -DarchtetypeGroupId=org.apache.maven.archetypes
-DgroupId=org.wso2.carbon -DartifactId=org.wso2.carbon.helloworldChanges to generated Maven 2 project
- Make another directory under "src/main/java/org/wso2/carbon/" with the name helloword.
- Move the App.java file located inside the "src/main/java/org/wso2/carbon/" folder to "src/main/java/org/wso2/carbon/helloworld" folder
- Replace source of the App.java file with the following.
package org.wso2.carbon.helloworld;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
/**
* Hello world!
*/
public class App implements BundleActivator {
public void start(BundleContext context) throws Exception {
System.out.println("Hello World!");
}
public void stop(BundleContext context) throws Exception {
System.out.println("Goodbye World!");
}
}You need to modify the generated pom.xml as well
Please note the changes done in the following pom.xml file. If you are interested in Maven bundle plugin, please refer this article on WSO2 Oxygen tank
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.helloworld</artifactId>
<packaging>bundle</packaging>
<version>1.0.0</version>
<name>org.wso2.carbon.helloworld</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.eclipse.osgi</groupId>
<artifactId>org.eclipse.osgi</artifactId>
<version>3.5.0.v20090520</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>1.4.0</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${pom.name}</Bundle-Name>
<Bundle-Activator>org.wso2.carbon.helloworld.App</Bundle-Activator>
<Private-Package>org.wso2.carbon.helloworld</Private-Package>
<Import-Package>org.osgi.framework.*</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>wso2-maven2-repository</id>
<name>WSO2 Maven2 Repository</name>
<url>http://dist.wso2.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</releases>
</repository>
</repositories>
</project>
Building the OSGi bundle.
mvn installGenerated OSGi bundle(org.wso2.carbon.helloworld-1.0.0.jar) is placed in the target folder of the project. Observe the content of the bundle, specially the MANIFEST.MF file. It contains the OSGi manifest headers
Starting WSO2 Carbon
If you have correctly followed above steps, now we are in a position to deploy the generated bundle in WSO2 Carbon. Download the Carbon distribution from the project site. Then unzip the it to a preferred location. Go to the "bin" directory and execute the following command. Here "-DosgiConsole=19443" option is used to allow telnet access to the Equinox OSGi console.
Linux
sh wso2server.sh -DosgiConsole=19443
Windows
wso2server -DosgiConsole=19443Now in a separate window, execute the following command to access the OSGi console
telnet localhost 19443Deploying the generated OSGi bundle
Execute the following command to install the bundle into the Carbon instance
osgi> install file:/home/sameera/wso2/testing/maven/archtype/org.wso2.carbon.helloworld/target/org.wso2.carbon.helloworld-1.0.0.jar
Bundle id is 267
Using the given bundle id, now we can start and stop the helloworld bundle
osgi> start 267
Hello World!
osgi> stop 267
Goodbye World!
Are you still following this blog post? Great!!! now you know how to develop and deploy OSGi bundles into WSO2 Carbon.

