Spring Boot CLI Commands

The Spring Boot CLI is a command line tool that you can use if you want to quickly develop a Spring application. It lets you run Groovy scripts, which means that you have a familiar Java-like syntax without so much boilerplate code. You can also bootstrap a new project or write your own command for it.

The Spring Boot CLI (Command-Line Interface) can be installed manually by using SDKMAN! (the SDK Manager) or by using Homebrew or MacPorts if you are an OSX user. Check out  “Installing the Spring Boot CLI” official documentation for comprehensive installation instructions.
Once you have installed the CLI, you can run it by typing spring and pressing Enter at the command line. If you run spring without any arguments, a simple help screen is displayed, as follows:
$ spring
usage: spring [--help] [--version]
       <command> [<args>]

Available commands are:

  run [options] <files> [--] [args]
    Run a spring groovy script

  ... more command help is shown here
Let's discuss Spring boot CLI commands for both Groovy based and Java-based spring boot applications.
To see the CLI in detail, we need to consider some simple examples. Below sample code shows you the simplest Groovy web application you can have and that you can run with Spring Boot:

app.groovy

@RestController
class WebApp {
    @RequestMapping("/")
    String greetings() {
        "Spring Boot Rocks"
    }
}
Now, let’s see the same web application but in Java:

WebApp.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class WebApp {
    @RequestMapping("/")
    public String greetings() {
        return "Spring Boot Rocks in Java too!";
    }
    public static void main(String[] args) {
        SpringApplication.run(WebApp.class, args);
    }
}
Spring Boot enables you to choose Java or Groovy in order to create enterprise and production-ready applications with ease. 

Spring Boot CLI Commands

Let’s start using all the CLI commands.

The run Command

The run command will allow you to run Java or Groovy Spring Boot applications. Its syntax is the following:
spring run [options] <files> [--] [args]
  • --autoconfigure [Boolean]: Adds the auto-configuration compiler transformation.
  • --classpath, -cp Adds the classpath entries, and it’s useful when you have third-party libraries.
  • -- no-guess-dependencies Does not attempt to guess the dependencies. This is useful when you already use the @Grab annotation in your application.
  • --no-guess-imports Does not attempt to guess the imports.
  • -q, --quiet Quiets logging. In other words, it won’t print anything to the console.
  • -v, --verbose Logs everything. It is useful for seeing what’s going on because it shows you even the code introspection and what is adding to the program.
  • --watch Sets a watch to the file(s) for changes. It is useful when you don’t want to stop and run the app again.
To run the Groovy application (shown in Listing 4-1 ), you simply execute:
$ spring run app.groovy 
Executing this command, you will have a web application up and running and listening to port 8080 by default, but you can override this by executing the following command:
$ spring run app.groovy -- --server.port=8888 
If you want to run the Java application, you just execute:
$ spring run WebApp.java 

The test Command

The test command runs a Spring Groovy script and Java tests. Its syntax is the following:
spring test [options] files [--] [args] 
The available options are:
  • --autoconfigure [Boolean] Adds the auto-configuration compiler transformation (default is true).
  • --classpath, -cp Adds the classpath entries, which is useful when you have third-party libraries.
  • --no-guess-dependencies Does not attempt to guess the dependencies.
  • --no-guess-imports Does not attempt to guess the imports.
Consider we have below simple spring boot unit test in Java application:

MyTest.java

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
public class MyTest {
    @Rule
    public OutputCapture capture = new OutputCapture();
    @Test
    public void stringTest() throws Exception {
            System.out.println("Spring Boot Test works in Java too!");
            assertThat(capture.toString(), containsString("Spring Boot Test works in Java too!"));
    }
}
To run above unit test, you just execute this command:
$ spring test MyTest.java 

The grab Command

The grab command will download all the Spring Groovy scripts and Java dependencies to the ./repository directory. Its syntax is the following:
spring grab [options] files [--] [args] 
The available options are:
  • --autoconfigure [Boolean] Adds the auto-configuration compiler transformation (default is true).
  • --classpath, -cp Adds the classpath entries, which is useful when you have third-party libraries.
  • --no-guess-dependencies Does not attempt to guess the dependencies.
  • --no-guess-imports Does not attempt to guess the imports.
Let's execute the above unit test using below grab command:
$ spring grab MyTest.java 
If you check out the current directory, you will see the repository subdirectory created with all the dependencies. The grab command is useful when you want to execute a Spring Boot application that doesn’t have an Internet connection and the libraries are needed. The grab command is also used to prepare your application before you can deploy it to the cloud.

The jar Command

The jar command will create a self- contained executable JAR file from a Groovy script or Java. Its syntax is the following:
spring jar [options] <jar-name> <files>
The available options are:
  • --autoconfigure [Boolean] Adds the auto-configuration compiler transformation (default is true).
  • --classpath, -cp Adds the classpath entries, which is useful when you have third-party libraries.
  • --exclude A pattern to find the files and exclude them from the final JAR file.
  • --include A pattern to find the files and include them in the final JAR file.
  • --no-guess-dependencies Does not attempt to guess the dependencies.
  • --no-guess-imports Does not attempt to guess the imports.
Let's use app.groovy sample code and execute the following command:
$ spring jar app.jar app.groovy 
Now you can check out your current directory and see that there are two files—one named app.jar. original and another named app.jar. The only difference between the files is that the app.jar.original is the one created by dependency management (Maven) to create the app.jar. It’s a fat JAR that can be executed with the following:
$ java -jar app.jar 

The war Command

This is very similar to the previous command. The war command will create a self-contained executable WAR file from a Groovy or Java. Its syntax is the following:
spring war [options] <war-name> <files> 
Let's use app.groovy to run the war command by executing the following:
$ spring war app.war app. groovy
After executing this command, you will have in your current directory the app.war.original and the app.war files. You can run it with the following command:
$ java -jar app.war 

The install Command

The install command is very similar to the grab command; the only difference is that you need to specify the library you want to install (in a coordinate format groupId:artifactId:version ; the same as the @Grab annotation). It will download it and the dependencies in a lib directory. Its syntax is the following:
spring install [options] <coordinates>
For example:
 spring install org.spockframework:spock-core:1.0-groovy-2.4
You will have in the lib directory the Spock library and its dependencies.

The uninstall Command

The uninstall command will uninstall the dependencies from the lib directory. Its syntax is the following:
spring uninstall [options] <coordinates> 
You can test this command by executing the following command:
$ spring uninstall org.spockframework:spock-core:1.0-groovy-2.4
It will remove all the Spock dependencies from the lib directory.

The init Command

The init command will help you initialize a new project by using the Spring Initializr ( http://start.spring.io/ ). Whether or not you are using an IDE, this command will help you get everything ready to start developing Spring Boot applications. Its syntax is the following:
spring init [options] [location] 
To create a default project, you just execute:
$ spring init
It will generate a demo.zip file. You can unzip it (a Maven project structure) and import in your IDE.

The shell Command

One of the most helpful features is Spring Shell, which wraps commands with the necessary spring prefix.
To start the embedded shell, we run:
spring shell
From here, we can directly enter desired commands without pre-pending the spring keyword (since we’re now in spring shell).

The help Command

The help command will be your best friend. You can execute it as follows:
spring help 

Summary

This article showed you how to use the Spring Boot Command Line Interface. It explained all the different commands and their options with sample examples.

Reference

Comments