Getting Started

To see what Spring Shell has to offer, we will write a trivial hello world shell application that has a simple command.spring-doc.cn

Creating a Project

For the purpose of this quick tutorial, clone and build the project with the following commands:spring-doc.cn

$>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:spring-doc.cn

@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 named hello, that takes an optional argument, name, and prints a greeting message to the standard output.spring-doc.cn

The main method bootstraps a Spring application context and runs the shell. To run the application, execute the SpringShellApplication class with the following command:spring-doc.cn

./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:spring-doc.cn

$>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!spring-doc.cn

Next Steps

The rest of this document delves deeper into the whole Spring Shell programming model.spring-doc.cn