作指南

生成

generate作用于生成文件。它需要一个to键指定目标路径。 该路径是相对于执行用户定义命令的位置的。如果文件已存在,则不会被覆盖。spring-doc.cadn.net.cn

文件的内容是使用text钥匙。spring-doc.cadn.net.cn

以下示例显示了一个简单的generate行动:spring-doc.cadn.net.cn

actions:
  - generate:
      to:  hello.txt
      text: Hello {{user-name}} on {{os-name}}.

{{user-name}}{{os-name}}变量由 Handlebars 模板引擎替换为实际值。 传递给用户定义命令的命令行选项将作为模板引擎使用的变量公开。spring-doc.cadn.net.cn

有关预定义模板引擎变量的更多信息,请参阅模板引擎部分。spring-doc.cadn.net.cn

文本语法

YAML 的文字语法允许表示多行字符串或保留字符串中的格式和空格。spring-doc.cadn.net.cn

当您想要保持换行符和缩进,但某些特殊字符必须使用斜杠字符进行转义时,文本语法非常有用。spring-doc.cadn.net.cn

以下示例使用 YAML 中的 Literal 语法:spring-doc.cadn.net.cn

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).

通过使用|字符后跟缩进块,则字符串将被视为文本,并保留换行符和缩进。spring-doc.cadn.net.cn

外部文件

在某些情况下,由于需要转义,因此很难使用 Literal 语法嵌入文本。 JSON 文件、正则表达式和文件路径是出现此类困难的常见示例。 此外,您可能希望将文本内容与作文件分开编辑,以使用文本编辑器的语法突出显示和验证功能。spring-doc.cadn.net.cn

要解决这些情况,您可以使用from键指定用于生成文本的源文件。spring-doc.cadn.net.cn

以下示例使用from钥匙:spring-doc.cadn.net.cn

actions:
  - generate:
      to:  hello.json
      from: json-template.json

tokey 是相对于运行命令的目录。spring-doc.cadn.net.cn

json-template.jsonfile 与命令.spring/commands/hello/create以下清单显示了其内容:spring-doc.cadn.net.cn

{
  "operatingSystem": "{{os-name}}",
  "phoneNumbers": [
    {
      "type": "iPhone",
      "number": "0123-4567-8888"
    },
    {
      "type": "home",
      "number": "0123-4567-8910"
    }
  ]
}

运行spring hello create介绍性示例中生成一个名为hello.json如下:spring-doc.cadn.net.cn

$ 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,fromtext钥匙。spring-doc.cadn.net.cn

以下示例在to钥匙:spring-doc.cadn.net.cn

actions:
  - generate:
      to: src/main/java/{{root-package-dir}}/{{feature}}/{{capitalizeFirst feature}}Controller.java
      from: RestController.java

有关预定义模板引擎变量的更多信息,请参阅模板引擎部分。spring-doc.cadn.net.cn

注入

injectaction 用于将文本注入到文件中。spring-doc.cadn.net.cn

您需要定义after:键或before:键指示要注入文本的位置。spring-doc.cadn.net.cn

下面的清单显示了一个示例文件:spring-doc.cadn.net.cn

Hello there.
This is a test file.
We are going to insert before the line that has the word marker1
marker2

下面的清单显示了一个injectaction 中注入INJECTED AFTER在包含单词的行之后marker2:spring-doc.cadn.net.cn

actions:
  - inject:
      to: sample.txt
      text: "INJECTED AFTER"
      after: marker2

运行此作后的文本文件为:spring-doc.cadn.net.cn

Hello there.
This is a test file.
We are going to insert before the line that has the word marker1
marker2
INJECTED AFTER

下面的清单显示了一个injectaction 中注入INJECTED BEFORE在包含单词的行之前marker1:spring-doc.cadn.net.cn

actions:
  - inject:
      to: sample.txt
      text: "INJECTED BEFORE"
      before: marker1

运行此作后的文本文件为:spring-doc.cadn.net.cn

Hello there.
This is a test file.
INJECTED BEFORE
We are going to insert before the line that has the word marker1
marker2

执行

execaction 运行 shell 命令。spring-doc.cadn.net.cn

下面的清单显示了运行 shell 命令的基本形式:spring-doc.cadn.net.cn

actions:
  - exec:
      command: mkdir {{tmp-dir}}/scratch

tmp-dirtemplate engine 变量是默认定义的,它是java.io.tmpdirJava 系统属性。spring-doc.cadn.net.cn

重定向输出

注入 Maven 依赖

inject-maven-dependencyaction 将 Maven 依赖项条目注入 Maven pom.xml 文件中。spring-doc.cadn.net.cn

您可以在text:田。spring-doc.cadn.net.cn

以下示例显示了注入 Maven 依赖项的基本语法:spring-doc.cadn.net.cn

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-managementaction 将 Maven 依赖项管理条目注入 Maven pom.xml 文件中。spring-doc.cadn.net.cn

您可以在text:田。spring-doc.cadn.net.cn

下面的清单显示了注入 Maven 依赖项的基本语法:spring-doc.cadn.net.cn

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 Build Plugin 条目注入 Maven pom.xml 文件中。spring-doc.cadn.net.cn

您可以在text:田。spring-doc.cadn.net.cn

以下示例显示了注入 Maven 依赖项的基本语法:spring-doc.cadn.net.cn

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>

注入 Maven 仓库

inject-maven-repositoryaction 将 Maven 存储库条目注入 Maven pom.xml 文件中。spring-doc.cadn.net.cn

您可以在text:田。spring-doc.cadn.net.cn

以下示例显示了注入 Maven 存储库的基本语法:spring-doc.cadn.net.cn

actions:
  - inject-maven-repository:
      text: |
        <repository>
          <id>spring-snapshots</id>
          <url>https://repo.spring.io/snapshot</url>
        </repository>