...
toptop Anchor
6.1. Extending the console
...
Using the command line, we can create our project:
...
This generate the main pom.xml
and some additional packages.
...
You can also use the interactive mode for creating the skeleton project:
...
Use the following values when prompted:
...
Manual creation
Alternatively, you can simply create the directory shell-sample-commands
and create the pom.xml
file inside it:
...
...
Dependencies
We need to tell maven which libraries our project depends on. In the dependencies
section of the pom, add the following one:
...
...
This dependency is needed to have access to the base classes that are used to define commands.
...
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.
...
...
Loading the project in your IDE
...
Inside the project, run the following command
...
or
...
...
The project files for your IDE should now be created. Just open the IDE and load the project.
...
We can now create the command class HelloShellCommand.java
...
...
Creating the associated blueprint configuration files
...
If you don't have the src/main/resources
directory yet, create it.
...
Then, re-generate the IDE project files and reload it so that this folder is now recognized as a source folder.
Inside this directory, create the OSGI-INF/blueprint/
directory and put the following file inside (the name of this file has no impact at all):
...
...
Compiling the jar
Let's try to build the jar. Remove the test classes and sample classes if you used the artifact, then from the command line, run:
...
...
The end of the maven output should look like:
...
...
Turning the jar into an OSGi bundle
OSGi bundles are jars but they require some manifest headers to be correctly recognized. We will leverage Felix's manven maven plugin to easily generate those.
Lets turn it into a bundle: modify the line in the pom.xml
to adjust the packaging:
...
...
Add the following section at the bottom of the pom.xml
, in the existing build/plugins
section:
...
...
The Import-Package
is required to make sure our bundle will import the org.osgi.service.command
package so that the service will be correctly seen in Felix.
...
Launch a Karaf instance and run the following command to install the newly created bundle:
...
Let's try running the command:
...
...
and for the link:
Yeah
Wiki Markup |
---|
{scrollbar} |
...
Command completer
A completer allow you to automatically complete a command argument using <tab>. A completer is simply a bean which is injected to a command.
Of course to be able to complete it, the command should require an argument.
Command argument
We add an argument to the HelloCommand:
...
The Blueprint configuration file is the same as previously.
Completer bean
A completer is a bean which implements the Completer interface:
...
Blueprint configuration file
Using Blueprint, you can "inject" the completer linked to your command. The same completer could be used for several commands and a command can have several completers:
...
Test in Karaf
Launch a Karaf instance and run the following command to install the newly created bundle:
...
Let's try running the command:
...
...