Spring Boot CLI——centos7

Posted wpcnblog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot CLI——centos7相关的知识,希望对你有一定的参考价值。

Spring Boot是一个命令行工具,用于使用Spring进行快速原型搭建。它允许你运行Groovy脚本,这意味着你可以使用类Java的语法,并且没有那么多的模板代码。

所有版本下载地址
这里下载的版本spring-boot-cli-2.2.0.BUILD-20190222.193142-143-bin.tar.gz

下载完成后解压

tar   spring-boot-cli-2.2.0.BUILD-20190222.193142-143-bin.tar.gz  -C  /home/maxzhao/
cd /home/maxzhao
mv spring-2.2.0.BUILD-SNAPSHOT  spring-2.2.0-cli
cd spring-2.2.0-cli 

当前目录下有INSTALL.txt文件,这里面就是具体的安装方法。
INSTALL.txt文件中讲的很清楚了,spring-boot-cli-2.2.0 需要JDK1.8+

环境变量配置:

# 管理员下
# set springboot-cli
vim /etc/profile
#最后输入
export SPRING_HOME="/home/maxzhao/soft/spring-2.2.0-cli"
export PATH="$SPRING_HOME/bin:$PATH"
#保存
source /etc/profile
spring --version 
# Spring CLI v2.2.0.BUILD-SNAPSHOT

简单的springboot-cli例子

 

运行Groovy脚本

在完成了Spring Boot CLI的安装之后,我们来试试使用它来快速的构建一个Spring Boot应用,方法很简单,只需要如下操作:

第一步:新建一个Groovy脚本,hello.groovy,内容如下:

@RestController
class ThisWillActuallyRun 

    @RequestMapping("/")
    String home() 
        "Hello World!"
    
    

第二步:使用spring命令运行该Groovy脚本,具体如下:

$ spring run hello.groovy
Resolving dependencies...............................................




                                *::::::::::::::*
                           ::::::::::::::::::::::::::
                        ::::::::::::::::::::::::::::::::       :.
                     *:::::::::::::::::::::::::::::::::::::   :*:
                   :::::::::::::::::::::::::::::::::::::::::::  *:
                 .::::::::::::::::::::::::::::::::::::::::::     :
                ::::::::::::::::::::::::::::::::::::::::::.      .:
               :::::::::::::::::::::::::::::::::::::::::          :
              ::::::::::::::::::::::::::::::::::::::              *:
             :::::::::::::::::::::::::::::::::*                    :
            :::::::::::::::::::*                                   :
            :::::::::::::::                                 *      *:
           .::::::::::::                                   *       .:
           :::::::::::.                                   :         :
           ::::::::::                                    :         .:
           ::::::::::                                  ::          .:
           .::::::::                                 :::           :*
            ::::::::                               :::             :
            ::::::::                            ::::              ::
             :::::::                         :::::               *:
              :::::::                   *:::::::                ::
               :::::::.          :::::::::::                  :::
                :::::::::::::::::::::::*                    ::::
                 .:::   :::::::::*                      .:::::*
                   :     ::::::::::::::::::::::::::::::::::::
                     . .:::::::::::::::::::::::::::::::::::
                        ::::::::::::::::::::::::::::::::
                           ::::::::::::::::::::::::::
                                *::::::::::::::*



