00:00

Spring Boot CLI

Last updated:

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.

How spring init creates a projectYou run spring init in a terminal with options such as dependencies, build tool and Java version. The CLI sends these options over HTTPS to the Spring Initializr service at start.spring.io, which generates the project and returns it as an archive. The CLI then extracts the archive into a folder, giving you a ready project with a build file, wrapper, main class and test. Terminal spring init --dependencies=web,... order-service start.spring.io Spring Initializr generates the project ./order-service pom.xml, mvnw, OrderServiceApplication.java, test class options archive The CLI needs network access to the Initializr service (or to your own instance with --target)
spring init is a terminal client for Spring Initializr; the project is generated by start.spring.io and unpacked locally.

2. Why Use the CLI

Way to create a projectBest when
start.spring.io websiteYou 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 CLIYou work in a terminal, on a remote server, or need a repeatable command in a script or README
curl to start.spring.ioYou 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.

Terminal
sdk install springboot
spring --version
Output
Spring CLI v4.2.0

3.2 Homebrew (macOS)

Terminal
brew tap spring-io/tap
brew install spring-boot

3.3 MacPorts (macOS)

Terminal
sudo port install spring-boot-cli

3.4 Scoop (Windows)

PowerShell
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:

CommandWhat it doesExample
initCreates a new project using Spring Initializrspring init -d=web demo
encodepasswordEncodes a password for use with Spring Securityspring encodepassword s3cret
shellStarts an interactive shell with tab completionspring shell
versionPrints the CLI versionspring version
helpShows details for a commandspring help init

The global option --debug prints extra information while a command runs, which helps when a network call fails.

Version note: Before Spring Boot 3.0, the CLI could compile and run Groovy scripts with 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

Terminal
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

OptionMeaningExample value
-d, --dependenciesComma-separated dependency IDsweb,validation,actuator
--buildBuild system: maven (default) or gradlegradle
-j, --java-versionJava language level25
-b, --boot-versionSpring Boot version (defaults to the service's current default)4.2.0
-g, --group-idMaven group IDcom.javabytechie
-a, --artifact-idMaven artifact IDorder-service
-n, --nameProject name, also used for the main class nameorder-service
--package-nameRoot Java packagecom.javabytechie.shop.order
-l, --languagejava, kotlin or groovyjava
-p, --packagingjar or warjar
-x, --extractExtract the archive (inferred when the location has no extension) 
--targetURL of the Initializr servicehttps://start.example.com
--listLists 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:

Terminal
spring init --list
Output (excerpt)
=======================================
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

Terminal
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
Output
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

OrderController.java
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

Terminal
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}'
Response
{"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:

Terminal
spring encodepassword ops-Pa55word
Output (the hash is different every time)
{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:

application.properties
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 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

ProblemCauseFix
spring: command not foundThe bin folder is not on PATHOpen a new terminal after installing, or add the folder to PATH
Another spring command runsA different tool with the same name is earlier on PATHCheck with which spring and reorder PATH
Unknown dependency IDThe ID does not exist or was renamedCheck spring init --list
The project uses an unexpected Boot versionThe service default changedPin it with -b
Connection errors behind a proxyThe CLI cannot reach start.spring.ioConfigure the JVM proxy settings, or point --target to an internal Initializr
spring run is not recognizedRemoved in Spring Boot 3.0Generate a project and use ./mvnw spring-boot:run

10. Best Practices

  • Pin important options such as --java-version, --build and --package-name in 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 --list instead 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 --target to 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