Spring Boot CLI Tutorial

Spring Boot CLI Tutorial

  • Sharad Jaiswal
  • Spring Boot Tutorials
  • 28th Dec, 2019

What is Spring boot CLI?

The Spring Boot CLI is a command-line interface. If you want to develop a Spring application you can use it. If you have a familiar Java-like syntax without so much boilerplate code it lets you run Groovy scripts. Or we can say It is a spring boot software to run and test spring boot applications from the command prompt. If we run Spring Boot applications by using CLI, then it internally uses Spring boot Auto configure components and Spring boot starter to resolve all dependencies and execute the application.

Now we are going to discuss CLI installation, Setup, and commands in the Windows environment. It's nearly similar to other environments too.

There are many ways to install the Spring Boot CLI -

  • From a downloaded distribution
  • By Using the groovy environment manager
  • BY OS X homebrew
  • using Macports

Features of Spring Boot CLI

Now we look at the different features of Spring Boot CLI:

  • Spring Boot CLI provides an interface to run and test the Spring Boot application from the command prompt.
  • In order to resolve all dependencies and executes the application It internally uses Spring Boot Starter and Spring Boot AutoConfigurate components.
  • It contains Grape Dependency and Groovy Compiler Manager.
  • Supports Groovy Scripts without external Groovy installation
  • It resolves all dependencies and adds Spring Boot defaults automatically.

We need to set up JDK first because Spring is a Java-based framework. The steps needed to set up Spring Boot CLI along with JDK installation are given below.

Setup Java Development Kit (JDK)

First, we can download the latest version of the SDK from Oracle's Java site− Java SE Downloads. Where we will find instructions for installing JDK in downloaded files After that follow the given instructions to install and configure the setup. Now Completely you set PATH and JAVA_HOME environment variables to refer to the directory that contains java and javac typically java_install_dir/bin and java_install_dir respectively.

For running Windows and if you have installed the JDK in C:\jdk1.6.0_15, you would have to put the following line in your C:\autoexec.bat file

set PATH=C:\jdk1.6.0_15\bin;%PATH% 
set JAVA_HOME=C:\jdk1.6.0_15

Alternatively, on Windows NT/2000/XP, first, you will have to right-click on My Computer and select Properties → Advanced → Environment Variables. Then, you will have to update the PATH value then click the OK button.

Unix (Solaris, Linux, etc.), when SDK is installed in /usr/local/jdk1.6.0_15 and you use the C shell then you will have to put the following into your .cshrc file

setenv PATH /usr/local/jdk1.6.0_15/bin:$PATH 
setenv JAVA_HOME /usr/local/jdk1.6.0_15 

MANUALLY INSTALLING THE SPRING BOOT CLI

We can install Spring Boot CLI software by using Zip file or Windows installer both processes are easy to install and both give us the same Spring Boot CLI software.

Step 1- We can download the Spring CLI distribution by using the Spring software repository.

First, you downloaded after that follow the INSTALL.txt instruction from the unpacked archive.

In short: There is a spring script (spring.bat for Windows) in a bin/ directory in the .zip file, or alternatively you can use java -jar with the .jar file (the script helps you to be sure that the classpath is set correctly)

Step 2- We Extract spring-boot-cli-1.4.0.BUILD-SNAPSHOT-bin.zip file into our local FileSystem.

Spring boot folder Structure

Step 3- We Set Spring Boot CLI Environment Variables in Windows System as given below.

set PATH= D:personal datadojspring-1.4.0.BUILD-SNAPSHOTbin

Step 4- Now we Set Spring Boot CLI Environment Variables in Windows System as shown below.

Install Using the CLI

First, we have installed the CLI now we can run it by typing spring. After that, we can use "spring –version" to know the Spring Boot CLI Version as given below.

C:\Users\Sumesh>spring --version
Spring CLI v1.4.0.BUILD-SNAPSHOT

To know version we can use “spring-help” the Spring Boot CLI. As given below.

C:\Users\Sumesh>spring --help
usage: spring [--help] [--version]
        []

Available commands are:

  run [options]  [--] [args]
    Run a spring groovy script

  test [options]  [--] [args]
    Run a spring groovy script test
……..more command help is shown here

Now we done successfully our Spring Boot CLI Installation process.

After Successfully installation we quick start Spring Boot CLI

Spring boot Hello world Example

Follow the following steps to develop the Spring Boot Hello World Example -

Step-1 First we create an "app" folder in our local file system to place our groovy scripts.

Step-2 Now Create a file called app.groovy

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

Compile and run the application type-

$ spring run app.groovy

If you need to use a — to separate them from the "spring" command arguments, then pass command-line arguments to the application, for example

$ spring run app.groovy — –server.port=9000

you can use the JAVA_OPTS environment variable, To set JVM command-line arguments. for example

$ JAVA_OPTS=-Xmx1024m spring run app.groovy

Code observation

We can find the following important points when we observed our app.groovy

  • Find no imports
  • Not find XML configuration to define Spring MVC Components for example views, view resolver, etc.
  • No dispatcher Servlet declaration and no web.xml
  • To create our Application war file not find any build scripts.
  • For deploy this application No need to build the war file

Running the Application

Now we open the command prompt at "app" folder in our local file system.

Execute the following command

spring run app.groovy

Observe the output at the "spring run" command console.

When we observe here, if we execute "spring run app.groovy", it starts Embedded Tomcat server at Default port number: 8080.

Now we can see our Spring Boot HelloWorld Example application is up and running. now It's time to test it.

  • First, we open the browser and access the following link.
  • After then access this URL: http://localhost:8080/

Spring boot hello world example

We can observe the followings points in the above example.

  • We find no Import
  • Not find XML or JAVA configuration
  • There is no any DispatcherServlet and web.xml
  • Not find build file for creating war like Maven or Gradle

Spring Boot responsible for Core Components, Groovy Compiler (groovyc) and Groovy Grape (Groovy’s JAR Dependency Manager).

The Groovy Compiler and Groovy Grape used by the component of Spring Boot to supply some Defaults lime adding compulsory imports and providing required configuration, resolving jar dependencies, adding main() method, etc., We don’t need to worry all these things, if we are Spring Boot Developer, that's the reason the Spring Boot Framework will take care of all those things for us.

That's the qualities of the Spring Boot Framework.

Suggested Read: Spring Boot Interview questions

Default import statements

Many import statements are automatically included to Reduce the size of our Groovy code And without needing to use fully-qualified names or import statements, Notice how the example above refers to @Component,@RestController, and @RequestMapping

Automatic main method

As we have written in Java application, we do not need to include a public static void main(String[] args) method with your Groovy scripts. when your compiled code acting as the source a SpringApplication is automatically created.

Summary

The Spring Boot auto-configuration makes Spring Boot CLI simple and easy and starter dependencies turn it up a notch. CLI makes it possible to develop Spring applications with minimal code noise with the help of groovy language.

This is About Spring Boot CLI. If you have any questions in your mind please Drop a Comment.

About Author :

  • Author of

    Sharad Jaiswal

    My name is Sharad Jaiswal, and I am the founder of Conax web Solutions. My tech stacks are PHP, NodeJS, Angular, React. I love to write technical articles and programming blogs.

Leave A Comment :

Valid name is required.

Valid name is required.

Valid email id is required.

You May Also Like