spring boot cli

Create a Spring Boot Application using Spring Boot CLI

Spread the love

In this article you will learn how to use Spring Boot CLI to be able to create or run Spring Boot applications from your command line. Let’s see how!

Getting started

1. Installation

There are two main ways to install Spring Boot CLI easily.

1.1 Installation Using SDKMan

If you are currently using a unix-based system, you can easily install Spring Boot CLI using SDKMan. If you haven’t installed SDKMan yet, you can check how to do it in our article “How to Use SDKMan”.

Let’s list the available versions of Spring Boot CLI first:

$ sdk list springboot

================================================================================
Available Springboot Versions
================================================================================
     2.7.5               2.4.5               2.1.6.RELEASE       1.4.4.RELEASE
     2.7.3               2.4.4               2.1.5.RELEASE       1.4.3.RELEASE
     2.7.2               2.4.3               2.1.4.RELEASE       1.4.2.RELEASE
     2.7.1               2.4.2               2.1.3.RELEASE       1.4.1.RELEASE
...

We are going to install the latest version, 2.7.5 at the time of writing.

$ sdk install springboot 2.7.5

If we do sdk list springboot again, we can see how version 2.7.5 gets marked as installed and currently in use.

$ sdk list springboot

================================================================================
Available Springboot Versions
================================================================================
 > * 2.7.5               2.4.5               2.1.6.RELEASE       1.4.4.RELEASE
     2.7.3               2.4.4               2.1.5.RELEASE       1.4.3.RELEASE
     2.7.2               2.4.3               2.1.4.RELEASE       1.4.2.RELEASE
     2.7.1               2.4.2               2.1.3.RELEASE       1.4.1.RELEASE
.....
     2.4.6               2.1.7.RELEASE       1.4.5.RELEASE

================================================================================
+ - local version
* - installed
> - currently in use
================================================================================

We’ve appended the legend that normally gets printed at the bottom of the list command output to be able to understand the symbols in the list. As you can see, 2.7.5 version is now installed and currently in use according to the legend.

If you’re a Mac user, you can also use homebrew to install Spring Boot Cli.

1.2 Installation Using Homebrew

To install Spring Boot CLI in your Mac you just have to run the following.

brew tap spring-io/tap
brew install spring-boot

As simple as that!

2. Checking installation version

Once installed, the easiest way to check if Spring Boot CLI was properly installed is by running this:

$ spring --version  
                                                                                                                                                           
Spring CLI v2.7.5

If you see something similar to that, you have successfully installed Spring Boot CLI on your machine!

Now that we have installed Spring Boot CLI, let’s see how to use it.

3. Usage

If it’s the first time you use Spring Boot CLI, probably the best way to start is to check what options are available. You can achieve that by running this:

$ spring help

This is equivalent to run just spring, as by default it will show you what options are available when none are specified.

3.1 Initialise a new Spring Boot project

The CLI allows us to initialise a Spring Boot project from scratch, this is achieved by running spring init command. Let’s see how.

In order to see what options are available we can ask the CLI to help us with this command:

$ spring help init

spring init - Initialize a new project using Spring Initializr (start.spring.io)

usage: spring init [options] [location]

Option                       Description
------                       -----------
-a, --artifact-id <String>   Project coordinates; infer archive name (for
                               example 'test')
-b, --boot-version <String>  Spring Boot version (for example '1.2.0.RELEASE')
--build <String>             Build system to use (for example 'maven' or
                               'gradle') (default: maven)
-d, --dependencies <String>  Comma-separated list of dependency identifiers to
                               include in the generated project
--description <String>       Project description
-f, --force                  Force overwrite of existing files
--format <String>            Format of the generated content (for example
                               'build' for a build file, 'project' for a
                               project archive) (default: project)
-g, --group-id <String>      Project coordinates (for example 'org.test')
-j, --java-version <String>  Language level (for example '1.8')
-l, --language <String>      Programming language  (for example 'java')
--list                       List the capabilities of the service. Use it to
                               discover the dependencies and the types that are
                               available
-n, --name <String>          Project name; infer application name
-p, --packaging <String>     Project packaging (for example 'jar')
--package-name <String>      Package name
-t, --type <String>          Project type. Not normally needed if you use --
                               build and/or --format. Check the capabilities of
                               the service (--list) for more details
--target <String>            URL of the service to use (default: https://start.
                               spring.io)
-v, --version <String>       Project version (for example '0.0.1-SNAPSHOT')
-x, --extract                Extract the project archive. Inferred if a
                               location is specified without an extension

examples:

    To list all the capabilities of the service:
        $ spring init --list

    To creates a default project:
        $ spring init

As you can see, there are many options that can be specified. However, you can leave most of them empty and CLI will use the predefined default values, which you could change later.

Let’s create a new Spring Boot application and go through the process together. We are going to create a Spring Boot application built with Gradle, using Kotlin as the language and we’ll choose JDK 17. The resulting command looks like this:

spring init --build gradle --description "Our first Spring Boot application" --group-id com.theboreddev --java-version 17 --language kotlin --name spring-boot-app --version 0.0.1-SNAPSHOT

This command is actually using the same service provided at start.spring.io. When you execute the command, it will download a zip file called demo.zip.

Make sure you create a directory for our new application under your workspace, move the demo.zip file there and then unzip its content.

$ mkdir spring-boot-app
$ mv demo.zip spring-boot-app
$ cd spring-boot-app
$ unzip demo.zip
$ rm demo.zip

Once you run these commands, you will be able to see that our new project has been created.

A few things you will be able to notice is that our application comes with a predefined SpringBootAppApplication class to start our app. Its content is quite simple:

package com.theboreddev.demo

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
class SpringBootAppApplication

fun main(args: Array<String>) {
	runApplication<SpringBootAppApplication>(*args)
}

This is enough to start our new application using Spring Boot.

Another thing to look at is that it automatically creates an application.properties file for us, we will be placing our configuration properties in this file. We won’t be showing application specific configuration in this article though, as this article should be limiting to show how Spring Boot CLI works. You can check our article “How to Bootstrap a Spring Boot Application” if you are interested in knowing a bit more about Spring Boot.

There are other command available in Spring Boot CLI, although these are mainly available for Groovy scripts. We’ll skip those in this article. If you’re still interested you can find a guide here.

If you are interested in learning Spring in depth, we highly recommend the following books:

Conclusion

In this article we have learned how to install Spring Boot CLI and make use of it to easily create new Spring Boot applications from our command line.

This is all from us today! Please follow us if you like our articles.

Thanks for reading us! See you soon!

Leave a Reply