Spring Boot CLI的安裝與使用

  • 2019 年 12 月 19 日
  • 筆記

Java Web項目一般都比較重,不但有外部依賴,而且因為Java是靜態語言,還需要編譯後再打包才可以發布,每次遇到問題都需要重複這個過程。 本文介紹Spring Boot CLI可以實現快速的項目的開發和調試(不過語言使用了JVM上的Groovy)

安裝

*Spring Boot CLI的安裝方式有很多種,支援sdkman安裝,在MacOS上也可以使用HomeBrew。本文介紹手工安裝,Windows, Mac, Linux系統都可以使用。

  • 下載壓縮包
wget https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.2.2.RELEASE/spring-boot-cli-2.2.2.RELEASE.jar

也可以在https://repo.spring.io/release/org/springframework/boot/spring-boot-cli選擇想要的版本,再下載

  • 解壓到目標目錄
unzip spring-boot-cli-2.2.2.RELEASE-bin.zip -d ~/  mv ~/spring-2.2.2.RELEASE ~/spring-boot-cli
  • 配置環境變數
echo SPRING_HOME=/home/tenmao/spring-boot-cli >> ~/.bashrc  echo 'PATH=$SPRING_HOME/bin:$PATH' >> ~/.bashrc  source ~/.bashrc  spring --version

運行Groovy

  • hello.groovy
@RestController  class WebApplication {        @RequestMapping("/")      String home() {          "Hello World!n"      }  }
  • 運行spring run hello.groovy

超級便捷,跟PHP、Python等動態語言一樣,可以實現快速的項目部署。Spring Boot cli的grab機制,會自動去下載依賴的Jar包

[tenmao@VM_77_51_centos ~/spring-2.2.2.RELEASE]$ bin/spring run hello.groovy  Resolving dependencies...........................      .   ____          _            __ _ _   /\ / ___'_ __ _ _(_)_ __  __ _      ( ( )___ | '_ | '_| | '_ / _` |       \/  ___)| |_)| | | | | || (_| |  ) ) ) )    '  |____| .__|_| |_|_| |___, | / / / /   =========|_|==============|___/=/_/_/_/   :: Spring Boot ::        (v2.2.2.RELEASE)    2019-12-18 14:44:42.450  INFO 32015 --- [       runner-0] o.s.boot.SpringApplication               : Starting application on VM_77_51_centos with PID 32015 (started by tenmao in /data/home/tenmao/spring-2.2.2.RELEASE)  2019-12-18 14:44:42.456  INFO 32015 --- [       runner-0] o.s.boot.SpringApplication               : No active profile set, falling back to default profiles: default  2019-12-18 14:44:43.560  INFO 32015 --- [       runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)  2019-12-18 14:44:43.578  INFO 32015 --- [       runner-0] o.apache.catalina.core.StandardService   : Starting service [Tomcat]  2019-12-18 14:44:43.578  INFO 32015 --- [       runner-0] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.29]  2019-12-18 14:44:43.626  INFO 32015 --- [       runner-0] org.apache.catalina.loader.WebappLoader  : Unknown class loader [org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader@64765494] of class [class org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader]  2019-12-18 14:44:43.661  INFO 32015 --- [       runner-0] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext  2019-12-18 14:44:43.661  INFO 32015 --- [       runner-0] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1015 ms  2019-12-18 14:44:43.875  INFO 32015 --- [       runner-0] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'  2019-12-18 14:44:44.338  INFO 32015 --- [       runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''  2019-12-18 14:44:44.342  INFO 32015 --- [       runner-0] o.s.boot.SpringApplication               : Started application in 2.566 seconds (JVM running for 35.992)  2019-12-18 14:44:59.802  INFO 32015 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'  2019-12-18 14:44:59.803  INFO 32015 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'  2019-12-18 14:44:59.810  INFO 32015 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 7 ms

默認是8080埠,也可以指定其他埠

spring run hello.groovy -- --server.port=9000
  • 測試
[root@VM_77_51_centos ~]# curl http://localhost:8080/  Hello World!

其他高級命令

可以spring help查看其它命令及使用方法

[tenmao@VM_77_51_centos ~]$ spring help  usage: spring [--help] [--version]         <command> [<args>]    Available commands are:      run [options] <files> [--] [args]      Run a spring groovy script      grab      Download a spring groovy script's dependencies to ./repository      jar [options] <jar-name> <files>      Create a self-contained executable jar file from a Spring Groovy script      war [options] <war-name> <files>      Create a self-contained executable war file from a Spring Groovy script      install [options] <coordinates>      Install dependencies to the lib/ext directory      uninstall [options] <coordinates>      Uninstall dependencies from the lib/ext directory      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