2018-04-04 18:11:20.571  INFO 70477 --- [       runner-0] o.s.boot.SpringApplication               : Starting application on zhaiyongchaodeMacBook-Pro.local with PID 70477 (started by zhaiyongchao in /Users/zhaiyongchao/Downloads)
2018-04-04 18:11:20.574  INFO 70477 --- [       runner-0] o.s.boot.SpringApplication               : No active profile set, falling back to default profiles: default
2018-04-04 18:11:20.732  INFO 70477 --- [       runner-0] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3c3ad454: startup date [Wed Apr 04 18:11:20 CST 2018]; root of context hierarchy
2018-04-04 18:11:21.758  INFO 70477 --- [       runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-04-04 18:11:21.783  INFO 70477 --- [       runner-0] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-04-04 18:11:21.784  INFO 70477 --- [       runner-0] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.28
2018-04-04 18:11:21.795  INFO 70477 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/zhaiyongchao/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2018-04-04 18:11:21.849  INFO 70477 --- [ost-startStop-1] org.apache.catalina.loader.WebappLoader  : Unknown loader org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader@5e3b901 class org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader
2018-04-04 18:11:21.884  INFO 70477 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-04-04 18:11:21.885  INFO 70477 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1153 ms
2018-04-04 18:11:22.042  INFO 70477 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-04-04 18:11:22.049  INFO 70477 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: characterEncodingFilter to: [/*]
2018-04-04 18:11:22.049  INFO 70477 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: ‘hiddenHttpMethodFilter‘ to: [/*]
2018-04-04 18:11:22.049  INFO 70477 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: ‘httpPutFormContentFilter‘ to: [/*]
2018-04-04 18:11:22.049  INFO 70477 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: ‘requestContextFilter‘ to: [/*]
2018-04-04 18:11:22.391  INFO 70477 --- [       runner-0] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3c3ad454: startup date [Wed Apr 04 18:11:20 CST 2018]; root of context hierarchy
2018-04-04 18:11:22.489  INFO 70477 --- [       runner-0] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/]" onto public java.lang.String ThisWillActuallyRun.home()
2018-04-04 18:11:22.495  INFO 70477 --- [       runner-0] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/error]" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-04-04 18:11:22.496  INFO 70477 --- [       runner-0] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/error],produces=[text/html]" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-04-04 18:11:22.550  INFO 70477 --- [       runner-0] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-04 18:11:22.550  INFO 70477 --- [       runner-0] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-04 18:11:22.598  INFO 70477 --- [       runner-0] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-04 18:11:22.955  INFO 70477 --- [       runner-0] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-04-04 18:11:23.004  INFO 70477 --- [       runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ‘‘
2018-04-04 18:11:23.010  INFO 70477 --- [       runner-0] o.s.boot.SpringApplication

从日志中我们可以看到我们通过Groovy脚本定义的一个简单web应用就构建成功了,可以通过访问localhost:8080来验证一下上面应用提供的接口:

$ curl localhost:8080
Hello World!

可以在命令中附加spring配置参数,注意要多加一个--

$ spring run hello.groovy -- --server.port=9000

要设置JVM参数,可以使用JAVA_OPTS环境变量,比如:

$ JAVA_OPTS=-Xmx1024m spring run hello.groovy

初始化新项目

使用init命令可以创建一个新的项目。如:

$ spring init --dependencies=web,data-jpa my-project

这个命令会创建一个名为my-project的目录,在其中包含一个基于Maven的项目,并添加对于spring-boot-starter-webspring-boot-starter-data-jpa的依赖。

你可以使用--list标记来显示所有可用的依赖。

$ spring init --list

运行spring help init可以查看具体的使用说明。

如下的命令会创建一个基于Gradlejava8的打包类型为war的项目。

$ spring init --build=gradle --java-version=1.8 --dependencies=websocket --packaging=war sample-app.zip

使用Spring Shell

Spring Boot CLI内置了一个shell,执行spring shell即可启动。这个shell支持ANSI彩色输出和tab键,如果要执行原生命令,在命令前加上!前缀即可。更多具体使用方法,可以在进入shell后输入help查看。

更多资料

可以在这里找到更多的groovy脚本例子,这里有Spring Build tool plugins相关的文档。

以上是关于Spring Boot CLI——centos7的主要内容,如果未能解决你的问题,请参考以下文章

spring-boot学习 (Groovy与Spring Boot Cli)

Spring Boot CLI命令行工具

Spring Boot CLI——centos7

如何在Ubuntu中安装Spring Boot CLI?

spring boot cli 安装

spring boot cli 安装