Action Files

Action files power user defined commands. These files are written in YAML format and are stored in the directory that defines the command.

See the documentation on Action file Structure for more information on the directory structure for user-defined commands.

Each file contains a series of actions that are run in the order in which they are defined in the file. An action performs a task that is commonly needed to help developers add or modify code and configuration to their current project. An action can run another executable application, which helps automate development tasks, such as deployment using a vendor’s CLI application.

There can be multiple action files in a directory, and they are evaluated in alphabetical order.

The order of evaluation may change in a future release.

Currently, only a few actions exist, but many more have been prototypes and will be available soon.

The list of actions are:

  • generate - Create a new file.

  • inject - Inject text into a specific location in an existing file.

  • inject-maven-dependency - Append a dependency entry to the current pom.xml file.

  • inject-maven-plugin - Append a Maven plugin entry to the current pom.xml file

  • inject-maven-dependency-management - Append a dependency management entry to the current pom.xml file.

  • inject-maven-repository - Append a repository entry to the current pom.xml file

  • inject-properties - Append properties to a Java properties file.

  • exec - Run another program.

An Introductory Example

The CLI command new command creates a simple user-defined command that we can use to demonstrate the components of an actions file.

spring command new --commandName hello --subCommandName create
Created user defined command /home/testing/rest-service/.spring/commands/hello/create

The directory structure is

$ tree .spring
.spring
└── commands
    └── hello
        └── create
            ├── command.yaml
            └── hello.yaml

The content of command.yaml, shown below, defines a command line argument named greeting. This argument is used in the hello.yaml action file.

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
```

The content of hello.yaml is

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

Understanding the actions file

To help you understand how YAML syntax is used to create an actions file, this section explains each line of the introduction’s example:

Code Explanation.

actions:

Groups together all the actions.

generate:

The type of action to take. For example, this action type generates files.

to:

Where in the file system to generate the file.

text:

The content of the file to generate.

Running the User-defined Command

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

$ cat hello.txt
Hello World! on Linux.

Next Steps