操作檔案

操作檔案為 使用者定義命令 提供支援。這些檔案以 YAML 格式編寫,並存儲在定義命令的目錄中。

有關使用者定義命令的目錄結構的更多資訊,請參閱 操作檔案結構 文件。

每個檔案包含一系列操作,這些操作按照它們在檔案中定義的順序執行。操作執行的任務通常用於幫助開發人員向其當前專案新增或修改程式碼和配置。操作可以執行另一個可執行應用程式,這有助於自動化開發任務,例如使用供應商的 CLI 應用程式進行部署。

一個目錄中可以有多個操作檔案,它們按字母順序進行評估。

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

目前,只有少數操作存在,但更多操作已經過原型設計並將很快可用。

操作列表如下:

  • generate - 建立一個新檔案。

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

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

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

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

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

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

  • exec - 執行另一個程式。

入門示例

CLI 的 command new 命令建立一個簡單的使用者定義命令,我們可以用它來演示操作檔案的元件。

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 操作檔案中使用。

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

理解操作檔案

為了幫助您理解 YAML 語法如何用於建立操作檔案,本節解釋了介紹性示例的每一行。

程式碼 解釋。

操作

將所有操作分組。

generate

要執行的操作型別。例如,此操作型別生成檔案。

在檔案系統中生成檔案的位置。

text

要生成的檔案內容。

執行使用者定義命令

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

$ cat hello.txt
Hello World! on Linux.

下一步

© . This site is unofficial and not affiliated with VMware.