最新の安定バージョンについては、Spring Cli 0.9.0 を使用してください!

アクションガイド

生成

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

marker2 という単語を含む行の後にテキスト INJECTED AFTER を挿入する inject アクションは次のとおりです。

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

marker1 という単語を含む行の前にテキスト INJECTED BEFORE を挿入する inject アクションは次のとおりです。

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 アクションは、シェルコマンドを実行するために使用されます。

シェルコマンドを実行する基本的な形式は次のとおりです。

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

テンプレートエンジン変数 tmp-dir はデフォルトで定義されており、Java システムプロパティ java.io.tmpdir の値です。

出力のリダイレクト

TBD

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>