操作指南

產生

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 動作,該動作在包含單字 marker2 的行之後注入 INJECTED AFTER

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 動作,該動作在包含單字 marker1 的行之前注入 INJECTED BEFORE

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>