gradlekaraf

How to create Karaf shell command in Gradle project


I'm trying to create own Karaf shell command and I'm willing to do this using Gradle because entire project is on Gradle.

The only relevant documentation I've found so far is here http://karaf.apache.org/manual/latest/#_shell_commands and it says

See [examples/karaf-command-example] to add your own shell commands.

Examples are created using MAVEN and there is absolutely no description about what magic is happening there and what should appear in resulting bundle so that I could reproduce it in Gradle.

Can someone tell how to achieve the same in Gradle project?
How Karaf identifies which commands are present in Bundle?


Solution

  • Over time I did not find how to make that same thing in gradle, but I came across the solution which gives same result - using OSGI Declarative Services.

    More details here (look for osgi.command.scope):
    http://blog.vogella.com/2016/09/26/configuring-osgi-declarative-services/

    Here is sample code, which allows running command some:command with one optional string parameter.

    package some.application;
    
    import org.osgi.service.component.annotations.Component;
    
    @Component(
        service=SomeCommand.class,
        property = {"osgi.command.scope=some", "osgi.command.function=command"}
    )
    public class SomeCommand {
    
        public void command() {
            System.out.println("SomeCommand: " + "<no args>");
        }
    
        public void command(String param) {
            System.out.println("SomeCommand: " + param);
        }
    }