https://docs.spring.io/spring-boot/docs/3.2.0/reference/htmlsingle/#cli
Spring Boot CLI 是一个命令行工具,你可以使用它从 start.spring.io 开始引导一个新项目或对密码进行编码。
安装 CLI
Spring Boot CLI(命令行界面)可以通过使用 SDKMAN!(软件开发工具包管理器)手动安装,或者如果你是 OSX 用户,可以使用 Homebrew 或 MacPorts 进行安装。
你不必使用 CLI 来使用 Spring Boot,但它是一个快速启动 Spring 应用程序的方式,无需使用集成开发环境(IDE)。
手动安装
可以从以下位置之一下载 Spring CLI 发行版:
下载后,请按照解压后的归档文件中的 INSTALL.txt
说明操作。简而言之,在 .zip
文件的 bin/
目录中有一个 spring
脚本(对于 Windows 是 spring.bat
)。或者,也可以使用 java -jar
与 .jar
文件一起使用(该脚本可以帮助确保类路径设置正确)。
使用 SDKMAN! 安装
SDKMAN!(软件开发工具包管理器)可用于管理各种二进制 SDK 的多个版本,包括 Groovy 和 Spring Boot CLI。从 sdkman.io 获取 SDKMAN!,并使用以下命令安装 Spring Boot:
$ sdk install springboot
$ spring --version
Spring CLI v3.2.0
如果为 CLI 开发功能并希望访问你构建的版本,请使用以下命令:
$ sdk install springboot dev /path/to/spring-boot/spring-boot-cli/target/spring-boot-cli-3.2.0-bin/spring-3.2.0/
$ sdk default springboot dev
$ spring --version
Spring CLI v3.2.0
上述说明安装了名为 dev
实例的本地 spring
实例。它指向你的目标构建位置,因此每次重新构建 Spring Boot 时,spring
都是最新的。
可以通过运行以下命令查看它:
$ sdk ls springboot
================================================================================
Available Springboot Versions
================================================================================
> + dev
* 3.2.0
================================================================================
+ - local version
* - installed
> - currently in use
================================================================================
OSX Homebrew 安装
如果你使用的是 Mac 并且使用 Homebrew,可以通过以下命令安装 Spring Boot CLI:
$ brew tap spring-io/tap
$ brew install spring-boot
Homebrew 将 spring
安装到 /usr/local/bin
目录。
如果没有找到该软件包,你的 Homebrew 安装可能已经过时。这种情况下,请运行 brew update
命令来更新 Homebrew,然后再试一次。
MacPorts安装
如果你使用的是 Mac 并且使用 MacPorts,可以通过以下命令安装 Spring Boot CLI:
$ sudo port install spring-boot-cli
命令行补全
Spring Boot CLI 包括为 BASH 和 zsh shell 提供命令补全功能的脚本。可以在任何 shell 中source
该脚本(也命名为 spring
),或者将其放入你的个人或系统范围内的 bash 补全初始化文件中。在 Debian 系统上,系统范围内的脚本位于 <installation location>/shell-completion/bash
,该目录下的所有脚本在新 shell 启动时都会被执行。例如,如果使用 SDKMAN! 进行安装,要手动运行该脚本,请使用以下命令:
$ . ~/.sdkman/candidates/springboot/current/shell-completion/bash/spring
$ spring <HIT TAB HERE>
grab help jar run test version
注意:如果使用 Homebrew 或 MacPorts 安装 Spring Boot CLI,命令行补全脚本将自动注册到你的 shell。
Windows Scoop 安装
如果你使用的是 Windows 并且使用 Scoop,可以通过以下命令安装 Spring Boot CLI:
> scoop bucket add extras
> scoop install springboot
注意:如果没有看到应用清单,你的 Scoop 安装可能已经过时。在这种情况下,运行 scoop update
并重试。
使用CLI
一旦安装了 CLI,可以通过在命令行输入 spring
并按 Enter 键来运行它。如果不带任何参数运行 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.2.0
初始化一个新项目
init
命令允许在不离开 shell 的情况下,使用 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'
前面的示例创建了一个名为 my-project
的目录,其中包含一个基于 Maven 的项目,使用了 spring-boot-starter-web
和 spring-boot-starter-data-jpa
。可以使用 --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
命令支持许多选项。有关更多详细信息,请查看帮助输出。例如,以下命令创建一个使用
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'
使用嵌入式 Shell
Spring Boot 包括适用于 BASH 和 zsh shells 的命令行补全脚本。如果你不使用这些 shell(或许你是 Windows 用户),可以使用 shell
命令启动集成的 shell,示例如下:
$ spring shell
Spring Boot (v3.2.0)
Hit TAB to complete. Type \'help' and hit RETURN for help, and \'exit' to quit.
在嵌入式 shell 内部,可以直接运行其他命令:
$ version
Spring CLI v3.2.0
嵌入式 shell 支持 ANSI 颜色输出以及tab
补全。如果需要运行原生命令,可以使用 !
前缀。要退出嵌入式 shell,请按 ctrl-c
。