美文网首页
Spring Boot CLI的安装与使用

Spring Boot CLI的安装与使用

作者: 十毛tenmao | 来源:发表于2019-12-18 23:41 被阅读0次

    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
    

    参考

    相关文章

      网友评论

          本文标题:Spring Boot CLI的安装与使用

          本文链接:https://www.haomeiwen.com/subject/sodunctx.html