How to Create a Command for Apache Karaf

1) Create the command class:

package br.eti.fernandoribeiro.sample;

import org.apache.felix.gogo.commands.Command;
import org.apache.karaf.shell.console.OsgiCommandSupport;

@Command(scope = "simplesassim", name = "sample")
public final class SampleCommand extends OsgiCommandSupport {

  @Override
  protected Object doExecute() {
    return null;
  }

}

2) Create the sample.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
  <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.0.0">
    <command name="simplesassim/sample"> <!-- As per KARAF-1001, this will be only a bean ID in 3.0 -->
      <action class="br.eti.fernandoribeiro.sample.SampleCommand" />
    </command>
  </command-bundle>
</blueprint>

3) Build the bundle.

4) Install the bundle.

5) Run the command:

simplesassim:sample

A sample is available here.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s