操作文件
Action 文件为 用户自定义命令 提供支持。 这些文件采用 YAML 格式编写,并存储在定义该命令的目录中。
有关用户定义命令的目录结构的更多信息,请参阅 Action 文件结构 文档。
每个文件包含一系列操作,这些操作按照文件中定义的顺序执行。 操作执行的任务通常用于帮助开发者向当前项目添加或修改代码和配置。 操作可以运行另一个可执行应用程序,这有助于自动化开发任务,例如使用提供商的 CLI 应用程序进行部署。
一个目录中可以存在多个操作文件,它们将按字母顺序进行评估。
| 评估顺序可能在未来的版本中发生变化。 |
目前,仅存在少量操作,但许多其他操作已完成原型设计,即将上线。
操作列表如下:
-
生成 - 创建新文件。
-
inject - 将文本注入到现有文件的特定位置。
-
inject-maven-dependency - 向当前的 pom.xml 文件追加一个依赖项条目。
-
inject-maven-plugin - 向当前的 pom.xml 文件追加一个 Maven 插件条目
-
inject-maven-dependency-management - 向当前的 pom.xml 文件追加一个依赖管理条目。
-
inject-maven-repository - 向当前的 pom.xml 文件追加一个仓库条目
-
inject-properties - 将属性追加到 Java 属性文件中。
-
exec - 运行另一个程序。
一个入门示例
CLI command new 命令创建一个简单的用户自定义命令,可用于演示操作文件的组成部分。
spring command new --commandName hello --subCommandName create
Created user defined command /home/testing/rest-service/.spring/commands/hello/create
目录结构是
$ tree .spring
.spring
└── commands
└── hello
└── create
├── command.yaml
└── hello.yaml
command.yaml 的内容(如下所示)定义了一个名为 greeting 的命令行参数。
该参数在 hello.yaml 操作文件中使用。
command:
description: Generate a new file with a hello message
options:
#
- name: greeting
description: who or what to say hello to
dataType: string
defaultValue: World
inputType: text # TEXT
```
hello.yaml 的内容是
actions:
- generate:
to: hello.txt
text: Hello {{greeting}} on {{os-name}}.
理解 actions 文件
为了帮助您理解如何使用 YAML 语法创建操作文件,本节将逐行解释引言中的示例:
| 代码 | Explanation. |
|---|---|
操作: |
将所有操作分组在一起。 |
生成: |
要采取的操作类型。例如,此操作类型会生成文件。 |
to: |
在文件系统中的哪个位置生成文件。 |
文本: |
生成文件的内容。 |
运行用户定义的命令
$ spring hello create --greeting World!
Generated /home/testing/rest-service/hello.txt
$ cat hello.txt
Hello World! on Linux.