Action Guide
此页面描述您可以使用的操作:
This page describes the actions you can use:
Generate
generate
操作用于生成文件,它需要一个 to
键来指定目标路径。路径相对于执行用户自定义命令的位置。如果文件已存在,则不会覆盖它。
The generate
action is used to generate files. It requires a to
key to specify the destination path.
The path is relative to where the user-defined command was executed. If the file already exists, it is not overwritten.
使用 text
键定义文件内容。
The content of the file is defined by using the text
key.
以下示例展示了一个简单的 generate
操作:
The following example shows a simple generate
action:
actions:
- generate:
to: hello.txt
text: Hello {{user-name}} on {{os-name}}.
{{user-name}}
和 `{{os-name}} 变量由 Handlebars 模板引擎替换为实际值。传递给用户自定义命令的命令行选项会用作变量,供模板引擎使用。
The {{user-name}}
and `{{os-name}} variables are replaced with actual values by the Handlebars template engine.
Command line options passed to user-defined command are exposed as variables to be used by the template engine.
有关预定义模板引擎变量的更多信息,请参阅 Template Engine 部分。
For more information on predefined template engine variables, see the Template Engine section.
Literal Syntax
YAML 的文本语法允许表示多行字符串或保留字符串中的格式和空格。
YAML’s literal syntax allows representing multi-line strings or preserving formatting and whitespace within a string.
当您要保留换行符和缩进,但某些特殊字符必须使用反斜杠字符进行转义时,文本语法很有用。
The literal syntax is useful when you want to maintain line breaks and indentation but some special characters must be escaped with a slash character.
以下示例在 YAML 中使用了文本语法:
The following example uses the literal syntax in YAML:
actions:
- generate:
to: hello.txt
text: |
This is a multi-line
string using the literal syntax.
It preserves the line breaks
and indentation exactly as written.
\t This is a tab character.
\n This is a newline character.
\\ This is a backslash.
\u2713 This is a Unicode character (checkmark symbol).
通过使用 |
字符后跟缩进块,该字符串将被视为文本,并保留换行符和缩进。
By using the |
character followed by an indented block, the string is treated as a literal, and line breaks and indentation are preserved.
External File
在某些情况下,由于需要转义,很难使用文本语法嵌入文本。JSON 文件、正则表达式和文件路径是出现此类困难的常见示例。此外,您可能希望单独编辑文本内容,以使用文本编辑器的语法突出显示和验证功能。
In some cases, it is difficult to embed text by using the literal syntax, due to required escaping. JSON files, regular expressions, and file paths are common examples where such difficulties arise. Additionally, you may want to edit the text content separately from the action file to use the syntax highlighting and validation features of text editors.
要解决这些情况,可以使用 from
键指定用于生成文本的源文件。
To address these cases, you can use the from
key to specify the source file for generating the text.
以下示例使用了 from
键:
The following example uses the from
key:
actions:
- generate:
to: hello.json
from: json-template.json
to
键是相对于运行命令的目录。
The to
key is relative to the directory where the command is run.
json-template.json
文件位于与命令(.spring/commands/hello/create
)相同的目录中。以下清单显示了内容:
The json-template.json
file is located in the same directory as the command, .spring/commands/hello/create
The following listing shows its contents:
{
"operatingSystem": "{{os-name}}",
"phoneNumbers": [
{
"type": "iPhone",
"number": "0123-4567-8888"
},
{
"type": "home",
"number": "0123-4567-8910"
}
]
}
从 introductory example 运行 spring hello create
,会生成一个名为 hello.json
的文件,如下所示:
Running spring hello create
from the introductory example produces a file called hello.json
, as follows:
$ spring hello create
Generated /home/testing/rest-service/hello.json
$ cat hello.json
{
"operatingSystem": "Linux",
"phoneNumbers": [
{
"type": "iPhone",
"number": "0123-4567-8888"
},
{
"type": "home",
"number": "0123-4567-8910"
}
]
}
Variable Replacement in Keys
您还可以在 to
、from
和 text
键中使用 Handlebars 模板变量。
You can also use Handlebars template variables in the to
, from
, and text
keys.
以下示例在 to
键中使用 Handlebars 模板变量:
The following example uses Handlebars template variables in the to
key:
actions:
- generate:
to: src/main/java/{{root-package-dir}}/{{feature}}/{{capitalizeFirst feature}}Controller.java
from: RestController.java
有关预定义模板引擎变量的更多信息,请参阅 Template Engine 部分。
For more information on predefined template engine variables, see the Template Engine section.
Inject
inject
操作用于将文本注入文件。
The inject
action is used to inject text into a file.
需要定义 after:
键或 before:
键来指示要注入文本的位置。
You need to define either the after:
key or the before:
key to indicate the location where to inject the text.
以下清单显示了一个示例文件:
The following listing shows a sample file:
Hello there.
This is a test file.
We are going to insert before the line that has the word marker1
marker2
以下清单显示了一个 inject
操作,它在包含单词 marker2
的行后注入 INJECTED AFTER
:
The following listing shows an inject
action that injects INJECTED AFTER
after the line that contains the word marker2
:
actions:
- inject:
to: sample.txt
text: "INJECTED AFTER"
after: marker2
运行此操作后的文本文件为:
The text file after running this action is:
Hello there.
This is a test file.
We are going to insert before the line that has the word marker1
marker2
INJECTED AFTER
以下清单显示了一个 inject
操作,它在包含单词 marker1
的行前注入 INJECTED BEFORE
:
The following listing shows an inject
action that injects INJECTED BEFORE
before the line that contains the word marker1
:
actions:
- inject:
to: sample.txt
text: "INJECTED BEFORE"
before: marker1
运行此操作后的文本文件为:
The text file after running this action is:
Hello there.
This is a test file.
INJECTED BEFORE
We are going to insert before the line that has the word marker1
marker2
Exec
exec
操作运行一个 Shell 命令。
The exec
action runs a shell command.
以下清单显示了运行 Shell 命令的基本格式:
The following listing shows the basic form to run a shell command:
actions:
- exec:
command: mkdir {{tmp-dir}}/scratch
tmp-dir
模板引擎变量被默认定义,并且是 java.io.tmpdir
Java 系统属性的值。
The tmp-dir
template engine variable is defined by default and is the value of the java.io.tmpdir
Java System Property.
Inject Maven Dependency
inject-maven-dependency
操作向您的 Maven pom.xml 文件中注入 Maven 依赖项条目。
The inject-maven-dependency
action injects Maven dependency entries into your Maven pom.xml file.
您可以在 text:
域中使用 Handlebars 模板变量和表达式。
You can use Handlebars template variables and expressions inside the text:
field.
以下示例显示了注入 Maven 依赖项的基本语法:
The following example shows the basic syntax for injecting a Maven dependency:
actions:
- inject-maven-dependency:
text: |
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
Inject Maven Dependency Management
inject-maven-dependency-management
操作向您的 Maven pom.xml 文件中注入 Maven 依赖项管理条目。
The inject-maven-dependency-management
action injects Maven dependency management entries into your Maven pom.xml file.
您可以在 text:
域中使用 Handlebars 模板变量和表达式。
You can use Handlebars template variables and expressions inside the text:
field.
以下清单显示了注入 Maven 依赖项的基本语法:
The following listing shows the basic syntax to inject a Maven dependency:
actions:
- inject-maven-dependency-management:
text: |
<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-bom</artifactId>
<version>0.6.0.RELEASE</version>
<scope>import</scope>
<type>pom</type>
</dependency>
Inject Maven Build Plugin
inject-maven-build-plugin
操作向您的 Maven pom.xml 文件中注入 Maven 构建插件条目。
The inject-maven-build-plugin
action injects Maven Build Plugin entries into your Maven pom.xml file.
您可以在 text:
域中使用 Handlebars 模板变量和表达式。
You can use Handlebars template variables and expressions inside the text:
field.
以下示例显示了注入 Maven 依赖项的基本语法:
The following example shows the basic syntax to inject a Maven dependency:
actions:
- inject-maven-build-plugin:
text: |
<plugin>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-maven-plugin</artifactId>
<version>1.14.4</version>
<configuration>
<classPathDiscovery>true</classPathDiscovery>
</configuration>
<executions>
<execution>
<goals>
<goal>transform-extended</goal>
</goals>
</execution>
</executions>
</plugin>
Inject Maven Repository
inject-maven-repository
操作向您的 Maven pom.xml 文件中注入 Maven 存储库条目。
The inject-maven-repository
action injects Maven repository entries into your Maven pom.xml file.
您可以在 text:
域中使用 Handlebars 模板变量和表达式。
You can use Handlebars template variables and expressions inside the text:
field.
以下示例显示了注入 Maven 存储库的基本语法:
The following example shows the basic syntax to inject a Maven repository:
actions:
- inject-maven-repository:
text: |
<repository>
<id>spring-snapshots</id>
<url>https://repo.spring.io/snapshot</url>
</repository>