Besides using your IDE, you can create and initialize a new Spring Boot project by using Spring Initializr. It is a web service that provides API to generate a new Spring Boot project. It can be used directly via the web interface at https://start.spring.io/ or via command-line tools such as cURL, HTTPie, and Spring Boot CLI

Using Spring Initializr via web UI

Go to the Spring Initializr web tool at https://start.spring.io/. The UI interface is pretty straight forward

Using Spring Initializr

By default, Maven, Java, and the latest Spring Boot version is selected. You can change the Java version, update project metadata such as group, artifact, name, description, package name, and packaging type (jar or war)

You can add initial dependencies to your project, of course. They are grouped into categories and can be filtered

You can click on the Explore button to see how the project structure and generated files content look like

In the final step, click on Generate, a demo.zip will be generated and downloaded into your browser. Unzip and open that file you will have the following project structure

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

The below gives you a sample of the generated pom.xml file content

<?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.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.hellokoding.tutorials</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</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-webflux</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>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>  

You can run the project right away with the mvnw script, given that JDK is already installed and JAVA_HOME environment variable is already set

./mvnw spring-boot:run

Using Spring Initializr with cURL or HTTPie

If you don't like to leave your terminal, you can use Spring Initializr with a command-line HTTP client such as cURL or HTTPie

The following cURL command downloads a new Spring Boot project from Spring Initalizr in tgz file. The tar -xzvf - command extracts the downloaded file into the current directory

curl https://start.spring.io/starter.tgz \  
-d dependencies=webflux,actuator \
-d javaVersion=1.8 \
-d packageName=com.hellokoding.tutorials \
-d groupId=com.hellokoding.tutorials \
| tar -xzvf -

Here is another version using HTTPie

http https://start.spring.io/starter.tgz \  
dependencies=webflux,actuator \  
javaVersion=1.8 \  
packageName=com.hellokoding.tutorials \  
groupId=com.hellokoding.tutorials \  
| tar -xzvf -

Using Spring Initializr with Spring Boot CLI

If you love to use Spring Initialzr via cURL or HTTPie but looking for a less verbose command, then try Spring Boot CLI

To use Spring Boot CLI, you have to first install it on your local machine by using 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 environments can be found at here

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 Initializr via a web interface, cURL, HTTPie, and Spring Boot CLI. You can find more about Spring Initializr at here and Spring Boot CLI at here