Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring boot remote shell custom command

Tags:

java

shell

spring

I try to add a new custom command to the spring boot remote shell without success. In the documentation is only a groovy example available but I like to use Java do create a new command.

http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-remote-shell.html I also check the CRaSH documentation: http://www.crashub.org/1.3/reference.html#_java_commands

I put my class under package commands and crash.commands but I don't see my new command if I connect to the shell via ssh and type help. Any idea what I'm doing wrong?

Here is my Java Code:

package commands;

import org.crsh.cli.Command;
import org.crsh.cli.Usage;
import org.crsh.command.BaseCommand;
import org.springframework.stereotype.Component;

@Component
public class hello extends BaseCommand {

  @Command
  @Usage("Say Hello")
  public String test() {
    return "HELLO";
  }
} 
like image 371
Manuel Avatar asked Oct 05 '14 09:10

Manuel


2 Answers

If your class is called 'hello', put it in

resources/crash/commands/hello/hello.java

Having a package statement doesn't matter. You don't have to put anything inside src, just have this inside resources.

@Component isn't required.

This class is compiled during runtime. See:

org.crsh.lang.impl.java.JavaCompiler#compileCommand

Therefor any dependency injection requirements need to be considered accordingly with lazy initialization.

like image 150
Ravindranath Akila Avatar answered Oct 02 '22 19:10

Ravindranath Akila


Put the class in the default package (i.e. omit the package statement) and put the Java source for the class in src/main/resources/commands. Spring/Crash will compile the Java code the first time you invoke the command. Also, if the class implements a single command the method name should be ‘main’ otherwise users have to type:

> hello test

You also don't need the @Component annotation on the class as it is not a Spring managed bean.

like image 31
Haywood Slap Avatar answered Oct 02 '22 17:10

Haywood Slap