CLI を使用する

CLI をインストールしたら、spring と入力してコマンドラインで Enter キーを押すと、CLI を実行できます。引数なしで spring を実行すると、次のようなヘルプ画面が表示されます。

$ spring
usage: spring [--help] [--version]
       <command> [<args>]

Available commands are:

  init [options] [location]
    Initialize a new project using Spring Initializr (start.spring.io)

  encodepassword [options] <password to encode>
    Encode a password for use with Spring Security

  shell
    Start a nested shell

Common options:

  --debug Verbose mode
    Print additional status information for the command you are running


See 'spring help <command>' for more information on a specific command.

次の例に示すように、spring help と入力して、サポートされているコマンドの詳細を取得できます。

$ spring help init
spring init - Initialize a new project using Spring Initializr (start.spring.io)

usage: spring init [options] [location]

Option                       Description
------                       -----------
-a, --artifact-id <String>   Project coordinates; infer archive name (for
                               example 'test')
-b, --boot-version <String>  Spring Boot version (for example '1.2.0.RELEASE')
--build <String>             Build system to use (for example 'maven' or
                               'gradle') (default: maven)
-d, --dependencies <String>  Comma-separated list of dependency identifiers to
                               include in the generated project
--description <String>       Project description
-f, --force                  Force overwrite of existing files
--format <String>            Format of the generated content (for example
                               'build' for a build file, 'project' for a
                               project archive) (default: project)
-g, --group-id <String>      Project coordinates (for example 'org.test')
-j, --java-version <String>  Language level (for example '1.8')
-l, --language <String>      Programming language  (for example 'java')
--list                       List the capabilities of the service. Use it to
                               discover the dependencies and the types that are
                               available
-n, --name <String>          Project name; infer application name
-p, --packaging <String>     Project packaging (for example 'jar')
--package-name <String>      Package name
-t, --type <String>          Project type. Not normally needed if you use --
                               build and/or --format. Check the capabilities of
                               the service (--list) for more details
--target <String>            URL of the service to use (default: https://start.
                               spring.io)
-v, --version <String>       Project version (for example '0.0.1-SNAPSHOT')
-x, --extract                Extract the project archive. Inferred if a
                               location is specified without an extension

examples:

    To list all the capabilities of the service:
        $ spring init --list

    To creates a default project:
        $ spring init

    To create a web my-app.zip:
        $ spring init -d=web my-app.zip

    To create a web/data-jpa gradle project unpacked:
        $ spring init -d=web,jpa --build=gradle my-dir

version コマンドを使用すると、次のように、使用している Spring Boot のバージョンを簡単に確認できます。

$ spring version
Spring CLI v3.3.1

新しいプロジェクトを初期化する

init コマンドを使用すると、次の例に示すように、シェルを移動せずに start.spring.io を使用して新しいプロジェクトを作成できます。

$ spring init --dependencies=web,data-jpa my-project
Using service at https://start.spring.io
Project extracted to '/Users/developer/example/my-project'

上記の例では、spring-boot-starter-web と spring-boot-starter-data-jpa を使用する Maven ベースのプロジェクトで my-project ディレクトリを作成します。次の例に示すように、--list フラグを使用して、サービスの機能をリストできます。

$ spring init --list
=======================================
Capabilities of https://start.spring.io
=======================================

Available dependencies:
-----------------------
actuator - Actuator: Production ready features to help you monitor and manage your application
...
web - Web: Support for full-stack web development, including Tomcat and spring-webmvc
websocket - Websocket: Support for WebSocket development
ws - WS: Support for Spring Web Services

Available project types:
------------------------
gradle-build -  Gradle Config [format:build, build:gradle]
gradle-project -  Gradle Project [format:project, build:gradle]
maven-build -  Maven POM [format:build, build:maven]
maven-project -  Maven Project [format:project, build:maven] (default)

...

init コマンドは多くのオプションをサポートしています。詳細については、help の出力を参照してください。たとえば、次のコマンドは、Java 17 および war パッケージングを使用する Gradle プロジェクトを作成します。

$ spring init --build=gradle --java-version=17 --dependencies=websocket --packaging=war sample-app.zip
Using service at https://start.spring.io
Content saved to 'sample-app.zip'

組み込みシェルの使用

Spring Boot には、BASH および zsh シェル用のコマンドライン補完スクリプトが含まれています。これらのシェルのいずれも使用しない場合(おそらく Windows ユーザー)、次の例に示すように、shell コマンドを使用して統合シェルを起動できます。

$ spring shell
Spring Boot (v3.3.1)
Hit TAB to complete. Type \'help' and hit RETURN for help, and \'exit' to quit.

組み込みシェル内から、他のコマンドを直接実行できます。

$ version
Spring CLI v3.3.1

埋め込みシェルは、ANSI カラー出力と tab 補完をサポートしています。ネイティブコマンドを実行する必要がある場合は、! プレフィックスを使用できます。組み込みシェルを終了するには、ctrl-c を押します。