How to Use Spring Boot CLI – Command Line Interface

How to Use Spring Boot CLI – Command Line Interface

0 Comments

Last Updated on October 21, 2024 by jt

The Spring Boot CLI (Command Line Interface) is a command-line tool that you can use to run and test Spring Boot Applications from a Terminal. The CLI is one of the fastest ways to develop a Spring-based application.

How does Spring Boot CLI work?

It uses Spring Boot Starter and Spring Boot AutoConfigure components to internally resolve all dependencies and execute the application.

Dependencies get resolved automatically due to the Groovy and Grape dependency manager present in the CLI.

Groovy is an object-oriented dynamic programming language and Grape is a JAR dependency manager that is embedded into Groovy. Therefore, you can run groovy scripts without so much boilerplate code.

In this post, you will learn how to configure Command Line Interface for Spring and execute simple terminal commands.

CLI Setup on Windows

For Windows Operating System, you can follow these steps to complete its installation.

Download the latest version of CLI API as ZIP archive from Spring software repository.

Unzip the zip distribution into a convenient location as done below.

Set SPRING_HOME pointing to the installation directory as shown in the below figure

Set your PATH variable pointing to the bin directory of the installation, as set in the below figure.

Once done, check the version using the command spring --version.

Hence, you can see the Spring version as 2.5.2

CLI Setup with SDKMAN

SDKMAN (The Software Development Kit Manager) can be used for managing multiple versions of various binary SDKs, including Groovy and the Spring Boot CLI. Get SDKMAN from sdkman.io and install Spring Boot by using the following commands:

sdk install springboot
spring --version

CLI Setup on Linux/Mac

If you are on a Mac and using Homebrew, all you need to do to install the Spring Boot CLI is:

brew tap pivotal/tap
brew install springboot

CLI Setup with GVM

GVM (the Groovy Environment Manager) can be used for managing multiple versions of various Groovy and Java binary packages, including Groovy itself and the Spring Boot CLI. Get gvm from gvmtool.net and install Spring Boot with:

gvm install springboot
spring --version

Spring Boot CLI Example

In this example, we will develop a simple Spring Boot MVC RestController.

Since CLI runs groovy scripts, therefore, let us write one.

This is the code for DemoCLI.groovy class.

DemoCLI.groovy

@RestController
class DemoCLI {
    @RequestMapping("/get-message")
    String greetHello() {
        "Hi ! This is a message from groovy script"
    }
}

It is a simple REST Controller annotated class with a request handler method.

Run and Test the Script

CLI provides a spring command to run Spring Boot Groovy scripts from Command Prompt.

Open the command prompt and type spring run DemoCLI.groovy

This is how the console window looks:

On executing the spring run command, the embedded Tomcat server starts at the default port number: 8080.

You can open the browser to access the URL http://localhost:8080/get-message

This is the output.

We are able to access our first Spring Boot MVC RESTful WebService.

Summary

Spring Boot CLI avoids lots of boilerplate code and Spring Configuration, as it automatically detects which dependency JARs are to be downloaded based on the classes and annotations used in code, thereby reducing development time.

You can find the source code of this post here on Github.

About SFG Contributor

Staff writer account for Spring Framework Guru

    You May Also Like

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.