64. 使用CLI

    一旦你安装完成CLI,你可以运行它,通过输入spring并在命令行按下Enter键。

    如果你运行spring而没带任何参数,一个简单的帮助屏幕会被展示,如下:

    1. $ spring
    2. usage: spring [--help] [--version]
    3. []
    4. Available commands are:
    5. run [options] [--] [args]
    6. Run a spring groovy script
    7. ... more command help is shown here

    你可以输入spring help得到更多细节关于受支持的命令,如下例子所示:

    1. $ spring help run
    2. spring run - Run a spring groovy script
    3.  
    4. usage: spring run [options] [--] [args]
    5. Option Description
    6. ------ -----------
    7. --autoconfigure [Boolean] Add autoconfigure compiler
    8. transformations (default: true)
    9. --classpath, -cp Additional classpath entries
    10. -e, --edit Open the file with the default system
    11. editor
    12. --no-guess-dependencies Do not attempt to guess dependencies
    13. --no-guess-imports Do not attempt to guess imports
    14. -q, --quiet Quiet logging
    15. -v, --verbose Verbose logging of dependency
    16. resolution
    17. --watch Watch the specified file for changes

    version命令提供一个快速的方法来检查你正在使用的Spring Boot的版本,如下:

    1. $ spring version
    2. Spring CLI v2.0.4.RELEASE

    64.1 使用CLI运行应用

    你可以编译和运行Groovy源码通过使用run命令。

    64.2 有多个源文件的应用

    你可以使用“shell globbing”和所有命令一起来接受文件输入。这么做使你从一个目录使用多个文件,如下所示:

    1. $ spring run *.groovy

    64.3 打包你的应用

    你可以使用jar命令来打包你的应用到一个自我包含的可执行jar文件,如下例子:

    1. $ spring jar my-app.jar *.groovy

    生成的jar文件包含编译应用产生的类和应用的所有依赖,以便于之后可以用java -jar运行。该jar文件也包含应用的classpath的入口(entries)。你可以添加和移除明确的路径到该jar通过使用—include—exclude。这两个都是逗号分隔的,而且都接受前缀, in the form of “+” and “-”, to signify that they should be removed from the defaults.默认包含的如下:

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

    默认不包含的如下:

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

    在命令行输入spring help jar来获取更多信息。

    64.4 初始化一个新工程

    init命令使你创建一个新的工程通过使用start.spring.io而不用离开shell,如下例子所示:

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

    之前的例子创建了一个my-project目录和一个基于Maven的工程,它使用spring-boot-starter-webspring-boot-starter-data-jpa。你可以列出该服务的能力(capability)通过使用—flag标志,如下所示:

    1. $ spring init --list
    2. =======================================
    3. Capabilities of https://start.spring.io
    4. =======================================
    5.  
    6. Available dependencies:
    7. -----------------------
    8. actuator - Actuator: Production ready features to help you monitor and manage your application
    9. ...
    10. web - Web: Support for full-stack web development, including Tomcat and spring-webmvc
    11. websocket - Websocket: Support for WebSocket development
    12. ws - WS: Support for Spring Web Services
    13.  
    14. Available project types:
    15. ------------------------
    16. gradle-build - Gradle Config [format:build, build:gradle]
    17. gradle-project - Gradle Project [format:project, build:gradle]
    18. maven-build - Maven POM [format:build, build:maven]
    19. maven-project - Maven Project [format:project, build:maven] (default)
    20.  
    21. ...

    init命令支持许多选项。查阅help输出了解更多细节。比如,下面的命令行创建一个使用Java 8和war打包的Gradle工程:

    1. $ spring init --build=gradle --java-version=1.8 --dependencies=websocket --packaging=war sample-app.zip
    2. Using service at https://start.spring.io
    3. Content saved to 'sample-app.zip'

    64.5 使用内嵌的Shell

    Spring Boot为BASH和zsh Shell包含命令行完成脚本。如果你不使用这些Shell中的任何一个(可能你是一个Windows用户),你可以使用shell命令来启动一个整个的shell,如下所示:

    1. $ spring shell
    2. Spring Boot (v2.0.4.RELEASE)
    3. Hit TAB to complete. Type \'help' and hit RETURN for help, and \'exit' to quit.

    在内嵌的shell内部,你可以直接运行其他命令:

    1. $ version
    2. Spring CLI v2.0.4.RELEASE

    该内嵌的Shell支持ANSI 颜色输出和tab补全。如果你需要运行一个原生的命令,你可以使用!前缀。要退出内嵌的shell,请按ctrl-c

    64.6 为CLI添加扩展

    你可以使用install命令添加扩展到CLI。该命令使用一个或多个artifact coordinates 集合(set),形如:group:artifact:version,如下所示:

    1. $ 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,如下所示:

    1. $ 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选项,如下:

    1. $ spring uninstall --all