...
Code Block |
---|
mvn archetype:create \ -DarchetypeArtifactId=maven-archetype-quickstart \ -DgroupId=org.apache.felix.karaf.shell.samples \ -DartifactId=shell-sample-commmandscommands \ -Dversion=1.0-SNAPSHOT |
This generate the main pom.xml
and some additional packages.
...
Code Block |
---|
Choose a number: (1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36) 15: : 15 Define value for groupId: : org.apache.felix.karaf.shell.samples Define value for artifactId: : shell-sample-commmandscommands Define value for version: 1.0-SNAPSHOT: : Define value for package: : org.apache.felix.karaf.shell.samples |
...
Alternatively, you can simply create the directory shell-sample-commmandscommands
and create the pom.xml
file inside it:
...
This dependency is needed to have access to the base classes that are used to define commands.
Configuring for Java 5
We are using annotations to define commands, so we need to ensure maven will actually use JDK 1.5 to compile the jar.
Just add the following snippet after the dependencies
section.Additionally, we need the ConfigAdmin service, which is in the OSGi Compendium jar:
Code Block | ||
---|---|---|
| ||
<build> <dependency> <plugins> <plugin> <groupId>org.apache.felix<maven.plugins</groupId> <artifactId>org.osgi.compendium<<artifactId>maven-compiler-plugin</artifactId> <configuration> <version>1.0.0</version> <target>1.5</target> <source>1.5</source> </configuration> <scope>provided<</scope>plugin> </dependency>plugins> </build> |
Loading the project in your IDE
...
Code Block | ||||
---|---|---|---|---|
| ||||
package org.apache.felix.karaf.shell.samples; import org.apache.felix.gogo.commands.Command; import org.apache.felix.gogo.commands.Option; import org.apache.felix.gogo.commands.Argument; import org.apache.felix.karaf.shell.console.OsgiCommandSupport; @Command(scope = "test", name = "hello", description="Says hello") public class HelloShellCommand extends OsgiCommandSupport { @Override protected Object doExecute() throws Exception { ioSystem.out.println("Executing Hello command"); return null; } } |
...
Add the following section at the bottom of the pom.xml
, in the existing build/plugins
section:
Code Block | ||
---|---|---|
| ||
<build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <version>2.0.0</version> <extensions>true</extensions> <configuration> <instructions> </instructions> </configuration> <dependencies> <dependency> <groupId> </dependency> </dependencies> </plugin> </plugins> </build> |
Let's compiled it again using the mvn install
command.
Test in Karaf
Launch a ServiceMix Kernel Karaf instance and run the following command to install are the newly created bundle:
Code Block |
---|
karaf@root> osgi:install -s mvn:org.apache.felix.karaf.shell.samples/shell-sample-commands/1.0-SNAPSHOT
|
Let's try running the command:
Code Block |
---|
servicemix>karaf@root> test/:hello Executing Hello command |
...