64. 使用CLI
一旦你安装完成CLI,你可以运行它,通过输入spring
并在命令行按下Enter键。
如果你运行spring
而没带任何参数,一个简单的帮助屏幕会被展示,如下:
- $ spring
- usage: spring [--help] [--version]
[ ] Available commands are: run [options] [--] [args] Run a spring groovy script ... more command help is shown here
你可以输入spring help
得到更多细节关于受支持的命令,如下例子所示:
- $ spring help run
- spring run - Run a spring groovy script
- usage: spring run [options]
[--] [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 v2.0.4.RELEASE
64.1 使用CLI运行应用
你可以编译和运行Groovy源码通过使用run
命令。
64.2 有多个源文件的应用
你可以使用“shell globbing”和所有命令一起来接受文件输入。这么做使你从一个目录使用多个文件,如下所示:
- $ spring run *.groovy
64.3 打包你的应用
你可以使用jar
命令来打包你的应用到一个自我包含的可执行jar文件,如下例子:
- $ spring jar my-app.jar *.groovy
生成的jar文件包含编译应用产生的类和应用的所有依赖,以便于之后可以用java -jar
运行。该jar文件也包含应用的classpath的入口(entries)。你可以添加和移除明确的路径到该jar通过使用—exclude
。这两个都是逗号分隔的,而且都接受前缀, in the form of “+” and “-”, to signify that they should be removed from the defaults.默认包含的如下:
- public/**, resources/**, static/**, templates/**, META-INF/**, *
默认不包含的如下:
- .*, repository/**, build/**, target/**, **/*.jar, **/*.groovy
在命令行输入spring help jar
来获取更多信息。
64.4 初始化一个新工程
该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-web
和spring-boot-starter-data-jpa
。你可以列出该服务的能力(capability)通过使用—flag
标志,如下所示:
- $ 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
输出了解更多细节。比如,下面的命令行创建一个使用Java 8和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'
64.5 使用内嵌的Shell
Spring Boot为BASH和zsh Shell包含命令行完成脚本。如果你不使用这些Shell中的任何一个(可能你是一个Windows用户),你可以使用shell
命令来启动一个整个的shell,如下所示:
- $ spring shell
- Spring Boot (v2.0.4.RELEASE)
- Hit TAB to complete. Type \'help' and hit RETURN for help, and \'exit' to quit.
在内嵌的shell内部,你可以直接运行其他命令:
- $ version
- Spring CLI v2.0.4.RELEASE
该内嵌的Shell支持ANSI 颜色输出和tab
补全。如果你需要运行一个原生的命令,你可以使用!
前缀。要退出内嵌的shell,请按ctrl-c
。
64.6 为CLI添加扩展
你可以使用install
命令添加扩展到CLI。该命令使用一个或多个artifact coordinates 集合(set),形如:group:artifact:version
,如下所示:
- $ spring install com.example:spring-boot-cli-extension:1.0.0.RELEASE
In addition to installing the artifacts identified by the coordinates you supply, all of the artifacts’ dependencies are also installed.
要卸载一个依赖,使用uninstall
命令。和install
命令一样,该命令使用一个或多个artifact coordinates 集合(set),形如:group:artifact:version
,如下所示:
- $ spring uninstall com.example:spring-boot-cli-extension:1.0.0.RELEASE
It uninstalls the artifacts identified by the coordinates you supply and their dependencies.
要卸载所有额外的依赖,你可以使用—all
选项,如下:
- $ spring uninstall --all