最新の安定バージョンについては、Spring Shell 3.4.0 を使用してください!

入門

Spring Shell が提供するものを確認するために、単純な引数を持つ単純な hello world シェルアプリケーションを作成できます。

Spring ShellSpring Boot 3.3.6 および Spring Framework 6.1.15 に基づいているため、JDK 17 が必要です。

プロジェクトの作成

このチュートリアルでは、Spring Shell 依存関係を選択できる start.spring.io を使用して単純な Spring Boot アプリケーションを作成します。この最小限のアプリケーションは、spring-boot-starter と spring-shell-starter のみに依存します。

通常、start.spring.io の Spring Shell バージョンは最新リリースです。

maven を使用すると、次のようなことが期待されます。

<properties>
    <spring-shell.version>3.3.4</spring-shell.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.shell</groupId>
        <artifactId>spring-shell-starter</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.shell</groupId>
            <artifactId>spring-shell-dependencies</artifactId>
            <version>${spring-shell.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

gradle を使用すると、次のようなことが期待されます。

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.shell:spring-shell-starter'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.shell:spring-shell-dependencies:3.3.4"
    }
}
3.2.x までのバージョンではすべてのランナーがデフォルトで有効になっていましたが、3.3.x 以降では NonInteractiveShellRunner のみがデフォルトで有効になっています。つまり、REPL を取得するには InteractiveShellRunner を有効にする必要があります。
spring:
  shell:
    interactive:
      enabled: true
この依存関係が存在するために Spring Shell が REPL (Read-Eval-Print-Loop) を開始する場合、このチュートリアル全体でビルド (-DskipTests) するときにテストをスキップするか、start.spring.io によって生成されたサンプル統合テストを削除する必要があります。これを削除しないと、統合テストで Spring ApplicationContext が作成され、ビルドツールによっては、評価ループでスタックしたままになるか、NPE でクラッシュします。

コンパイルしたら、対話モードで実行できます。

$ $JAVA_HOME/bin/java -jar demo-0.0.1-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v3.3.6)

2022-09-13T18:42:12.818+01:00  INFO 12644 --- [           main] com.example.demo.DemoApplication
: Starting DemoApplication using Java 17.0.4 on ...
2022-09-13T18:42:12.821+01:00  INFO 12644 --- [           main] com.example.demo.DemoApplication
: No active profile set, falling back to 1 default profile: "default"
2022-09-13T18:42:13.606+01:00  INFO 12644 --- [           main] com.example.demo.DemoApplication
: Started DemoApplication in 1.145 seconds (process running for 1.578)
shell:>help
AVAILABLE COMMANDS

Built-In Commands
       help: Display help about available commands
       stacktrace: Display the full stacktrace of the last error.
       clear: Clear the shell screen.
       quit, exit: Exit the shell.
       history: Display or save the history of previously run commands
       version: Show version info
       script: Read and execute commands from a file.

または非対話モード:

$JAVA_HOME/bin/java -jar demo-0.0.1-SNAPSHOT.jar help

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v3.3.6)

2022-09-13T18:42:12.818+01:00  INFO 12644 --- [           main] com.example.demo.DemoApplication
: Starting DemoApplication using Java 17.0.4 on ...
2022-09-13T18:42:12.821+01:00  INFO 12644 --- [           main] com.example.demo.DemoApplication
: No active profile set, falling back to 1 default profile: "default"
2022-09-13T18:42:13.606+01:00  INFO 12644 --- [           main] com.example.demo.DemoApplication
: Started DemoApplication in 1.145 seconds (process running for 1.578)
AVAILABLE COMMANDS

Built-In Commands
       help: Display help about available commands
       stacktrace: Display the full stacktrace of the last error.
       clear: Clear the shell screen.
       quit, exit: Exit the shell.
       history: Display or save the history of previously run commands
       version: Show version info
       script: Read and execute commands from a file.
ログがログを作成して、シェルアプリでより適切に機能することを確認してください。

最初のコマンド

これで、最初のコマンドを追加できます。これを行うには、新しいクラス (好きな名前を付けます) を作成し、@Component のバリエーションである @ShellComponent でアノテーションを付けます。これは、候補コマンドをスキャンするクラスのセットを制限するために使用されます。

次に、String を引数として取り、それを "Hello world" で返す helloWorld メソッドを作成できます。@ShellMethod を追加し、必要に応じて key パラメーターを使用してコマンド名を変更します。コマンドの実行時に引数のデフォルト値が指定されていない場合は、@ShellOption を使用して引数のデフォルト値を定義できます。

import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.ShellOption;

@ShellComponent
public class MyCommands {

	@ShellMethod(key = "hello-world")
	public String helloWorld(
		@ShellOption(defaultValue = "spring") String arg
	) {
		return "Hello world " + arg;
	}
}

新しい hello-world コマンドが役立つようになりました。

My Commands
       hello-world:

そして、それを実行することができます:

shell:>hello-world
Hello world spring

shell:>hello-world --arg boot
Hello world boot

このドキュメントの残りの部分では、Spring Shell プログラミングモデル全体をより深く掘り下げます。