49.2.1 远程shell命令

你可以使用Groovy或Java编写其他的shell命令(具体参考CRaSH文档),Spring Boot默认会搜索以下路径的命令:

  • classpath*:/commands/**
  • classpath*:/crash/commands/**

设置shell.command-path-patterns属性可以改变搜索路径。 如果使用可执行存档(archive),shell依赖的所有类都必须打包进一个内嵌的jar,而不是直接打包进可执行jar或war。

下面是一个从src/main/resources/commands/hello.groovy加载的’hello’命令:

  1. package commands
  2. import org.crsh.cli.Usage
  3. import org.crsh.cli.Command
  4. class hello {
  5. @Usage("Say Hello")
  6. @Command
  7. def main(InvocationContext context) {
  8. return "Hello"
  9. }
  10. }

Spring Boot为InvocationContext添加一些其他属性,你可以在命令中访问它们:

属性名称 描述
spring.boot.version Spring Boot的版本
spring.version Spring核心框架的版本
spring.beanfactory 获取Spring的BeanFactory
spring.environment 获取Spring的Environment