Using Spring boot CLI to bootstrap your projects

In this tutorial we have discovered how to boostrap your Spring Boot applications without leaving the shell.

A pre-requisite is to download the latest version of spring cli which is available at:

Unzip the file and put the “bin” folder in your system PATH. Let’s try it!

$ spring init Using service at Content saved to '' 

As you can see, you can actually use spring init without any parameter. It will create a skeleton project with minimal dependencies and save it into the file.

Specifying dependencies

You can specify the additional dependencies on the command line using either –dependencies or -d :

$ spring init -dweb,jpa 

This will create a containing a minimal project structure with Spring Boot’s web and JPA starters. Please note that you mustn’t include a space between -d and the dependencies otherwise the dependencies will not be included but they will be part of the project name rather.

Out of the box, the init command will create an executable JAR file. If you want to create a WAR file instead, you can specify so with the –packaging or -p parameter:

$ spring init -dweb,jpa -p war 

You can also specify the build type, which by default is Maven. if you want to use Gradle instead:

$ spring init -dweb,jpa --build gradle -p war 

The init command features several other parameters, including parameters for building a Groovy-based project, specifying the Java version to compile with, and selecting a version of Spring Boot to build against.

Need help?

If you don’t remember the dependency names or the parameter names to pass just use the –list flag, as shown in the following example:

$ spring init --list ======================================= Capabilities of =======================================  Available dependencies:

actuator - Actuator: Production ready features to help you monitor and manage your application  . . . . web - Web: Support for full-stack web development, including Tomcat and spring-webmvc websocket - Websocket: Support for WebSocket development ws - WS: Support for Spring Web Services  Project types (* denotes the default) +-----------------+-----------------------------------------+-----------------------------+ | Id              | Description                             | Tags                        | +-----------------+-----------------------------------------+-----------------------------+ | gradle-build    | Generate a Gradle build file            | build:gradle,format:build   | | gradle-project  | Generate a Gradle based project archive | build:gradle,format:project | | maven-build     | Generate a Maven pom.xml                | build:maven,format:build    | | maven-project * | Generate a Maven based project archive  | build:maven,format:project  | +-----------------+-----------------------------------------+-----------------------------+   Parameters +-------------+------------------------------------------+------------------------------+ | Id          | Description                              | Default value                | +-------------+------------------------------------------+------------------------------+ | artifactId  | project coordinates (infer archive name) | demo                         | | bootVersion | spring boot version                      | 2.1.1.RELEASE                | | description | project description                      | Demo project for Spring Boot | | groupId     | project coordinates                      | com.example                  | | javaVersion | language level                           | 1.8                          | | language    | programming language                     | java                         | | name        | project name (infer application name)    | demo                         | | packageName | root package                             | com.example.demo             | | packaging   | project packaging                        | jar                          | | type        | project type                             | maven-project                | | version     | project version                          | 0.0.1-SNAPSHOT               | +-------------+------------------------------------------+------------------------------+ 

Grabbing just the pom.xml

There will be times when you only need the pom.xml (or build.gradle) file, maybe because you want to check out the Spring Boot dependencies or to look at the plugins declarations. In order to do that, it is sufficient to execute the following command:

$ curl -s -d packaging=war -o pom.xml 

This command will generate only the pom.xml file, for a standard Web application.