For the latest stable version, please use Spring Cli 0.9.0! |
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 where the user-defined command was executed. If the file already exists, it will not be overwritten.
The content of the file is defined using the text
key.
Here’s a simple example of the generate
action is:
actions:
- generate:
to: hello.txt
text: Hello {{user-name}} on {{os-name}}.
The variables {{user-name}}
and `{{os-name}} will be replaced with actual values using 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, refer to 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, indentation, but some special characters must be escaped with a slash character.
Here’s an example of using 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 diffiuclt to embed text using the literal syntax due to required escaping. JSON files, regular expresssions, 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 leverage 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.
Here’s an example using the from
key
actions:
- generate:
to: hello.json
from: json-template.json
The to
key is relative to the director where the command is executed.
The file json-template.json
is located in the same directory as the command, .spring/commands/hello/create
and its contents are:
{
"operatingSystem": "{{os-name}}",
"phoneNumbers": [
{
"type": "iPhone",
"number": "0123-4567-8888"
},
{
"type": "home",
"number": "0123-4567-8910"
}
]
}
Executing spring hello create
from the introductory example produces the file hello.json
as shown below
$ 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.
Here is an example that 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, refer to 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:
to indicate the location where to inject the text.
Here is a sample file.
hello there
this is a test file
we are going to insert before the line that has the word marker1
marker2
An inject
action that will inject the text INJECTED AFTER
after the line that contains the word marker2
is:
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
An inject
action that will inject the text INJECTED BEFORE
before the line that contains the word marker1
is:
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 is used to inject Maven dependency entries into your Maven pom.xml file.
You can use Handlebars template variables and expressions inside the text:
field, and they will be processed.
The basic form to inject a Maven dependency is
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 is used to inject Maven dependency management entries into your Maven pom.xml file.
You can use Handlebars template variables and expressions inside the text:
field, and they will be processed.
The basic form to inject a Maven dependency is:
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 is used to inject Maven Build Plugin entrires into your Maven pom.xml file.
You can use Handlebars template variables and expressions inside the text:
field, and they will be processed.
The basic form to inject a Maven dependency is:
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 is used to inject Maven repository entries into your Maven pom.xml file.
You can use Handlebars template variables and expressions inside the text:
field, and they will be processed.
The basic form to inject a Maven repository is:
actions:
- inject-maven-repository:
text: |
<repository>
<id>spring-snapshots</id>
<url>https://repo.spring.io/snapshot</url>
</repository>