Home  >  Spring Boot

Spring Boot CLI Example

By Arvind Rai, January 19, 2017
On this page we will provide spring boot CLI example. Spring boot CLI is Command Line Interface to run spring boot command. Spring provides spring command that is used to create, run and test the spring boot application. Spring boot CLI application uses groovy and in this way with a minimum number of lines of code we create our application and launch it. Spring boot CLI with groovy has following features.

1. Spring boot CLI application uses groovy. We create groovy files in our application and for this we need not to provide any JAR dependency of groovy. Spring boot configures the groovy JAR dependencies itself.
2. While writing groovy files, we need not to write import statements until required. Before writing import statement we should check if we are getting import error. In this way we write minimum number of lines of code as much as possible.
3. Spring boot CLI does not require any build tools. Spring boot automatically grabs the spring dependencies JAR in the same way as Maven and Gradle do. The version is picked up from spring boot CLI version.
4. Spring boot downloads JAR dependencies keeping in mind that what spring annotation and classes we have used in our application coding. If we are using @Controller annotation then spring boot downloads spring web application JAR dependencies.
5. To download JAR for third party dependencies such as thymeleaf, spring boot uses @Grab annotation groovy feature.
6. Spring boot provides spring run command to run application and spring test command to run spring test application.
7. We can create executable JAR file using spring boot command line. For packaging of the application, spring boot provides spring jar command.
8. Using command line interface we can create project from spring initializer site using spring init command.
9. Spring boot provides integrated shell for Windows user using the command spring shell .
10. We need not to create spring boot main() method when using spring boot CLI. It is provided automatically by spring boot CLI.

Here on this page we will install spring boot CLI and then we will create a sample spring boot CLI application and run it and test it.

Install Spring Boot CLI

To work with Spring boot CLI, first we need to install it in our system. There are many ways to install Spring boot CLI.
1. Manual installation
2. Installation with SDKMAN!
3. OSX Homebrew installation
4. MacPorts installation
5. Command-line completion

Find the link for Spring boot CLI installation detail. In my example I will install Spring boot CLI using manual installation in my Windows 7 OS. Find the below steps.
Step 1: Download Spring boot CLI using below link

Step 2: Unzip it and keep somewhere in your system. Suppose I have kept it as follows.
Now we need to set following environment variables in our system.
1. SPRING_HOME with the value C:\spring-1.4.3.RELEASE
2. PATH with the value C:\spring-1.4.3.RELEASE\bin
Step 3: Now we will test our installation. Open the command prompt and type the command spring, we will get following result.
Spring Boot CLI Example
Find more Spring boot CLI commands.
spring help run : Gives details of supported commands.
spring version : Gives version of Spring boot CLI.
Now we are done to start working with Spring boot CLI.

Demo Project Structure

Find the demo project structure which we will run using spring boot CLI and groovy.

Spring Boot CLI Hello World Example

