Spring Boot CLI
The Spring Boot CLI (command-line interface) is a small tool that lets you create Spring Boot projects and run a few helper tasks from your terminal. With one spring init command you get the same project that the Spring Initializr website would generate, with your chosen build tool, Java version and dependencies, ready to open in any IDE. It is handy for scripts, for servers without a browser, and for developers who simply prefer the terminal.
This tutorial shows how to install the Spring Boot CLI, explains every command it offers in Spring Boot 4, and walks through creating, running and securing an order service from the terminal. It also clears up a common confusion: what happened to running Groovy scripts with the CLI.
1. What Is the Spring Boot CLI?
The Spring Boot CLI is a program called spring. It is not part of your application and does not run inside it. It is a developer tool, like git or mvn, released together with each Spring Boot version.
Its main job is to talk to Spring Initializr, the service behind start.spring.io that generates Spring Boot projects. The CLI sends your choices to that service and unpacks the project it returns. It also has a password encoder for Spring Security and an interactive shell.
2. Why Use the CLI
| Way to create a project | Best when |
|---|---|
| start.spring.io website | You want to browse and pick dependencies visually |
| IDE wizard (IntelliJ IDEA, VS Code, Eclipse) | You want the project opened in the IDE straight away |
| Spring Boot CLI | You work in a terminal, on a remote server, or need a repeatable command in a script or README |
curl to start.spring.io | You cannot install tools, for example inside a minimal CI container |
All four ask the same service, so the generated project is the same. The CLI's advantage is a short, readable command that a team can copy and share.
3. How to Install the Spring Boot CLI
The CLI needs Java 17 or later on your path. Choose one installation method:
3.1 SDKMAN (macOS, Linux, WSL)
SDKMAN manages versions of Java tools. It is the easiest way to switch between CLI versions.
sdk install springboot
spring --version
Spring CLI v4.2.0
3.2 Homebrew (macOS)
brew tap spring-io/tap
brew install spring-boot
3.3 MacPorts (macOS)
sudo port install spring-boot-cli
3.4 Scoop (Windows)
scoop bucket add extras
scoop install springboot
3.5 Manual Installation
Download spring-boot-cli-4.2.0-bin.zip (or .tar.gz) from the Spring Boot installation page, unpack it, and add its bin folder to your PATH. The folder contains a spring script and spring.bat for Windows.
4. Spring Boot CLI Commands
Run spring with no arguments to see the available commands:
| Command | What it does | Example |
|---|---|---|
init | Creates a new project using Spring Initializr | spring init -d=web demo |
encodepassword | Encodes a password for use with Spring Security | spring encodepassword s3cret |
shell | Starts an interactive shell with tab completion | spring shell |
version | Prints the CLI version | spring version |
help | Shows details for a command | spring help init |
The global option --debug prints extra information while a command runs, which helps when a network call fails.
spring run app.groovy, and had commands such as jar, war, grab and test. These were removed in Spring Boot 3.0. Old tutorials that start an application from a single Groovy file no longer apply; today you generate a normal project and run it with Maven or Gradle.5. Creating Projects with spring init
5.1 The Simplest Command
spring init demo
This creates a demo folder with a Maven project, the default Java version and only the core starter. If the location ends with .zip, the CLI saves the archive instead of extracting it.
5.2 Important Options
| Option | Meaning | Example value |
|---|---|---|
-d, --dependencies | Comma-separated dependency IDs | web,validation,actuator |
--build | Build system: maven (default) or gradle | gradle |
-j, --java-version | Java language level | 25 |
-b, --boot-version | Spring Boot version (defaults to the service's current default) | 4.2.0 |
-g, --group-id | Maven group ID | com.javabytechie |
-a, --artifact-id | Maven artifact ID | order-service |
-n, --name | Project name, also used for the main class name | order-service |
--package-name | Root Java package | com.javabytechie.shop.order |
-l, --language | java, kotlin or groovy | java |
-p, --packaging | jar or war | jar |
-x, --extract | Extract the archive (inferred when the location has no extension) | |
--target | URL of the Initializr service | https://start.example.com |
--list | Lists all dependency IDs and project types the service offers |
5.3 Finding Dependency IDs
Dependency IDs are short names, not Maven artifact names. web means Spring Web and brings spring-boot-starter-webmvc; data-jpa brings spring-boot-starter-data-jpa. List them all with:
spring init --list
=======================================
Capabilities of https://start.spring.io
=======================================
Available dependencies:
-----------------------
actuator - Actuator: Production ready features to help you monitor and manage your application
...
web - Web: Support for full-stack web development, including Tomcat and spring-webmvc
...
6. A Complete Example: An Order Service from the Terminal
6.1 Generate the Project
spring init \
--build=maven \
--java-version=25 \
--dependencies=web,validation,actuator \
--group-id=com.javabytechie \
--artifact-id=order-service \
--name=order-service \
--package-name=com.javabytechie.shop.order \
order-service
Using service at https://start.spring.io
Project extracted to '/home/dev/projects/order-service'
The folder contains pom.xml, the Maven wrapper, OrderServiceApplication.java in com.javabytechie.shop.order, an application.properties file and a test that checks the context starts. The project structure is explained in Spring Boot project structure.
6.2 Add an Endpoint
package com.javabytechie.shop.order;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Positive;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/orders")
public class OrderController {
private static final Logger log = LoggerFactory.getLogger(OrderController.class);
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public OrderConfirmation place(@Valid @RequestBody OrderRequest request) {
String orderId = UUID.randomUUID().toString();
log.info("Order {} received for {} x {}", orderId, request.quantity(), request.sku());
return new OrderConfirmation(orderId, request.sku(), request.quantity(), "RECEIVED");
}
public record OrderRequest(@NotBlank String sku, @Positive int quantity) {
}
public record OrderConfirmation(String orderId, String sku, int quantity, String status) {
}
}
6.3 Build, Run and Test
cd order-service
./mvnw spring-boot:run
# in a second terminal
curl -s -X POST http://localhost:8080/api/orders \
-H "Content-Type: application/json" \
-d '{"sku":"KB-100","quantity":2}'
{"orderId":"6f1c2a9e-4b7d-4f0e-9a51-2d3c8e7b1f40","sku":"KB-100","quantity":2,"status":"RECEIVED"}
The order ID is random, so yours will differ. From here, development continues with the normal build tool; the CLI's work is done.
7. Encoding Passwords with spring encodepassword
Spring Security never stores plain passwords. It stores a hash, with a prefix that names the algorithm, such as {bcrypt}. The CLI can produce such a value for configuration files and test data:
spring encodepassword ops-Pa55word
{bcrypt}$2a$10$Qm7l3vYc0bq9V1x5r4d3KuJmJ2b2y1x0s9r8q7p6o5n4m3l2k1j0i
The -a (--algorithm) option chooses the encoder: default (Spring Security's delegating encoder, which uses bcrypt and adds the prefix), bcrypt or pbkdf2. For example, set the default user for local testing when the Spring Security starter is on the classpath:
spring.security.user.name=ops
spring.security.user.password={bcrypt}$2a$10$Qm7l3vYc0bq9V1x5r4d3KuJmJ2b2y1x0s9r8q7p6o5n4m3l2k1j0i
A hash is safer than a plain password but is still a credential. For real environments, keep it out of version control; see managing secrets in Spring Boot.
8. The Embedded Shell and Command Completion
spring shell opens an interactive shell where you type commands without the spring prefix and press Tab to complete them. A ! prefix runs a normal operating system command, and Ctrl+C exits.
$ spring shell
Spring Boot (v4.2.0)
Hit TAB to complete. Type 'help' and hit RETURN for help, and 'exit' to quit.
$ version
Spring CLI v4.2.0
On bash and zsh, the CLI ships completion scripts, which Homebrew and MacPorts register for you. With SDKMAN, source the script from ~/.sdkman/candidates/springboot/current/shell-completion/.
9. Common Problems and Fixes
| Problem | Cause | Fix |
|---|---|---|
spring: command not found | The bin folder is not on PATH | Open a new terminal after installing, or add the folder to PATH |
Another spring command runs | A different tool with the same name is earlier on PATH | Check with which spring and reorder PATH |
| Unknown dependency ID | The ID does not exist or was renamed | Check spring init --list |
| The project uses an unexpected Boot version | The service default changed | Pin it with -b |
| Connection errors behind a proxy | The CLI cannot reach start.spring.io | Configure the JVM proxy settings, or point --target to an internal Initializr |
spring run is not recognized | Removed in Spring Boot 3.0 | Generate a project and use ./mvnw spring-boot:run |
10. Best Practices
- Pin important options such as
--java-version,--buildand--package-namein shared commands, so every developer gets the same project. - Keep the command in your README to document how the service was created and to make new similar services quickly.
- Use
--listinstead of guessing IDs; dependency IDs are short names, not artifact IDs. - Upgrade the CLI with Spring Boot (for example
sdk upgrade springboot) so its defaults match the version you use. - Point
--targetto a company Initializr if your organization publishes its own defaults. - Treat encoded passwords as secrets and keep them out of version control.
11. Summary
The Spring Boot CLI is a terminal client for Spring Initializr plus a few helpers. Install it with SDKMAN, Homebrew, MacPorts, Scoop or a ZIP file, create projects with spring init and the options for dependencies, build tool, Java version and package, encode Spring Security passwords with spring encodepassword, and use spring shell for tab completion. Since Spring Boot 3.0 it no longer runs Groovy scripts, so after generating a project you build and run it with Maven or Gradle as usual. Keep the exact spring init command in your documentation, and every new service starts the same way.
12. References
Spring Boot CLI Interview Questions and Answers
Common interview questions on Spring Boot CLI, from basic to scenario-based, with short answers you can explain in your own words.
Q. What is the Spring Boot CLI, and which commands does it provide in Spring Boot 4?
Answer: It is a command-line developer tool named spring, released with each Spring Boot version. In Spring Boot 4 it provides init to generate projects through Spring Initializr, encodepassword to hash passwords for Spring Security, shell for an interactive shell with completion, plus version and help. It is not a dependency of your application.
Q. Can you still run a Groovy script with spring run?
Answer: No. Running Groovy scripts and the related commands such as jar, war, grab and test were removed in Spring Boot 3.0. The CLI now focuses on project creation and helpers. Generate a normal project with spring init and run it with ./mvnw spring-boot:run or ./gradlew bootRun.
Q. How do you generate a Gradle project with Java 25 and the web and JPA dependencies from the CLI?
Answer: Run spring init --build=gradle --java-version=25 --dependencies=web,data-jpa my-service. Dependency values are Initializr IDs, not Maven artifact names; spring init --list shows all valid IDs and project types. Add --package-name and --group-id to control the generated package and coordinates.
Q. What does spring encodepassword output, and why does the value start with {bcrypt}?
Answer: It outputs a hash of the given password created with Spring Security's delegating password encoder by default, which uses bcrypt. The {bcrypt} prefix tells the DelegatingPasswordEncoder which algorithm to use when verifying, so an application can support several algorithms and migrate between them. The -a option can choose bcrypt or pbkdf2 directly.
Q. Your company hosts its own Spring Initializr with internal defaults. How do you use it from the CLI?
Answer: Pass its URL with --target, for example spring init --target=https://start.internal.example.com -d=web orders. The CLI uses the same API, so --list then shows the company's dependencies and defaults. Wrapping that command in a small script or documenting it in a template repository gives every team the same starting point.