Action Guide

Generate

The generate action is used to generate files. It requires a to key to specify the destination path. The path is relative to where the user-defined command was executed. If the file already exists, it is not overwritten.spring-doc.cn

The content of the file is defined by using the text key.spring-doc.cn

The following example shows a simple generate action:spring-doc.cn

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

The {{user-name}} and {{os-name}} variables are replaced with actual values by the Handlebars template engine. Command line options passed to user-defined command are exposed as variables to be used by the template engine.spring-doc.cn

For more information on predefined template engine variables, see the Template Engine section.spring-doc.cn

Literal Syntax

YAML’s literal syntax allows representing multi-line strings or preserving formatting and whitespace within a string.spring-doc.cn

The literal syntax is useful when you want to maintain line breaks and indentation but some special characters must be escaped with a slash character.spring-doc.cn

The following example uses the literal syntax in YAML:spring-doc.cn

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

By using the | character followed by an indented block, the string is treated as a literal, and line breaks and indentation are preserved.spring-doc.cn

External File

In some cases, it is difficult to embed text by using the literal syntax, due to required escaping. JSON files, regular expressions, and file paths are common examples where such difficulties arise. Additionally, you may want to edit the text content separately from the action file to use the syntax highlighting and validation features of text editors.spring-doc.cn

To address these cases, you can use the from key to specify the source file for generating the text.spring-doc.cn

The following example uses the from key:spring-doc.cn

actions:
  - generate:
      to:  hello.json
      from: json-template.json

The to key is relative to the directory where the command is run.spring-doc.cn

The json-template.json file is located in the same directory as the command, .spring/commands/hello/create The following listing shows its contents:spring-doc.cn

{
  "operatingSystem": "{{os-name}}",
  "phoneNumbers": [
    {
      "type": "iPhone",
      "number": "0123-4567-8888"
    },
    {
      "type": "home",
      "number": "0123-4567-8910"
    }
  ]
}

Running spring hello create from the introductory example produces a file called hello.json, as follows:spring-doc.cn

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

Variable Replacement in Keys

You can also use Handlebars template variables in the to, from, and text keys.spring-doc.cn

The following example uses Handlebars template variables in the to key:spring-doc.cn

actions:
  - generate:
      to: src/main/java/{{root-package-dir}}/{{feature}}/{{capitalizeFirst feature}}Controller.java
      from: RestController.java

For more information on predefined template engine variables, see the Template Engine section.spring-doc.cn

Inject

The inject action is used to inject text into a file.spring-doc.cn

You need to define either the after: key or the before: key to indicate the location where to inject the text.spring-doc.cn

The following listing shows a sample file:spring-doc.cn

Hello there.
This is a test file.
We are going to insert before the line that has the word marker1
marker2

The following listing shows an inject action that injects INJECTED AFTER after the line that contains the word marker2:spring-doc.cn

actions:
  - inject:
      to: sample.txt
      text: "INJECTED AFTER"
      after: marker2

The text file after running this action is:spring-doc.cn

Hello there.
This is a test file.
We are going to insert before the line that has the word marker1
marker2
INJECTED AFTER

The following listing shows an inject action that injects INJECTED BEFORE before the line that contains the word marker1:spring-doc.cn

actions:
  - inject:
      to: sample.txt
      text: "INJECTED BEFORE"
      before: marker1

The text file after running this action is:spring-doc.cn

Hello there.
This is a test file.
INJECTED BEFORE
We are going to insert before the line that has the word marker1
marker2

Exec

The exec action runs a shell command.spring-doc.cn

The following listing shows the basic form to run a shell command:spring-doc.cn

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

The tmp-dir template engine variable is defined by default and is the value of the java.io.tmpdir Java System Property.spring-doc.cn

Redirecting Output

Inject Maven Dependency

The inject-maven-dependency action injects Maven dependency entries into your Maven pom.xml file.spring-doc.cn

You can use Handlebars template variables and expressions inside the text: field.spring-doc.cn

The following example shows the basic syntax for injecting a Maven dependency:spring-doc.cn

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>

Inject Maven Dependency Management

The inject-maven-dependency-management action injects Maven dependency management entries into your Maven pom.xml file.spring-doc.cn

You can use Handlebars template variables and expressions inside the text: field.spring-doc.cn

The following listing shows the basic syntax to inject a Maven dependency:spring-doc.cn

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>

Inject Maven Build Plugin

The inject-maven-build-plugin action injects Maven Build Plugin entries into your Maven pom.xml file.spring-doc.cn

You can use Handlebars template variables and expressions inside the text: field.spring-doc.cn

The following example shows the basic syntax to inject a Maven dependency:spring-doc.cn

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>

Inject Maven Repository

The inject-maven-repository action injects Maven repository entries into your Maven pom.xml file.spring-doc.cn

You can use Handlebars template variables and expressions inside the text: field.spring-doc.cn

The following example shows the basic syntax to inject a Maven repository:spring-doc.cn

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