We will create a simple "Hello World" web application using Groovy. Find the code below.
class HelloController {
    String home() {
        "Hello World!"
When we use Spring boot CLI with Groovy, we are able to run our application with minimum code. In our code we will get following observation.
1. Default import statements. In most of the cases we need not to import API. Import only if that is not part of default import statements.
2. No main method for spring boot. It will be automatically created.

To compile and run Groovy source code, Spring boot CLI provides run command that we call as follows.
spring run hello.groovy 
Using the command we achieve following points.
1. It downloads all the dependency JAR. The version of dependency JAR is defined by the version of Spring boot CLI we are using. Downloading the JAR take place only for the first time.
2. The dependency JAR are defined by the classes and annotations used in the code. As we are using @RestController, so it downloads JAR related to Spring MVC and embedded Tomcat.
3. Now it compiles the code and start embedded tomcat server on the default port 8080 .

Find the print screen.
Spring Boot CLI Example
Use the URL http://localhost:8080/home to run the application. Find the print screen.
Spring Boot CLI Example
If we want to change the default port, then we need to run the run command with server.port as follows.
spring run hello.groovy -- --server.port=8484 
We need to take care that we will use a separator -- with spring command argument --server.port. Now the embedded tomcat will start with the port 8484 and to run the application we need to use http://localhost:8484/home URL.
If we have multiple groovy files and to run all those groovy files together we can use following command.
spring run *.groovy 

Using @Grab Annotation

Groovy provides @Grab annotation to resolve JAR dependencies. Spring boot also supports @Grab annotation to resolve only third party dependencies. Spring dependencies are grabbed automatically on the basis of spring annotation and classes used in our application. It downloads the JAR in the same way as Maven and Gradle without using any build tool. We use @Grab annotation to download third party dependencies as follows.
class MessageController {} 
In the above code, we will observe that we need to pass only artifact id. Group id and version will be taken from default dependency metadata of spring boot. Now find the groovy files and HTML used in the example.
class MessageController {
	String getMsg(Model model) {
    	String msg = "Welcome to Everyone!";
        model.addAttribute("message", msg);
        return "hello";
<html xmlns:th="http://www.thymeleaf.org">
    <title>Spring Boot CLI Example</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <p th:text="'Message: ' + ${message}" />
    <title>Spring Boot CLI Example</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <p>Click to get <a href="/msg">Message</a> </p>
To run the example, run the following command from the root directory of the project using command prompt.
spring run *.groovy 
Now access the URL http://localhost:8080/

Spring boot needs @Grab annotation only to resolve third party JAR, for example spring-boot-starter-thymeleaf, freemarker etc. Spring boot automatically grabs the spring JAR as required. For example if we are using following annotations and classes then the related JAR dependencies will automatically be downloaded.
1. @Controller @RestController @EnableWebMvc : In this case Spring MVC and embedded tomcat will be downloaded.
2. @EnableWebSecurity : Spring security related JAR will be downloaded.
3. @EnableJms : JMS application related JAR will be downloaded.
4. @Test : Spring Test application related JAR will be downloaded.

We can find detail in "Deduced grab dependencies section" in the link.

Spring Boot CLI Test Application

Spring boot CLI provides test command using which we can test our spring boot application. Find the groovy class used in our example to test hello.groovy.
class ApplicationTests {
    void HelloAppTest() {
        assertEquals("Hello World!", new HelloController().home())
To run the test application use following command.
spring test hello.groovy tests.groovy 
Explanation of above command is follows.
spring test : Spring boot CLI command to test the application.
hello.groovy: Application file that needs to be tested.
tests.groovy: Test file to test the application file.

After running the command we get the result as follows.
Spring Boot CLI Example

Packaging Application using Spring Boot CLI

Spring boot CLI provides jar command to package our application. We can use it as follows.
spring jar spring-app.jar *.groovy 
This will create two JAR.
spring-app.jar : This is executable JAR
spring-app.jar.original : This is original JAR.

Executable jar is run as follows.
java -jar spring-app.jar 
Find the print screen.
Spring Boot CLI Example
While packaging the application spring boot includes following directories by default.
public/**, resources/**, static/**, templates/**, META-INF/**
And default exclude directories are
repository/**, build/**, target/**, **/*.jar, **/*.groovy 
Using --include we can add directory in packaging from default exclude directories. Using --exclude, we can remove directories in packaging from default include directories. For more detail we can run the help command as follows.
spring help jar 

Create a New Project using Spring Boot CLI

Using init command, Spring boot CLI can create a new project with maven as default build tool that uses service of https://start.spring.io . Suppose we want to create a web project using thymeleaf then we will run command as follows.
spring init --dependencies=web,thymeleaf my-app.zip 
The dependencies web,thymeleaf will configure following spring boot starter in pom.xml.
my-app.zip file will be downloaded in in the directory from where we are running command. Find the print screen.
Spring Boot CLI Example
In case we want to use Gradle build tool that will build a WAR file with any specific java version then we can run the command as follows.
spring init --build=gradle --java-version=1.8 --dependencies=web,thymeleaf --packaging=war my-app.zip 

Using Embedded Shell

Spring boot has command line completion scripts for BASH and Zsh shells. If we are using WINDOWS, spring boot provides shell command to launch an integrated shell. If using WINDOWS then launch integrated shell using the following command.
spring shell 
Now we can directly run command without using spring keyword such as
$ version
$ test hello.groovy tests.groovy
$ run hello.groovy 
Find the print screen.
Spring Boot CLI Example

I am done now. Happy Spring boot learning!


Installing Spring Boot
Using the CLI

Download Source Code

comments powered by Disqus

Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us