此版本仍在开发中,尚未视为稳定版。如需最新稳定版本,请使用 Spring Boot 4.0.4spring-doc.cadn.net.cn

使用命令行界面

一旦你安装了 CLI,就可以在命令行中输入 spring 并按回车键来运行它。 如果你在不带任何参数的情况下运行 spring,将会显示一个帮助界面,如下所示:spring-doc.cadn.net.cn

$ 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-doc.cadn.net.cn

$ 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-doc.cadn.net.cn

$ spring version
Spring CLI v4.0.5-SNAPSHOT

初始化新项目

init 命令允许您在不离开 shell 的情况下,使用 start.spring.io 创建一个新项目,如下例所示:spring-doc.cadn.net.cn

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

前面的示例会创建一个名为 my-project 的目录,其中包含一个基于 Maven 的项目,该项目使用了 spring-boot-starter-webspring-boot-starter-data-jpa。 你可以通过使用 --list 标志来列出该服务的功能,如下例所示:spring-doc.cadn.net.cn

$ 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-doc.cadn.net.cn

$ 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'

使用嵌入式 Shell

Spring Boot 为 BASH 和 zsh shell 提供了命令行自动补全脚本。 如果你未使用这两种 shell(例如你是 Windows 用户),可以使用 shell 命令启动一个集成 shell,如下例所示:spring-doc.cadn.net.cn

$ spring shell
Spring Boot (v4.0.5-SNAPSHOT)
Hit TAB to complete. Type \'help' and hit RETURN for help, and \'exit' to quit.

在嵌入式 shell 内部,您可以直接运行其他命令:spring-doc.cadn.net.cn

$ version
Spring CLI v4.0.5-SNAPSHOT

嵌入式 shell 支持 ANSI 彩色输出以及 tab 自动补全。 如果需要运行原生命令,可以使用 ! 前缀。 要退出嵌入式 shell,请按 ctrl-cspring-doc.cadn.net.cn