Action Files

Action files 為使用者自訂命令提供強大功能。這些檔案以 YAML 格式撰寫,並儲存在定義命令的目錄中。

請參閱Action file 結構文件,以取得使用者自訂命令目錄結構的更多資訊。

每個檔案都包含一系列動作,這些動作會按照檔案中定義的順序執行。動作會執行一項常見任務,以協助開發人員將程式碼和組態新增或修改到目前的專案。動作可以執行另一個可執行應用程式,這有助於自動化開發任務,例如使用供應商的 CLI 應用程式進行部署。

一個目錄中可以有多個 action files,它們會依字母順序評估。

評估順序可能會在未來的版本中變更。

目前,只有少數動作存在,但已製作許多原型,並將很快推出。

動作列表如下

  • generate - 建立新檔案。

  • inject - 將文字注入到現有檔案中的特定位置。

  • inject-maven-dependency - 將 dependency 條目附加到目前的 pom.xml 檔案。

  • inject-maven-plugin - 將 Maven plugin 條目附加到目前的 pom.xml 檔案

  • inject-maven-dependency-management - 將 dependency management 條目附加到目前的 pom.xml 檔案。

  • inject-maven-repository - 將 repository 條目附加到目前的 pom.xml 檔案

  • inject-properties - 將屬性附加到 Java 屬性檔案。

  • exec - 執行另一個程式。

簡介範例

CLI command new 命令會建立一個簡單的使用者自訂命令,我們可以用它來示範 actions file 的組件。

spring command new --commandName hello --subCommandName create
Created user defined command /home/testing/rest-service/.spring/commands/hello/create

目錄結構如下

$ tree .spring
.spring
└── commands
    └── hello
        └── create
            ├── command.yaml
            └── hello.yaml

下面顯示的 command.yaml 內容定義了一個名為 greeting 的命令列引數。此引數用於 hello.yaml action file 中。

command:
  description: Generate a new file with a hello message
  options:
    #
    - name: greeting
      description: who or what to say hello to
      dataType: string
      defaultValue: World
      inputType: text     # TEXT
```

hello.yaml 的內容為

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

瞭解 actions file

為了幫助您瞭解如何使用 YAML 語法建立 actions file,本節將說明簡介範例的每一行

程式碼 說明。

actions

將所有動作組合在一起。

generate

要採取的動作類型。例如,此動作類型會產生檔案。

to

檔案系統中產生檔案的位置。

text

要產生的檔案內容。

執行使用者自訂命令

$ spring hello create --greeting World!
Generated /home/testing/rest-service/hello.txt

$ cat hello.txt
Hello World! on Linux.

下一步