Part VII. Spring Boot CLI
文檔說明:
- 文檔對應的版本為 2.1.0.M3
- 這不是文檔的完整中文翻譯,也有可能跟原文文字不一一對應,只是我閱讀文檔時候做的簡單筆記
- 如果對應的章節(jié)沒有任何中文棚蓄,有可能是文檔內容比較少晌块,建議直接看原文,或者是我不感興趣的部分
- 目錄標題沒有做翻譯徊件,首先標題一般一眼就能看懂什么意思奸攻,不做翻譯還能保證原文意思,其次也方便對應到原文位置
Spring Boot CLI(Command-Line Interface) 是一個快速開發(fā) Spring Boot 應用的命令行工具虱痕,允許你運行 Groovy 腳本睹耐。
65. Installing the CLI
參看 10.2 節(jié)內容:Installing the Spring Boot CLI
66. Using the CLI
spring 命令:
$ 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
$ spring version
Spring CLI v2.1.0.M3
66.1 Running Applications with the CLI
通過 run 命令可編譯并運行 Groovy 代碼,Spring Boot CLI 已經內置了 Groovy 軟件部翘,你不需要在額外安裝硝训。
@RestController
class WebApplication {
@RequestMapping("/")
String home() {
"Hello World!"
}
}
$ spring run hello.groovy
$ spring run hello.groovy -- --server.port=9000
$ JAVA_OPTS=-Xmx1024m spring run hello.groovy
66.1.1 Deduced “grab” Dependencies
Groovy 提供了 @Grab 注解來讓你聲明第三方依賴庫,Groovy 會自動下載庫文件略就,如果 Maven 或者 Gradle 的行為一樣捎迫。
Spring Boot 延續(xù)了這項功能,意圖在你的代碼里面減少 @Grab 注解表牢。例如上面例子的 @RestController窄绒,Spring Boot 自動引入了 Tomcat 和 Spring MVC。
更多的 Grab 自動配置 可參看 CompilerAutoConfiguration 類崔兴。
66.1.2 Deduced “grab” Coordinates
引入第三方庫的時候彰导,不要指定 group 和 version,會借用 Spring Boot 默認的依賴元數據敲茄。
66.1.3 Default Import Statements
import 語句不需要填寫 fully-qualified names位谋。
66.1.4 Automatic Main Method
66.1.5 Custom Dependency Management
引入你指定的依賴庫:
@DependencyManagementBom("com.example.custom-bom:1.0.0")
66.2 Applications with Multiple Source Files
$ spring run *.groovy
66.3 Packaging Your Application
打包成可執(zhí)行的 jar 文件:
$ spring jar my-app.jar *.groovy
66.4 Initialize a New Project
init 命令可以讓你通過 start.spirng.io 網址來創(chuàng)建一個新項目
$ spring init --dependencies=web,data-jpa my-project
Using service at https://start.spring.io
Project extracted to '/Users/developer/example/my-project'
66.5 Using the Embedded Shell
Spring Boot 在 BASH 和 zsh 有自動補全命令的腳本,在 Windows 下面可以啟用內嵌的 shell:
$ spring shell
Spring Boot (v2.1.0.M3)
Hit TAB to complete. Type \'help' and hit RETURN for help, and \'exit' to quit.
66.6 Adding Extensions to the CLI
引入第三方庫:
# group:artifact:version
$ spring install com.example:spring-boot-cli-extension:1.0.0.RELEASE
# uninstall
$ spring uninstall com.example:spring-boot-cli-extension:1.0.0.RELEASE
67. Developing Applications with the Groovy Beans DSL
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"
}
}
68. Configuring the CLI with settings.xml
Spring Boot CLI 使用 Aether 來處理依賴事情堰燎,Aether 也是 Maven 依賴管理的引擎掏父。CLI 也會使用 ~/.m2/setting.xml 來配置 Aether。
See Maven’s settings documentation for further information.
69. What to Read Next
如果你覺得 CLI 工具已經不能滿足你的需求秆剪,嘗試一下構建工具赊淑。