|
This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Shell 3.4.1! |
Getting Started
To see what Spring Shell has to offer, we will write a trivial hello world shell application that has a simple command.
Creating a Project
For the purpose of this quick tutorial, clone and build the project with the following commands:
$>git clone [email protected]:spring-projects/spring-shell.git
$>cd spring-shell
$>./mvnw install -DskipTests
Your First Command
In your favorite IDE, open the org.springframework.shell.samples.helloworld.SpringShellApplication:
@EnableCommand(SpringShellApplication.class)
public class SpringShellApplication {
public static void main(String[] args) throws Exception {
ApplicationContext context = new AnnotationConfigApplicationContext(SpringShellApplication.class);
ShellRunner runner = context.getBean(ShellRunner.class);
runner.run(args);
}
@Command(name = "hello", description = "Say hello to a given name", group = "Greetings",
help = "A command that greets the user with 'Hello ${name}!'. Usage: hello [-n | --name]=<name>")
public void sayHello(@Option(shortName = 'n', longName = "name", description = "the name of the person to greet",
defaultValue = "World") String name) {
System.out.println("Hello " + name + "!");
}
}
This class defines a single command, hello, that takes an optional argument, name, and prints a greeting message to standard output.
The main method bootstraps a Spring application context and runs the shell. To run the application, execute the SpringShellApplication class with the following command:
./mvnw -pl org.springframework.shell:spring-shell-sample-hello-world exec:java -Dexec.mainClass=org.springframework.shell.samples.helloworld.SpringShellApplication
This will start the shell, and you should see a prompt and be able to type commands like this:
$>help
Available commands:
Built-In Commands
clear: Clear the terminal screen
help: Display help about available commands
version: Show version info
Greetings
hello: Say hello to a given name
$>hello --name=foo
Hello foo!
$>exit
Exiting the shell
Congratulations, you have created and run your first Spring Shell application!