Action Guide
This page describes the actions you can use:
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.
The content of the file is defined by using the text
key.
The following example shows a simple generate
action:
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.
For more information on predefined template engine variables, see the Template Engine section.
Literal Syntax
YAML’s literal syntax allows representing multi-line strings or preserving formatting and whitespace within a string.
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.
The following example uses the literal syntax in 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).
By using the |
character followed by an indented block, the string is treated as a literal, and line breaks and indentation are preserved.
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.
To address these cases, you can use the from
key to specify the source file for generating the text.
The following example uses the from
key:
actions:
- generate:
to: hello.json
from: json-template.json
The to
key is relative to the directory where the command is run.
The json-template.json
file is located in the same directory as the command, .spring/commands/hello/create
The following listing shows its contents:
{
"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 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.
The following example uses Handlebars template variables in the to
key:
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.
Inject
The inject
action is used to inject text into a file.
You need to define either the after:
key or the before:
key to indicate the location where to inject the text.
The following listing shows a sample file:
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
:
actions:
- inject:
to: sample.txt
text: "INJECTED AFTER"
after: marker2
The text file after running this action is:
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
:
actions:
- inject:
to: sample.txt
text: "INJECTED BEFORE"
before: marker1
The text file after running this action is:
Hello there.
This is a test file.
INJECTED BEFORE
We are going to insert before the line that has the word marker1
marker2
Inject Maven Dependency
The inject-maven-dependency
action injects Maven dependency entries into your Maven pom.xml file.
You can use Handlebars template variables and expressions inside the text:
field.
The following example shows the basic syntax for injecting a Maven dependency:
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.
You can use Handlebars template variables and expressions inside the text:
field.
The following listing shows the basic syntax to inject a Maven dependency:
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.
You can use Handlebars template variables and expressions inside the text:
field.
The following example shows the basic syntax to inject a Maven dependency:
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.
You can use Handlebars template variables and expressions inside the text:
field.
The following example shows the basic syntax to inject a Maven repository:
actions:
- inject-maven-repository:
text: |
<repository>
<id>spring-snapshots</id>
<url>https://repo.spring.io/snapshot</url>
</repository>