【Java】コマンドラインからSpring Bootを始める



IDE を使わずに、コマンドラインからのみで Spring Boot を始める方法を紹介します。

Spring Boot CLI

Spring Boot CLI は Spring Boot のプロジェクトを新規作成できたりするコマンドラインツールになります。

Mac であればbrewを使ってインストールできます。

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

Spring Boot のプロジェクト作成

spring initで Spring Initializer を使った新規プロジェクトの初期化ができます。

下記だと、依存関係としてspring-boot-starter-web、名前をsample-maven-spring-boot-appの Maven でビルドするプロジェクトを作成できます。

spring init -d=web --build=maven -n=sample-spring-boot-maven-app sample-spring-boot-maven-app


設定できるオプションはspring help initで確認できます。

❯ 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
-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.
-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


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

    To creates a default project:
        $ spring init

    To create a web my-app.zip:
        $ spring init -d=web my-app.zip

    To create a web/data-jpa gradle project unpacked:
        $ spring init -d=web,jpa --build=gradle my-dir

spring init --listで設定できるパラメータとデフォルト値が確認できます。

❯ spring init --list


| Id          | Description                              | Default value                |
| artifactId  | project coordinates (infer archive name) | demo                         |
| bootVersion | spring boot version                      | 3.0.6                        |
| description | project description                      | Demo project for Spring Boot |
| groupId     | project coordinates                      | com.example                  |
| javaVersion | language level                           | 17                           |
| language    | programming language                     | java                         |
| name        | project name (infer application name)    | demo                         |
| packageName | root package                             | com.example.demo             |
| packaging   | project packaging                        | jar                          |
| type        | project type                             | gradle-project               |
| version     | project version                          | 0.0.1-SNAPSHOT               |