On this page
67. 使用 CLI
安装 CLI 后,可以通过键入spring
并在命令行中按 Enter 来运行它。如果您不带任何参数运行spring
,则会显示一个简单的帮助屏幕,如下所示:
$ spring
usage: spring [--help] [--version]
<command> [<args>]
Available commands are:
run [options] <files> [--] [args]
Run a spring groovy script
... more command help is shown here
您可以键入spring help
以获取有关任何受支持命令的更多详细信息,如以下示例所示:
$ spring help run
spring run - Run a spring groovy script
usage: spring run [options] <files> [--] [args]
Option Description
------ -----------
--autoconfigure [Boolean] Add autoconfigure compiler
transformations (default: true)
--classpath, -cp Additional classpath entries
-e, --edit Open the file with the default system
editor
--no-guess-dependencies Do not attempt to guess dependencies
--no-guess-imports Do not attempt to guess imports
-q, --quiet Quiet logging
-v, --verbose Verbose logging of dependency
resolution
--watch Watch the specified file for changes
version
命令提供了一种快速的方法来检查您正在使用的 Spring Boot 版本,如下所示:
$ spring version
Spring CLI v2.1.1.RELEASE
67.1 使用 CLI 运行应用程序
您可以使用run
命令来编译和运行 Groovy 源代码。 Spring Boot CLI 是完全独立的,因此您不需要任何外部 Groovy 安装。
以下示例显示了用 Groovy 编写的“ hello world” Web 应用程序:
hello.groovy.
@RestController
class WebApplication {
@RequestMapping("/")
String home() {
"Hello World!"
}
}
要编译并运行该应用程序,请键入以下命令:
$ spring run hello.groovy
要将命令行参数传递给应用程序,请使用--
将命令与“ spring”命令参数分开,如以下示例所示:
$ spring run hello.groovy -- --server.port=9000
要设置 JVM 命令行参数,可以使用JAVA_OPTS
环境变量,如以下示例所示:
$ JAVA_OPTS=-Xmx1024m spring run hello.groovy
Note
在 Microsoft Windows 上设置JAVA_OPTS
时,请确保引用整个指令,例如set "JAVA_OPTS=-Xms256m -Xmx2048m"
。这样做可以确保将值正确传递给流程。
67.1.1 推论"grab"依赖性
标准 Groovy 包含@Grab
注解,该注解使您可以声明对第三方库的依赖关系。 Groovy 可以使用这种有用的技术以与 Maven 或 Gradle 相同的方式下载 jar,而无需使用构建工具。
Spring Boot 进一步扩展了该技术,并尝试根据您的代码推断出哪些库可以“抢”。例如,由于先前显示的WebApplication
代码使用@RestController
注解,因此 Spring Boot 会捕获“ Tomcat”和“ Spring MVC”。
以下各项用作“抓取提示”:
Items | Grabs |
---|---|
JdbcTemplate , NamedParameterJdbcTemplate , DataSource |
JDBC Application. |
@EnableJms |
JMS Application. |
@EnableCaching |
Caching abstraction. |
@Test |
JUnit. |
@EnableRabbit |
RabbitMQ. |
延伸Specification |
Spock test. |
@EnableBatchProcessing |
Spring Batch. |
@MessageEndpoint @EnableIntegration |
Spring Integration. |
@Controller @RestController @EnableWebMvc |
Spring MVC 嵌入式 Tomcat。 |
@EnableWebSecurity |
Spring Security. |
@EnableTransactionManagement |
SpringTransactionManagement。 |
Tip
请参阅 Spring Boot CLI 源代码中的CompilerAutoConfiguration的子类,以确切地了解如何应用定制。
67.1.2 推导"grab"坐标
Spring Boot 通过允许您指定不带组或版本的依赖项(例如@Grab('freemarker')
)来扩展 Groovy 的标准@Grab
支持。这样做可以参考 Spring Boot 的默认依赖元数据来推断工件的组和版本。
Note
默认元数据与您使用的 CLI 版本相关。仅当您移至新版本的 CLI 时,它才会更改,从而使您可以控制依赖项的版本何时更改。可以在appendix中找到一个表格,其中显示了默认元数据中包含的依赖项及其版本。
67.1.3 默认导入语句
为了帮助减少 Groovy 代码的大小,将自动包含多个import
语句。注意,前面的示例如何引用@Component
,@RestController
和@RequestMapping
,而无需使用完全限定的名称或import
语句。
Tip
许多 Spring Comments 无需使用import
语句即可工作。在添加导入之前,请尝试运行您的应用程序以查看失败的原因。
67.1.4 自动 Main 方法
与等效的 Java 应用程序不同,您不需要在Groovy
脚本中包含public static void main(String[] args)
方法。会自动创建一个SpringApplication
,并将您的编译后代码用作source
。
67.1.5 自定义依赖项 Management
默认情况下,CLI 使用spring-boot-dependencies
中声明的依赖性 Management 来解决@Grab
依赖性。可以使用@DependencyManagementBom
注解来配置其他依赖项 Management,这些依赖项 Management 将覆盖默认的依赖项 Management。Comments 的值应指定一个或多个 Maven BOM 的坐标(groupId:artifactId:version
)。
例如,考虑以下声明:
@DependencyManagementBom("com.example.custom-bom:1.0.0")
前面的声明在com/example/custom-versions/1.0.0/
下的 Maven 存储库中选取custom-bom-1.0.0.pom
。
当您指定多个 BOM 时,它们以声明它们的 Sequences 应用,如以下示例所示:
@DependencyManagementBom(["com.example.custom-bom:1.0.0",
"com.example.another-bom:1.0.0"])
前面的示例表明another-bom
中的依赖项 Management 覆盖custom-bom
中的依赖项 Management。
您可以在可以使用@Grab
的任何地方使用@DependencyManagementBom
。但是,为确保依赖性 Management 的 Sequences 一致,您最多可以在应用程序中使用@DependencyManagementBom
。 Spring IO 平台是依赖 Management 的有用资源(它是 Spring Boot 依赖 Management 的超集),您可以将其包括在以下行中:
@DependencyManagementBom('io.spring.platform:platform-bom:1.1.2.RELEASE')
67.2 具有多个源文件的应用程序
您可以对所有接受文件 Importing 的命令使用“ shell globbing”。这样可以使您从单个目录使用多个文件,如以下示例所示:
$ spring run *.groovy
67.3 打包您的应用程序
您可以使用jar
命令将您的应用程序打包到一个独立的可执行 jar 文件中,如以下示例所示:
$ spring jar my-app.jar *.groovy
生成的 jar 包含通过编译应用程序产生的类以及应用程序的所有依赖关系,以便随后可以使用java -jar
来运行它。 jar 文件还包含来自应用程序的 Classpath 的条目。您可以使用--include
和--exclude
添加和删除 jar 的显式路径。两者都用逗号分隔,并且都接受形式为“”和“-”的前缀,以表示应将其从默认值中删除。默认包括以下内容:
public/**, resources/**, static/**, templates/**, META-INF/**, *
默认排除项如下:
.*, repository/**, build/**, target/**, **/*.jar, **/*.groovy
在命令行上键入spring help jar
以获取更多信息。
67.4 初始化新项目
init
命令使您可以使用start.spring.io创建新项目,而无需离开 Shell,如以下示例所示:
$ 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 8 和war
打包的 Gradle 项目:
$ spring init --build=gradle --java-version=1.8 --dependencies=websocket --packaging=war sample-app.zip
Using service at https://start.spring.io
Content saved to 'sample-app.zip'
67.5 使用嵌入式 Shell
Spring Boot 包含用于 BASH 和 zsh Shell 的命令行完成脚本。如果您不使用这两个 Shell 程序(也许您是 Windows 用户),则可以使用shell
命令启动集成 Shell 程序,如以下示例所示:
$ spring shell
Spring Boot (v2.1.1.RELEASE)
Hit TAB to complete. Type \'help' and hit RETURN for help, and \'exit' to quit.
在嵌入式 Shell 程序内部,您可以直接运行其他命令:
$ version
Spring CLI v2.1.1.RELEASE
嵌入式 Shell 支持 ANSI 颜色输出以及tab
完成。如果需要运行本机命令,则可以使用!
前缀。要退出嵌入式 Shell,请按ctrl-c
。
67.6 将扩展添加到 CLI
您可以使用install
命令将扩展添加到 CLI。该命令采用group:artifact:version
格式的一组或多组工件坐标,如以下示例所示:
$ spring install com.example:spring-boot-cli-extension:1.0.0.RELEASE
除了安装由您提供的坐标标识的工件之外,还将安装所有工件的依赖项。
要卸载依赖项,请使用uninstall
命令。与install
命令一样,它采用group:artifact:version
格式的一组或多组工件坐标,如以下示例所示:
$ spring uninstall com.example:spring-boot-cli-extension:1.0.0.RELEASE
它将卸载由您提供的坐标及其依赖项标识的工件。
要卸载所有其他依赖项,可以使用--all
选项,如以下示例所示:
$ spring uninstall --all