You can create and initialize a new Spring Boot project by using Spring Boot CLI or Spring Initializr

Spring Boot CLI is a command line tool for quickly bootstrapping a new Spring Boot project or running Groovy scripts. By default, Spring Boot CLI uses Spring Initialzr service to generate the template

Spring Initializr provides API to generate JVM-based projects. It can be used directly via web interface at https://start.spring.io/ or via other tools such as cURL, Spring Boot CLI

Lets walk through this tutorial to explore them in more details

Install Spring Boot CLI

Spring Boot CLI requires Java 8+ and Maven 3.3+

You can install Spring Boot CLI manually or via package managers such as Homebrew and MacPorts on Mac, SDKMAN! on UNIX based systems, Scoop on Windows

On Mac, Homebrew installs the tool into /usr/local/bin

$ brew tap pivotal/tap
$ brew install springboot

Install guide for other tools can be found at here

Create a new Spring Boot project with default template

On your terminal, type the following command to create a new Spring Boot project with the default template

$ spring init my-spring-boot

The CLI will create a new directory named my-spring-boot if not existing, and generate the following project structure inside it

├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── example
│   │   │           └── demo
│   │   │               └── DemoApplication.java
│   │   └── resources
│   │       └── application.properties
│   └── test
│       └── java
│           └── com
│               └── example
│                   └── demo
│                       └── DemoApplicationTests.java
├── .gitignore
├── HELP.md
├── mvnw
├── mvnw.cmd
└── pom.xml

You can now open the new project in your favourite editors, edit the artifactId, groupId and project version, add more dependencies into pom.xml, modify the package and application name and start working on it

If you'd like to create a new project with the above predefined settings, learn to do it by providing more options to the command

The spring init command

Spring Boot CLI provides $ spring init command to initialize a new project using Spring Initalizr

The syntax of spring init command as below

$ spring init [options] [location]

location can be either an absolute or relative directory path, default is the current directory.

The directory will be auto created if not existing. The command will exit if any duplicate files, use --force option if you'd like to overwrite

options can be the combination of the following

  • -d: list of dependencies in comma-separated format to include in the project, use --list to discover

  • -b: Spring Boot version, default is the latest stable release

    -j: Java version

  • -g: groupId, default is com.example

    -a: artifactId, default is the value of location param, hyphens - is used as a separator in case of absolute path

    -v: project version, default is 0.0.1-SNAPSHOT

    --description: project description, default is Demo project for Spring Boot

  • --package-name: package name, -n: project name; infer application name

The default values will be used if location or options is omitted

Beside spring init, Spring Boot CLI also offers other commands for working with Groovy scripts, you can explore more by typing $ spring --help

Create a new Spring Boot project with predefined settings

On your terminal, type the following command to create a new Spring Boot project at directory another-spring-boot with spring-boot-starter-web and spring-boot-starter-actuator as dependencies, com.hellokoding.tutorials as package name, HelloApplication as app name

$ spring init -d=web,actuator -n=hello \
    --package-name=com.hellokoding.tutorials \
    another-spring-boot

The CLI will create a new directory named another-spring-boot if not existing, and generate the following project structure inside it

├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── hellokoding
│   │   │           └── tutorials
│   │   │               └── HelloApplication.java
│   │   └── resources
│   │       ├── static
│   │       ├── templates
│   │       └── application.properties
│   └── test
│       └── java
│           └── com
│               └── hellokoding
│                   └── tutorials
│                       └── HelloApplicationTests.java
├── .gitignore
├── HELP.md
├── mvnw
├── mvnw.cmd
└── pom.xml

pom.xml

<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>another-spring-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hello</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>  

Conclusion

In this tutorial, we learned some ways to create and initialize a new Spring Boot project by using Spring Boot CLI. You can find more about it at here