Spring Boot CLI 是一个命令行工具,如果你想快速开发 Spring 应用程序,可以使用它。它允许你运行 Groovy 脚本,这意味着你有一个熟悉的类似 Java 的语法,而没有太多的样板代码。你还可以引导一个新项目或为它编写自己的命令。 Spring Boot CLI(命令行接口)可以使用 SDKMAN! 手动安装(SDK 管理器),如果你是OSX用户,使用 Homebrew 或 MacPorts。有关全面的安装说明,请参阅新手入门部分中的小节 10.2, “安装 Spring Boot CLI”。
一旦安装了 CLI,就可以通过键入 $ 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
可以键入 $ 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
$ spring version Spring CLI v2.1.0.BUILD-SNAPSHOT
可以使用 下面的示例展示了在 Groovy 中编写的 “hello world” web 应用程序: hello.groovy. @RestController class WebApplication { @RequestMapping("/") String home() { "Hello World!" } } 若要编译并运行应用程序,请键入以下命令: $ spring run hello.groovy
要将命令行参数传递给应用程序,请使用 $ spring run hello.groovy -- --server.port=9000
若要设置 JVM 命令行参数,可以使用 $ JAVA_OPTS=-Xmx1024m spring run hello.groovy
标准 Groovy 包含一个
Spring Boot 进一步扩展了这项技术,并试图根据你的代码推断哪些库抓取。例如,由于前面显示的 以下项目被用作抓取提示:
Spring Boot 扩展了 Groovy 的标准
为了帮助减少 Groovy 代码的大小,自动包含几个
与等效 Java 应用程序不同,你不需要用
默认情况下,CLI 在解析 例如,考虑下面的声明: @DependencyManagementBom("com.example.custom-bom:1.0.0")
前面的声明在 当你指定多个 BOM 时,它们以你声明它们的顺序来应用,如下面的示例所示: @DependencyManagementBom(["com.example.custom-bom:1.0.0", "com.example.another-bom:1.0.0"])
前面的示例表明,在
你可以在任何地方使用 @DependencyManagementBom('io.spring.platform:platform-bom:1.1.2.RELEASE')
可以使用 “shell globbing”,所有接受文件输入的命令。这样做可以让你从单个目录中使用多个文件,如下面的示例所示: $ spring run *.groovy
可以使用 $ spring jar my-app.jar *.groovy
生成的 jar 包含编译应用程序和所有应用程序依赖项产生的类,以便可以使用 public/**, resources/**, static/**, templates/**, META-INF/**, * 默认排除如下: .*, repository/**, build/**, target/**, **/*.jar, **/*.groovy
在命令行上键入
$ spring init --dependencies=web,data-jpa my-project Using service at https://start.spring.io Project extracted to '/Users/developer/example/my-project'
前面的示例使用基于 Maven 的项目创建一个 $ 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) ...
$ 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'
Spring Boot 包含 BASH 和 zsh shell 的命令行实现脚本。如果不使用这些 shell 中的任何一个(可能是 Windows 用户),可以使用 $ spring shell
Spring Boot (v2.1.0.BUILD-SNAPSHOT)
Hit TAB to complete. Type \'help' and hit RETURN for help, and \'exit' to quit.
从嵌入式 shell 内部,你可以直接运行其他命令: $ version Spring CLI v2.1.0.BUILD-SNAPSHOT
嵌入式 shell 支持 ANSI 颜色输出以及
可以通过使用安装命令添加 CLI 的扩展。命令获取格式为 $ spring install com.example:spring-boot-cli-extension:1.0.0.RELEASE 除了安装由你提供的组合标识的工件之外,还安装了所有的工件的依赖项。
若要卸载依赖项,请使用 $ spring uninstall com.example:spring-boot-cli-extension:1.0.0.RELEASE 它卸载由你提供的组合和它们的依赖项所标识的工件。
若要卸载所有附加的依赖项,可以使用 $ spring uninstall --all
Spring Framework 4.0 对 @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" } }
你可以将类声明与
Spring Boot CLI 使用 Aether,Maven 的依赖性解决引擎来解决依赖关系。CLI 利用在
有关的进一步信息请参阅 Maven 的设置文档。 这有一些示例 Groovy 脚本可从 GitHub 库中获得,你可以尝试使用 Spring Boot CLI。在源代码中也有大量的 JavaDoc。 如果你发现你达到了 CLI 工具的限制,那么你可能希望将应用程序转换为完整的 Gradle 或 Maven 构建的 Groovy 项目。下一部分介绍了 Spring Boot 的 "构建工具插件",它可以与 Gradle 或 Maven 一起使用。 |