Migrate shell commands from Karaf 3 to Karaf 4

Today the migration from Karaf 3 to version 4 brings some new interesting effects. One of them is a full yellow 'blinking' source code where shell commands are implemented.
It looks like all the shell interfaces from version 3 are deprecated now. The reason is that the developers want to define commands without using blueprint definition files in the OSGI INF folder any more. But to establish the new way a new interface is created and in focus.
To use the new interface you first  have to change the maven configuration of your project. Add the following parameters:
 <felix.plugin.version>3.0.1</felix.plugin.version>
 <maven.version>2.0.9</maven.version>
And the following parts inside your main pom.xml:
<dependencyManagement>
 <dependencies>
 <dependency>
 <groupId>org.apache.felix</groupId>
 <artifactId>maven-bundle-plugin</artifactId>
 <version>${felix.plugin.version}</version>
 </dependency>
 <dependency>
 <groupId>org.apache.maven</groupId>
 <artifactId>maven-plugin-api</artifactId>
 <version>${maven.version}</version>
 </dependency>
 </dependencies>
 </dependencyManagement>
 <pluginManagement>
 <plugins>
 <plugin>
 <groupId>org.apache.karaf.tooling</groupId>
 <artifactId>karaf-services-maven-plugin</artifactId>
 <version>${karaf.version}</version>
 <executions>
 <execution>
 <id>service-metadata-generate</id>
 <phase>process-classes</phase>
 <goals>
 <goal>service-metadata-generate</goal>
 </goals>
 </execution>
 </executions>
 </plugin>
 </plugins> 
 </pluginManagement>
Now you need to add the following build instruction to every sub project into the build/plugin part of the pom.xml:
 <plugin>
 <groupId>org.apache.karaf.tooling</groupId>
 <artifactId>karaf-services-maven-plugin</artifactId>
 </plugin>
This was the basic configuration to instruct maven to build everything right. Now you can remove the old blueprint.xml files because they are no more in use.
To create or update command add the following imports:
import org.apache.karaf.shell.api.action.Action;
import org.apache.karaf.shell.api.action.Argument;
import org.apache.karaf.shell.api.action.Command;
import org.apache.karaf.shell.api.action.Option;
Mark the class as component and service and enhance 'Action':
@Command(scope = "test", name = "cmd", description = "Test Command")
@Service
public class CmdTest implements Action {
The old interface had a method 'execute(Session)' but the new one is only 'execute()'. The parameter is left. To have access to the session you need to add a reference variable like this:
@Reference
private Session session;
after building and deploying into karaf engine the command is available as usually.

Kommentare

Beliebte Posts aus diesem Blog

Sonatype Nexus fails with random "peer not authenticated" errors behind ingress

[mhus lib] Reorg in generation 7 nearly finished

Creating a flux sync configuration referring a config map for substitution