操作指南

生成

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"
    }
  ]
}

鍵中的變數替換

您還可以在 tofromtext 鍵中使用 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

執行

exec 操作執行 shell 命令。

以下列表顯示了執行 shell 命令的基本形式

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

tmp-dir 模板引擎變數是預設定義的,其值是 java.io.tmpdir Java 系統屬性的值。

重定向輸出

待定

注入 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>

注入 Maven 倉庫

inject-maven-repository 操作將 Maven 倉庫條目注入到您的 Maven pom.xml 檔案中。

您可以在 text: 欄位內部使用 Handlebars 模板變數和表示式。

以下示例顯示了注入 Maven 倉庫的基本語法

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