VII. spring_boot_cli

Spring Boot CLI

Spring Boot CLI是一个命令行工具,如果想使用Spring进行快速开发可以使用它。它允许你运行Groovy脚本,这意味着你可以使用熟悉的类Java语法,并且没有那么多的模板代码。你也可以启动一个新的工程或为Spring Boot CLI编写自己的命令。

安装CLI

你可以手动安装Spring Boot CLI,也可以使用GVM(Groovy环境管理工具)或Homebrew,MacPorts(如果你是一个OSX用户)。参考”Getting started”的Section 10.2, “Installing the Spring Boot CLI” 可以看到全面的安装指令。

使用CLI

一旦安装好CLI,你可以输入spring来运行它。如果你不使用任何参数运行spring,将会展现一个简单的帮助界面:

$ spring
usage: spring [--help] [--version]
       <command> [<args>]

Available commands are:

  run [options] <files> [--] [args]
    Run a spring groovy script

  ... more command help is shown here

你可以使用help获取任何支持命令的详细信息。例如:

$ 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

version命令提供一个检查你正在使用的Spring Boot版本的快速方式:

$ spring version
Spring CLI v1.3.0.BUILD-SNAPSHOT
  • 使用CLI运行应用

你可以使用run命令编译和运行Groovy源代码。Spring Boot CLI完全自包含,以致于你不需要安装任何外部的Groovy。

下面是一个使用Groovy编写的”hello world” web应用:
hello.grooy

@RestController
class WebApplication {

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

}

想要编译和运行应用,输入:

$ spring run hello.groovy

想要给应用传递命令行参数,你需要使用一个--来将它们和”spring”命令参数区分开来。例如:

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

想要设置JVM命令行参数,你可以使用JAVA_OPTS环境变量,例如:

$ JAVA_OPTS=-Xmx1024m spring run hello.groovy
  • 推断”grab”依赖

标准的Groovy包含一个@Grab注解,它允许你声明对第三方库的依赖。这项有用的技术允许Groovy以和Maven或Gradle相同的方式下载jars,但不需要使用构建工具。

Spring Boot进一步延伸了该技术,它会基于你的代码尝试推导你”grab”哪个库。例如,由于WebApplication代码上使用了@RestController注解,”Tomcat”和”Spring MVC”将被获取(grabbed)。

下面items被用作”grab hints”:

itemsGrabs
JdbcTemplate,NamedParameterJdbcTemplate,DataSourceJDBC应用
@EnableJmsJMS应用
@EnableCachingCaching abstraction
@TestJUnit
@EnableRabbitRabbitMQ
@EnableReactorProject Reactor
继承SpecificationSpock test
@EnableBatchProcessingSpring Batch
@MessageEndpoint,@EnableIntegrationPatternsSpring Integration
@EnableDeviceResolverSpring Mobile
@Controller,@RestController,@EnableWebMvcSpring MVC + Embedded Tomcat
@EnableWebSecuritySpring Security
@EnableTransactionManagementSpring Transaction Management

:想要理解自定义是如何生效,可以查看Spring Boot CLI源码中的CompilerAutoConfiguration子类。

  • 推断”grab”坐标

Spring Boot扩展Groovy标准”@Grab”注解使其能够允许你指定一个没有group或version的依赖,例如@Grab('freemarker')
artifact’s的组和版本是通过查看Spring Boot的依赖元数据推断出来的。注意默认的元数据是和你使用的CLI版本绑定的-只有在你迁移到一个CLI新版本时它才会改变,这样当你的依赖改变时你就可以控制了。在附录的表格中可以查看默认元数据包含的依赖和它们的版本。

  • 默认import语句

为了帮助你减少Groovy代码量,一些import语句被自动包含进来了。注意上面的示例中引用@Component@RestController@RequestMapping而没有使用全限定名或import语句。

:很多Spring注解在不使用import语句的情况下可以正常工作。尝试运行你的应用,看一下在添加imports之前哪些会失败。

  • 自动创建main方法

跟等效的Java应用不同,你不需要在Groovy脚本中添加一个public static void main(String[] args)方法。Spring Boot 会使用你编译后的代码自动创建一个SpringApplication。

  • 自定义”grab”元数据

Spring Boot提供一个新的@GrabMetadata注解,你可以使用它提供自定义的依赖元数据,以覆盖Spring Boot的默认配置。该元数据通过使用提供一个或多个配置文件坐标的注解来指定(使用一个属性标识符”type”部署到Maven仓库).。配置文件中的每个实体必须遵循group:module=version的格式。

例如,下面的声明:

`@GrabMetadata("com.example.custom-versions:1.0.0")`

将会加载Maven仓库处于com/example/custom-versions/1.0.0/下的custom-versions-1.0.0.properties文件。

可以通过注解指定多个属性文件,它们会以声明的顺序被使用。例如:

`@GrabMetadata(["com.example.custom-versions:1.0.0",
        "com.example.more-versions:1.0.0"])`

意味着位于more-versions的属性将覆盖位于custom-versions的属性。

