Master Spring Boot CLI: Install, Run, Test, and Package Groovy Apps

This guide walks you through installing Spring Boot CLI, explains its Groovy‑based features, demonstrates creating and running a Hello World web app, shows how to use @Grab for dependencies, test with spring test, package into executable JARs, and explore init and shell commands.

Java Web Project
Java Web Project
Java Web Project
Master Spring Boot CLI: Install, Run, Test, and Package Groovy Apps

Overview of Spring Boot CLI

Spring Boot CLI is a command‑line tool that compiles and runs Groovy scripts as Spring Boot applications. It automatically resolves Maven‑style dependencies based on the annotations used in the script, so no explicit build tool or main() method is required.

Key Technical Behaviors

Groovy support – .groovy files are compiled with Spring‑provided Groovy JARs; no manual Groovy dependency is needed.

Implicit imports – only missing classes require an explicit import statement.

Annotation‑driven dependency resolution – the presence of annotations such as @RestController triggers download of Spring MVC, embedded Tomcat, etc.

Third‑party JARs via @Grab – Groovy’s @Grab fetches external libraries (e.g., Thymeleaf) using Spring Boot’s default metadata for group ID and version.

Convenient commands – spring run, spring test, spring jar, spring init, and spring shell for interactive use.

Installation (Windows example)

Download the CLI 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 to a directory, e.g. 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

Open a command prompt and run spring to verify the installation.

Demo Project Layout

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

Hello World Web Application

File hello.groovy:

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

Run with:

spring run hello.groovy

Execution flow:

CLI downloads required JARs; versions are derived from the CLI version. @RestController causes Spring MVC and embedded Tomcat JARs to be fetched.

Groovy code is compiled and Tomcat starts on port 8080.

Access the endpoint at http://localhost:8080/home.

Changing the Server Port

Pass a Spring property after a double‑dash separator:

spring run hello.groovy -- --server.port=8484

The application then listens on http://localhost:8484/home.

Using @Grab for Third‑Party Dependencies

Example pulling in Thymeleaf:

@Grab('spring-boot-starter-thymeleaf')
class MessageController {
    @RequestMapping("/msg")
    String getMsg(Model model) {
        String msg = "Welcome to Everyone!"
        model.addAttribute("message", msg)
        return "hello"
    }
}

The @Grab annotation resolves the artifact ID; group ID and version are taken from Spring Boot’s default dependency metadata.

Testing the Application

File tests.groovy:

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

Run tests with:

spring test hello.groovy tests.groovy

Packaging as an Executable JAR

Package all Groovy files:

spring jar spring-app.jar *.groovy

Two files are produced: spring-app.jar – runnable JAR. spring-app.jar.original – original JAR before repackaging.

Run with:

java -jar spring-app.jar

Generating a New Project with spring init

Generate a Maven project containing Web and Thymeleaf starters:

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

The resulting pom.xml includes spring-boot-starter-web and spring-boot-starter-thymeleaf. The zip is saved in the current directory.

Embedded Shell (Windows)

Start the interactive shell:

spring shell

Inside the shell the spring prefix is optional:

$ version
$ test hello.groovy tests.groovy
$ run hello.groovy

Annotation‑Based Auto‑Grab Details

Spring Boot deduces which starter JARs to download based on the following annotations: @Controller, @RestController, @EnableWebMvc → Spring MVC + embedded Tomcat. @EnableWebSecurity → Spring Security. @EnableJms → JMS support. @Test → Spring Test starter.

These mappings are documented in the CLI reference under “Deduced ‘grab’ Dependencies”.

References (plain URLs)

Spring Boot installation guide: http://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-installing-spring-boot.html

CLI usage documentation: https://docs.spring.io/spring-boot/docs/current/reference/html/cli-using-the-cli.html

Concrete example source: https://www.concretepage.com/spring-boot/spring-boot-cli-example

Example zip download: https://www.concretepage.com/spring-boot/download/spring-boot-cli-example.zip

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavaCLISpring BootCommand LineTutorialGroovy
Java Web Project
Written by

Java Web Project

Focused on Java backend technologies, trending internet tech, and the latest industry developments. The platform serves over 200,000 Java developers, inviting you to learn and exchange ideas together. Check the menu for Java learning resources.

0 followers
Reader feedback

How this landed with the community

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.