Spring Boot CLI 命令

x33g5p2x  于2022-10-05 转载在 Spring  
字(1.9k)|赞(0)|评价(0)|浏览(670)

Spring Boot CLI 是创建/管理项目的强大工具。通过使用“–help”参数执行命令,您将看到可用选项列表:

  1. ./spring --help
  2. usage: spring [--help] [--version]
  3.        <command> [<args>]
  4. Available commands are:
  5.   run [options] <files> [--] [args]
  6.     Run a spring groovy script
  7.   grab                
  8.     Download a spring groovy script's dependencies to ./repository
  9.   jar [options] <jar-name> <files>
  10.     Create a self-contained executable jar file from a Spring Groovy script
  11.   war [options] <war-name> <files>
  12.     Create a self-contained executable war file from a Spring Groovy script
  13.   install [options] <coordinates>
  14.     Install dependencies to the lib/ext directory
  15.   uninstall [options] <coordinates>
  16.     Uninstall dependencies from the lib/ext directory
  17.   init [options] [location]
  18.     Initialize a new project using Spring Initializr (start.spring.io)
  19.   encodepassword [options] <password to encode>
  20.     Encode a password for use with Spring Security
  21.   shell                
  22.     Start a nested shell

让我们更详细地了解每个命令。

运行命令

run 命令将允许您运行 Java 或 Groovy Spring Boot 应用程序。下面是它的示例用法,它启动了一个名为“myapp.groovy”的应用程序,覆盖了默认的 Web 服务器端口:

  1. $ spring run myapp.groovy -- --server.port=8081

如果您有多个文件,则可以使用通配符 * 来编译所有文件。只需执行以下命令:

  1. $ spring run *.groovy

测试命令

test 命令运行 Spring Groovy 脚本和 Java 测试。要运行它,请执行:

  1. $ spring test test.groovy

抓取命令

抓取命令会将所有 Spring Groovy 脚本和 Java 依赖项下载到 ./repository
目录。例子:

  1. $ spring grab MyApplication.java

jar 命令

jar 命令将从 Groovy 或 Java 脚本创建一个自包含的可执行 JAR 文件。例子:

  1. $ spring jar myapp.jar app.groovy

生成的 app.jar 是一个胖 JAR,可以使用以下命令执行:

  1. $ java -jar myapp.jar

war 命令

这与前面的命令非常相似。 war 命令将创建一个独立的可执行文件
来自 Groovy 或 Java 脚本的 WAR 文件。例子:

  1. $ spring war app.war app.groovy

安装命令

install 命令与 grab 命令非常相似;唯一的区别是您需要指定要安装的库(以坐标格式 groupId:artifactId:version ;与@Grab 注释相同)。它将下载它和 lib 目录中的依赖项。例子:

  1. $ spring install org.spockframework:spock-core:1.0-groovy-2.4

您将在 lib 目录中拥有 Spock 库及其依赖项。

卸载命令

卸载命令将从 lib 目录中卸载依赖项。您可以通过执行以下命令来测试此命令:

  1. $ spring uninstall org.spockframework:spock-core:1.0-groovy-2.4

初始化命令

init 命令将帮助您使用 Spring Initializr ( http://start.spring.io/ ) 初始化一个新项目。无论您是否使用 IDE,此命令都将帮助您做好一切准备,开始开发 Spring Boot 应用程序。查看本教程以了解有关使用 Spring init 引导您的项目的更多信息:Using Spring boot CLI to bootstrap your projects

相关文章