你可以在任何能够使用@Grab的地方使用@GrabMetadata,然而,为了确保元数据的顺序一致,你在应用程序中最多只能使用一次@GrabMetadataSpring IO Platform是一个非常有用的依赖元数据源(Spring Boot的超集),例如:

@GrabMetadata('io.spring.platform:platform-versions:1.0.4.RELEASE')
  • 测试你的代码

test命令允许你编译和运行应用程序的测试用例。常规使用方式如下:

$ spring test app.groovy tests.groovy
Total: 1, Success: 1, : Failures: 0
Passed? true

在这个示例中,test.groovy包含JUnit @Test方法或Spock Specification类。所有的普通框架注解和静态方法在不使用import导入的情况下,仍旧可以使用。

下面是我们使用的test.groovy文件(含有一个JUnit测试):

class ApplicationTests {

    @Test
    void homeSaysHello() {
        assertEquals("Hello World!", new WebApplication().home())
    }

}

:如果有多个测试源文件,你可以倾向于使用一个test目录来组织它们。

  • 多源文件应用

你可以在所有接收文件输入的命令中使用shell通配符。这允许你轻松处理来自一个目录下的多个文件,例如:

$ spring run *.groovy

如果你想将’test’或’spec’代码从主应用代码中分离,这项技术就十分有用了:

$ spring test app/*.groovy test/*.groovy
  • 应用打包

你可以使用jar命令打包应用程序为一个可执行的jar文件。例如:

$ spring jar my-app.jar *.groovy

最终的jar包括编译应用产生的类和所有依赖,这样你就可以使用java -jar来执行它了。该jar文件也包括来自应用classpath的实体。你可以使用--include--exclude添加明确的路径(两者都是用逗号分割,同样都接收值为’+’和’-‘的前缀,’-‘意味着它们将从默认设置中移除)。默认包含(includes):

public/**, resources/**, static/**, templates/**, META-INF/**, *

默认排除(excludes):

.*, repository/**, build/**, target/**, **/*.jar, **/*.groovy

查看spring help jar可以获得更多信息。

  • 初始化新工程

init命令允许你使用start.spring.io在不离开shell的情况下创建一个新的项目。例如:

$ spring init --dependencies=web,data-jpa my-project
Using service at https://start.spring.io
Project extracted to '/Users/developer/example/my-project'

这创建了一个my-project目录,它是一个基本Maven且依赖spring-boot-starter-webspring-boot-starter-data-jpa的项目。你可以使用--list参数列出该服务的能力。

$ spring init --list
=======================================
Capabilities of https://start.spring.io
=======================================

Available dependencies:
-----------------------
actuator - Actuator: Production ready features to help you monitor and manage your application
...
web - Web: Support for full-stack web development, including Tomcat and spring-webmvc
websocket - Websocket: Support for WebSocket development
ws - WS: Support for Spring Web Services

Available project types:
------------------------
gradle-build -  Gradle Config [format:build, build:gradle]
gradle-project -  Gradle Project [format:project, build:gradle]
maven-build -  Maven POM [format:build, build:maven]
maven-project -  Maven Project [format:project, build:maven] (default)

...

init命令支持很多选项,查看help输出可以获得更多详情。例如,下面的命令创建一个使用Java8和war打包的gradle项目:

$ spring init --build=gradle --java-version=1.8 --dependencies=websocket --packaging=war sample-app.zip
Using service at https://start.spring.io
Content saved to 'sample-app.zip'
  • 使用内嵌shell

Spring Boot包括完整的BASH和zsh shells的命令行脚本。如果你不使用它们中的任何一个(可能你是一个Window用户),那你可以使用shell命令启用一个集成shell。

$ spring shell
Spring Boot (v1.3.0.BUILD-SNAPSHOT)
Hit TAB to complete. Type \'help' and hit RETURN for help, and \'exit' to quit.

从内嵌shell中可以直接运行其他命令:

$ version
Spring CLI v1.3.0.BUILD-SNAPSHOT

内嵌shell支持ANSI颜色输出和tab补全。如果需要运行一个原生命令,你可以使用$前缀。点击ctrl-c将退出内嵌shell。

  • 为CLI添加扩展

使用install命令可以为CLI添加扩展。该命令接收一个或多个格式为group:artifact:version的artifact坐标集。例如:

$ spring install com.example:spring-boot-cli-extension:1.0.0.RELEASE

除了安装你提供坐标的artifacts标识外,所有依赖也会被安装。使用uninstall可以卸载一个依赖。和install命令一样,它接收一个或多个格式为group:artifact:version的artifact坐标集。例如:

$ spring uninstall com.example:spring-boot-cli-extension:1.0.0.RELEASE

它会通过你提供的坐标卸载相应的artifacts标识和它们的依赖。

为了卸载所有附加依赖,你可以使用--all选项。例如:

$ spring uninstall --all
  • 使用Groovy beans DSL开发应用

Spring框架4.0版本对beans{} DSL(借鉴自Grails)提供原生支持,你可以使用相同的格式在你的Groovy应用程序脚本中嵌入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"
    }
}

你可以使用beans{}混合位于相同文件的类声明,只要它们都处于顶级,或如果你喜欢的话,可以将beans DSL放到一个单独的文件中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值