操作指南
本页面描述了您可以使用的操作
生成
generate 操作用于生成文件。它需要一个 to 键来指定目标路径。该路径是相对于用户定义命令执行位置的。如果文件已存在,则不会被覆盖。
文件内容通过使用 text 键来定义。
以下示例展示了一个简单的 generate 操作
actions:
- generate:
to: hello.txt
text: Hello {{user-name}} on {{os-name}}.
{{user-name}} 和 {{os-name}} 变量会被 Handlebars 模板引擎替换为实际值。传递给用户定义命令的命令行选项会作为变量暴露给模板引擎使用。
有关预定义模板引擎变量的更多信息,请参见模板引擎部分。
字面量语法
YAML 的字面量语法允许表示多行字符串或保留字符串中的格式和空白符。
当您希望保留换行符和缩进,但某些特殊字符必须用斜杠字符转义时,字面量语法非常有用。
以下示例在 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).
通过使用 | 字符后跟缩进块,该字符串将被视为字面量,并且换行符和缩进会被保留。
外部文件
在某些情况下,由于需要转义,使用字面量语法嵌入文本会很困难。JSON 文件、正则表达式和文件路径是出现此类困难的常见示例。此外,您可能希望将文本内容与操作文件分开编辑,以便使用文本编辑器的语法高亮和验证功能。
为了解决这些情况,您可以使用 from 键来指定用于生成文本的源文件。
以下示例使用了 from 键
actions:
- generate:
to: hello.json
from: json-template.json
to 键是相对于运行命令的目录的。
json-template.json 文件位于与命令相同的目录 .spring/commands/hello/create 中。以下列表显示了其内容
{
"operatingSystem": "{{os-name}}",
"phoneNumbers": [
{
"type": "iPhone",
"number": "0123-4567-8888"
},
{
"type": "home",
"number": "0123-4567-8910"
}
]
}
从入门示例运行 spring hello create 会生成一个名为 hello.json 的文件,如下所示
$ 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"
}
]
}
键中的变量替换
您还可以在 to、from 和 text 键中使用 Handlebars 模板变量。
以下示例在 to 键中使用了 Handlebars 模板变量
actions:
- generate:
to: src/main/java/{{root-package-dir}}/{{feature}}/{{capitalizeFirst feature}}Controller.java
from: RestController.java
有关预定义模板引擎变量的更多信息,请参见模板引擎部分。
注入
inject 操作用于将文本注入到文件中。
您需要定义 after: 键或 before: 键来指示注入文本的位置。
以下列表显示了一个示例文件
Hello there.
This is a test file.
We are going to insert before the line that has the word marker1
marker2
以下列表显示了一个 inject 操作,它将 INJECTED AFTER 注入到包含单词 marker2 的行之后
actions:
- inject:
to: sample.txt
text: "INJECTED AFTER"
after: marker2
运行此操作后的文本文件是
Hello there.
This is a test file.
We are going to insert before the line that has the word marker1
marker2
INJECTED AFTER
以下列表显示了一个 inject 操作,它将 INJECTED BEFORE 注入到包含单词 marker1 的行之前
actions:
- inject:
to: sample.txt
text: "INJECTED BEFORE"
before: marker1
运行此操作后的文本文件是
Hello there.
This is a test file.
INJECTED BEFORE
We are going to insert before the line that has the word marker1
marker2
注入 Maven 依赖
inject-maven-dependency 操作将 Maven 依赖条目注入到您的 Maven pom.xml 文件中。
您可以在 text: 字段内部使用 Handlebars 模板变量和表达式。
以下示例显示了注入 Maven 依赖的基本语法
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>
注入 Maven 依赖管理
inject-maven-dependency-management 操作将 Maven 依赖管理条目注入到您的 Maven pom.xml 文件中。
您可以在 text: 字段内部使用 Handlebars 模板变量和表达式。
以下列表显示了注入 Maven 依赖的基本语法
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>
注入 Maven 构建插件
inject-maven-build-plugin 操作将 Maven 构建插件条目注入到您的 Maven pom.xml 文件中。
您可以在 text: 字段内部使用 Handlebars 模板变量和表达式。
以下示例显示了注入 Maven 构建插件的基本语法
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>