入門
Spring Shell が提供するものを確認するために、単純なコマンドを持つ単純な hello world シェルアプリケーションを作成します。
プロジェクトの作成
この簡単なチュートリアルでは、次のコマンドを使用してプロジェクトを複製してビルドします。
$>git clone [email protected] (英語) :spring-projects/spring-shell.git
$>cd spring-shell
$>./mvnw install -DskipTests最初のコマンド
お気に入りの IDE で、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 + "!");
}
} このクラスは、オプションの引数 name を受け取り、標準出力に挨拶メッセージを出力する、hello という名前の単一のコマンドを定義します。
メインメソッドは Spring アプリケーションコンテキストをブートストラップし、シェルを実行します。アプリケーションを実行するには、次のコマンドで SpringShellApplication クラスを実行します。
./mvnw -pl org.springframework.shell:spring-shell-sample-hello-world exec:java -Dexec.mainClass=org.springframework.shell.samples.helloworld.SpringShellApplicationこれによりシェルが起動し、プロンプトが表示され、次のようなコマンドを入力できるようになります。
$>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おめでとうございます。最初の Spring Shell アプリケーションを作成して実行しました。