Spring Boot 官網文檔簡單翻譯 Part VII

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 工具已經不能滿足你的需求秆剪,嘗試一下構建工具赊淑。

?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市仅讽,隨后出現的幾起案子陶缺,更是在濱河造成了極大的恐慌,老刑警劉巖洁灵,帶你破解...
    沈念sama閱讀 218,607評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件饱岸,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機苫费,發(fā)現死者居然都...
    沈念sama閱讀 93,239評論 3 395
  • 文/潘曉璐 我一進店門汤锨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人百框,你說我怎么就攤上這事泥畅。” “怎么了球散?”我有些...
    開封第一講書人閱讀 164,960評論 0 355
  • 文/不壞的土叔 我叫張陵牛欢,是天一觀的道長。 經常有香客問我,道長活逆,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,750評論 1 294
  • 正文 為了忘掉前任躁倒,我火速辦了婚禮撰豺,結果婚禮上,老公的妹妹穿的比我還像新娘有决。我一直安慰自己闸拿,他們只是感情好,可當我...
    茶點故事閱讀 67,764評論 6 392
  • 文/花漫 我一把揭開白布书幕。 她就那樣靜靜地躺著新荤,像睡著了一般。 火紅的嫁衣襯著肌膚如雪台汇。 梳的紋絲不亂的頭發(fā)上苛骨,一...
    開封第一講書人閱讀 51,604評論 1 305
  • 那天,我揣著相機與錄音苟呐,去河邊找鬼痒芝。 笑死,一個胖子當著我的面吹牛牵素,可吹牛的內容都是我干的严衬。 我是一名探鬼主播,決...
    沈念sama閱讀 40,347評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼笆呆,長吁一口氣:“原來是場噩夢啊……” “哼请琳!你這毒婦竟也來了?” 一聲冷哼從身側響起腰奋,我...
    開封第一講書人閱讀 39,253評論 0 276
  • 序言:老撾萬榮一對情侶失蹤单起,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后劣坊,有當地人在樹林里發(fā)現了一具尸體嘀倒,經...
    沈念sama閱讀 45,702評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,893評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現自己被綠了测蘑。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片灌危。...
    茶點故事閱讀 40,015評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖碳胳,靈堂內的尸體忽然破棺而出勇蝙,到底是詐尸還是另有隱情,我是刑警寧澤挨约,帶...
    沈念sama閱讀 35,734評論 5 346
  • 正文 年R本政府宣布味混,位于F島的核電站,受9級特大地震影響诫惭,放射性物質發(fā)生泄漏翁锡。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,352評論 3 330
  • 文/蒙蒙 一夕土、第九天 我趴在偏房一處隱蔽的房頂上張望馆衔。 院中可真熱鬧,春花似錦怨绣、人聲如沸角溃。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽减细。三九已至,卻和暖如春赢笨,著一層夾襖步出監(jiān)牢的瞬間邪财,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評論 1 270
  • 我被黑心中介騙來泰國打工质欲, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留树埠,地道東北人。 一個月前我還...
    沈念sama閱讀 48,216評論 3 371
  • 正文 我出身青樓嘶伟,卻偏偏與公主長得像怎憋,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子九昧,可洞房花燭夜當晚...
    茶點故事閱讀 44,969評論 2 355

推薦閱讀更多精彩內容