Backend Development 12 min read

Spring Boot CLI Tutorial: Installation, Project Structure, Hello World, @Grab, Testing, Packaging, and New Project Creation

This tutorial explains how to install Spring Boot CLI, demonstrates the project layout, creates a Hello World application using Groovy, shows how to use @Grab for dependencies, run tests, package the app into an executable JAR, and generate new projects with the init command.

IT Xianyu
IT Xianyu
IT Xianyu
Spring Boot CLI Tutorial: Installation, Project Structure, Hello World, @Grab, Testing, Packaging, and New Project Creation

Introduction

Spring Boot CLI is a command‑line interface for running Spring Boot commands, allowing developers to create, run, and test Spring Boot applications with minimal code using Groovy.

Key Features of Spring Boot CLI

Runs Groovy scripts without needing explicit Groovy JAR dependencies; Spring Boot auto‑configures them.

Eliminates the need for import statements until required.

No build tool is necessary; dependencies are fetched automatically based on annotations and classes used.

Uses @Controller , @Grab , and other annotations to download appropriate JARs.

Provides commands such as spring run , spring test , spring jar , and spring init .

Installation of Spring Boot CLI

Multiple installation methods are available (manual, SDKMAN, Homebrew, etc.). The example uses manual installation on Windows:

Download the ZIP from http://repo.spring.io/release/org/springframework/boot/spring-boot-cli/1.4.3.RELEASE/spring-boot-cli-1.4.3.RELEASE-bin.zip .

Extract it, e.g., to C:\spring-1.4.3.RELEASE .

Set environment variables: SPRING_HOME=C:\spring-1.4.3.RELEASE PATH=%PATH%;C:\spring-1.4.3.RELEASE\bin

Verify installation by running spring in a command prompt.

Demo Project Structure

spring-app
│
├─message.groovy
├─hello.groovy
├─tests.groovy
├─templates
│   └─hello.html
└─static
    └─index.html

Hello World Example

Create hello.groovy :

@RestController
class HelloController {
    @RequestMapping("/home")
    String home() {
        "Hello World!"
    }
}

Run it with:

spring run hello.groovy

The CLI automatically downloads Spring MVC and embedded Tomcat JARs, compiles the code, and starts the server on port 8080. Access the endpoint at http://localhost:8080/home . The port can be changed using --server.port=8484 .

Using @Grab Annotation

@Grab resolves third‑party JAR dependencies. Example:

@Grab('spring-boot-starter-thymeleaf')
class MessageController {}

Groovy source message.groovy and accompanying HTML templates are provided, and the application can be started with spring run *.groovy .

Testing the Application

Create tests.groovy :

class ApplicationTests {
    @Test
    void HelloAppTest() {
        assertEquals("Hello World!", new HelloController().home())
    }
}

Run tests using:

spring test hello.groovy tests.groovy

Packaging the Application

Package into an executable JAR with:

spring jar spring-app.jar *.groovy

Run the JAR via java -jar spring-app.jar . The CLI includes directories such as public/**, resources/**, static/**, templates/**, META-INF/** and excludes typical build folders.

Creating a New Project

Use the init command to generate a Maven project from https://start.spring.io :

spring init --dependencies=web,thymeleaf my-app.zip

For a Gradle WAR project with Java 1.8:

spring init --build=gradle --java-version=1.8 --dependencies=web,thymeleaf --packaging=war my-app.zip

Embedded Shell

On Windows, start the integrated shell with:

spring shell

Within the shell, commands can be run without the spring prefix, e.g., run hello.groovy or test hello.groovy tests.groovy .

Source Code Download

The complete example can be downloaded from https://www.concretepage.com/spring-boot/download/spring-boot-cli-example.zip .

JavaCLIBackend DevelopmentSpring BootGroovy
IT Xianyu
Written by

IT Xianyu

We share common IT technologies (Java, Web, SQL, etc.) and practical applications of emerging software development techniques. New articles are posted daily. Follow IT Xianyu to stay ahead in tech. The IT Xianyu series is being regularly updated.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.