2

I already managed to start Spring Shell using Spring Boot:

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class);
    }
}

All my @ShellComponent classes are detected and I can use the shell as expected.

Now I would like to run the shell without Spring Boot, I expect it to look something like this

Shell shell = context.getBean(Shell.class);
shell.run(...);

What approach should I take to configure all required dependencies myself?

Thanks in advance!

Stefan
  • 418
  • 5
  • 13

3 Answers3

3

By extracting the necessary parts of ebottard's link (Thank you!) I finally managed to run the shell like I wanted:

@Configuration
@Import({
        SpringShellAutoConfiguration.class,
        JLineShellAutoConfiguration.class,

        JCommanderParameterResolverAutoConfiguration.class,
        StandardAPIAutoConfiguration.class,

        StandardCommandsAutoConfiguration.class,
})
public class SpringShell {

    public static void main(String[] args) throws IOException {
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringShell.class);
        Shell shell = context.getBean(Shell.class);
        shell.run(context.getBean(InputProvider.class));
    }

    @Bean
    @Autowired
    public InputProvider inputProvider(LineReader lineReader, PromptProvider promptProvider) {
        return new InteractiveShellApplicationRunner.JLineInputProvider(lineReader, promptProvider);
    }
}
Stefan
  • 418
  • 5
  • 13
  • I would be very interested in you solution. I got it to compile and execute. It even reacts to a customized PromptProvider, but commands annotated with @ShellComponent/@ShellMethod are not recognized. I checked if these commands are in a scanned package. – Johann Heinzelreiter Feb 23 '21 at 15:13
1

See this example that shows how to wire up everything without relying on Autoconfiguration.

ebottard
  • 1,997
  • 2
  • 12
  • 14
0

without Spring Boot I write this:

@Configuration
@ComponentScan(value = {"path.to.commands", "org.springframework.shell.commands", "org.springframework.shell.converters", "org.springframework.shell.plugin.support"})
public class TestShell {
private static String[] args;

@Bean("commandLine")
public CommandLine getCommandLine() throws IOException {
    return SimpleShellCommandLineOptions.parseCommandLine(args);
}

@Bean("shell")
public JLineShellComponent getShell() {
    return new JLineShellComponent();
}

public static void main(String[] args) {
    TestShell.args = args;
    System.setProperty("jline.terminal", "none");

    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(TestShell.class);
    ctx.registerShutdownHook();

    JLineShellComponent shell = ctx.getBean(JLineShellComponent.class);

    shell.start();
    shell.waitForComplete();
    ExitShellRequest exitShellRequest = shell.getExitShellRequest();
    if (exitShellRequest == null)
        exitShellRequest = ExitShellRequest.NORMAL_EXIT;
    System.exit(exitShellRequest.getExitCode());
}

}

and command class:

@Component
public class Hello implements CommandMarker {
@CliCommand(value="hi", help = "say hello.")
public String hi() {
    return "hello";
}

}

see org.springframework.shell.Bootstrap.

pwipo
  • 463
  • 3
  • 7