Search This Blog

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:

--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:

--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.