Spring Boot CLI

Learn via video courses
Topics Covered

Overview

Many developers prefer to work with the command line. Spring boot provides a command line interface tool to test spring applications as scripts.

Introduction to Spring Boot CLI

The Spring Boot CLI is a Command Line Interface for Spring Boot to get started with spring boot quickly. Spring boot code can run as groovy scripts. Spring Boot CLI is the fastest way to create and test a Spring-based application as scripts.

Spring Boot CLI Features

  • Interface to run and test spring boot application.
  • Spring boot CLI, internally uses starter and auto-configuration to resolve the dependencies.
  • Supports groovy script without groovy installation.

Spring Boot CLI Environment Setup

  1. Setup JDK - Download the JDK version of your choice from the oracle website. Install it based on the operating system. Set JAVA_HOME and path env variable for your account.
  1. Download CLI You can download the Spring CLI distribution zip or tar.gz from the Spring software repository Extract the archive to the folder of your choice.

extracting-archive-to-folder

  1. Set the environment No specific environment variables are required to run the CLI. However, you may want to set SPRING_HOME to point to a specific installation. Add SPRING_HOME/bin to your PATH environment variable.

set-environment

  1. Verify installation To test if you have successfully installed the CLI, you can run the command spring --version. It should give you the output below:

Common Terminal Commands used in Spring Boot CLI

Some of the commonly used commands used with spring boot CLI are:

The Run Command

The run command will allow you to run the groovy script. Its syntax is the following:

The Jar Command

The jar command creates a self-contained executable jar file from a Spring Groovy script. Its syntax is the following:

The Init Command

The init command initializes a new project using Spring Initializr (start.spring.io). Its syntax is the following:

Spring Boot CLI Hello World Example

Let's build a simple hello world application using spring boot CLI. The application exposes one single endpoint, /hello.

  1. Create file helloworld.groovy anywhere in your system.
  2. Add the below code to the created file.

.groovy extension is a must.

Code observation

  • No imports.
  • No build tool.
  • No XML of java configuration.
  1. Run and test Go to the command prompt and navigate to the directory where the groovy file is placed, and run the command:

Command runs the script with an embedded tomcat server on port 8080.

The output of the command:

Invoke the endpoint /hello should return the required response.

invoking-the-endpoints

Conclusions

  • Spring boot offers the spring boot CLI tool.
  • Spring boot CLI is useful in quickly running and testing spring boot applications as a groovy script.
  • Spring boot CLI needs a file with a .groovy extension to run it.