第七部分。Spring Boot CLI
Spring Boot CLI是一个命令行工具,如果您想用Spring快速开发,可以使用它。它允许你运行Groovy脚本,这意味着你有一个熟悉的类Java语法,没有太多的样板代码。您也可以引导一个新项目或编写自己的命令。
61.安装CLI
Spring Boot CLI可以手动安装; 使用SDKMAN!(SDK管理器)或使用Homebrew或MacPorts(如果您是OSX用户)。有关 全面的安装说明,请参见 “入门”一节中的第10.2节“安装Spring Boot CLI”。
62.使用CLI
一旦你安装了CLI,你可以通过输入来运行它spring
。如果您运行时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
您可以使用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 v1.5.14.RELEASE
62.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
要将命令行参数传递给应用程序,需要使用a --
将它们与“spring”命令参数分开,例
$ spring run hello.groovy -- --server.port=9000
要设置JVM命令行参数,您可以使用JAVA_OPTS
环境变量,例
$ JAVA_OPTS=-Xmx1024m spring run hello.groovy
![]() |
|
62.1.1推导出“抓取”依赖关系
标准Groovy包含一个@Grab
注释,允许您声明第三方库的依赖关系。这个有用的技术允许Groovy像Maven或Gradle一样下载jar,但不需要使用构建工具。
Spring Boot进一步扩展了这一技术,并试图根据您的代码推断出哪些库需要“抓取”。例如,由于WebApplication
上面的代码使用 @RestController
注释,“Tomcat”和“Spring MVC”将被抓取。
以下项目用作“抓取提示”:
Items | Grabs |
---|---|
| JDBC Application. |
| JMS Application. |
| Caching abstraction. |
JUnit. | |
| RabbitMQ. |
| Project Reactor. |
extends | Spock test. |
| Spring Batch. |
| Spring Integration. |
| Spring Mobile. |
| Spring MVC + Embedded Tomcat. |
| Spring Security. |
| Spring Transaction Management. |
![]() |
请参阅 |
62.1.2推导出“抓取”坐标
例如,Spring Boot扩展了Groovy的标准@Grab
支持,允许您指定不带组或版本的依赖项@Grab('freemarker')
。这将会参考Spring Boot的默认依赖元数据来推断工件的组和版本。请注意,默认元数据与您正在使用的CLI版本相关联 - 只有当您转移到CLI的新版本时,才会更改默认元数据,使您可以控制何时版本的依赖关系可能会发生变化。显示默认元数据中所包含的依赖关系及其版本的表可以在附录中找到。
62.1.3默认导入语句
为了减少Groovy代码的大小,import
会自动包含几个语句。注意上面的例子中如何引用@Component
, @RestController
而@RequestMapping
无需使用完全限定名称或import
说明。
![]() |
许多Spring注释将在不使用 |
62.1.4自动主方法
与等效的Java应用程序不同,您不需要public static void main(String[] args)
在Groovy
脚本中包含 方法。A SpringApplication
是自动创建的,编译后的代码充当 source
。
62.1.5自定义依赖关系管理
默认情况下,CLI使用spring-boot-dependencies
解析时声明的依赖关系管理@Grab
依赖项。可以使用@DependencyManagementBom
注释来配置其他依赖项管理,它将覆盖默认的依赖关系管理。注解的值应该指定groupId:artifactId:version
一个或多个Maven BOM 的坐标()。
例如,下面的声明:
@DependencyManagementBom("com.example.custom-bom:1.0.0")
将custom-bom-1.0.0.pom
在下面的Maven仓库中找到 com/example/custom-versions/1.0.0/
。
当指定多个BOM时,将按照它们声明的顺序应用它们。例如:
@DependencyManagementBom(["com.example.custom-bom:1.0.0", "com.example.another-bom:1.0.0"])
表示依赖管理 another-bom
将覆盖依赖关系管理custom-bom
。
但是,您可以@DependencyManagementBom
在任何可以使用的地方使用@Grab
,以确保依从性管理的顺序一致@DependencyManagementBom
,您的应用程序中最多只能使用 一次。一个有用的依赖管理源(即Spring Boot依赖管理的超集)是 Spring IO Platform,例如@DependencyManagementBom('io.spring.platform:platform-bom:1.1.2.RELEASE')
。
62.2测试你的代码
该 test
命令允许您编译和运行应用程序的测试。典型用法如下所示:
$ spring test app.groovy tests.groovy Total: 1, Success: 1, : Failures: 0 Passed? true
在这个例子中,tests.groovy
包含JUnit @Test
方法或Spock Specification
类。所有常见的框架注释和静态方法都应该可以使用,而无需使用import
它们。
以下是tests.groovy
我们上面使用的文件(使用JUnit测试):
class ApplicationTests { @Test void homeSaysHello() { assertEquals("Hello World!", new WebApplication().home()) } }
![]() |
如果您有多个测试源文件,您可能更愿意将它们组织到一个 |
62.3具有多个源文件的应用程序
您可以在接受文件输入的所有命令中使用“shell globbing”。这允许您轻松使用单个目录中的多个文件,例如
$ spring run *.groovy
如果您想从主应用程序代码中分离出“测试”或“规范”代码,该技术也可能很有用:
$ spring test app/*.groovy test/*.groovy
62.4打包您的应用程序
您可以使用该jar
命令将您的应用程序打包成一个自包含的可执行jar文件。例如:
$ spring jar my-app.jar *.groovy
生成的jar将包含通过编译应用程序和所有应用程序的依赖关系生成的类,以便可以使用它运行java -jar
。该jar文件还将包含应用程序类路径中的条目。你可以使用--include
和添加到jar的显式路径--exclude
(两者都是逗号分隔的,并且两者都接受值“+”和“ - ”的前缀以表示它们应该从默认值中移除)。默认包括
public/**, resources/**, static/**, templates/**, META-INF/**, *
并且默认排除是
.*, repository/**, build/**, target/**, **/*.jar, **/*.groovy
请参阅输出以spring help jar
获取更多信息。
62.5初始化一个新项目
该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
使用spring-boot-starter-web
和创建一个基于Maven的项目的目录 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
命令支持许多选项,请查看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'
62.6使用embedded shell
Spring Boot包含BASH和zsh shell的命令行完成脚本。如果你没有使用这两个shell(也许你是一个Windows用户),那么你可以使用这个 shell
命令启动一个集成的shell。
$ spring shell Spring Boot (v1.5.14.RELEASE) Hit TAB to complete. Type \'help' and hit RETURN for help, and \'exit' to quit.
从嵌入式shell中,您可以直接运行其他命令
$ version Spring CLI v1.5.14.RELEASE
嵌入式外壳支持ANSI颜色输出以及tab
完成。如果你需要运行一个本地命令,你可以使用!
前缀。点击ctrl-c
将退出嵌入式外壳。
62.7向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
63.使用Groovy beans DSL开发应用程序
Spring Framework 4.0具有对beans{}
“DSL”(从Grails借用 )的本地支持,您可以使用相同的格式将Groovy应用程序脚本中的bean定义嵌入到其中。这有时是包含中间件声明等外部功能的好方法。例如:
@Configuration class Application implements CommandLineRunner { @Autowired SharedService service @Override void run(String... args) { println service.message } } import my.company.SharedService beans { service(SharedService) { message = "Hello World" } }
beans{}
只要它们保持在顶层,您可以将类声明与同一个文件混合使用,或者如果您愿意,可以将beans DSL放在单独的文件中。
64.使用settings.xml配置CLI
Spring Boot CLI使用Maven的依赖关系解析引擎Aether来解决依赖关系。CLI使用找到的Maven配置~/.m2/settings.xml
来配置Aether。CLI遵循以下配置设置:
- Offline
- Mirrors
- Servers
- Proxies
-
Profiles
- Activation
- Repositories
- Active profiles
有关更多信息,请参阅Maven的设置文档。
65.接下来要读什么
GitHub存储库中提供了一些示例groovy脚本,您可以使用它来尝试Spring Boot CLI。整个源代码中还有大量的Javadoc 。
如果你发现你达到了CLI工具的限制,你可能会想要将你的应用程序转换为完整的Gradle或Maven构建的“groovy项目”。下一节介绍Spring Boot的 构建工具插件,您可以使用Gradle或Maven。