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

2023.05.01
2024.03.24
プログラミング
JavaSpring Boot

はじめに

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

Spring Boot CLI

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

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

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

Getting Started

Spring Boot のプロジェクト作成

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

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

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

設定できるオプションとデフォルト値

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

1❯ spring help init
2
3spring init - Initialize a new project using Spring Initializr (start.spring.io)
4
5usage: spring init [options] [location]
6
7Option                       Description
8------                       -----------
9-a, --artifact-id <String>   Project coordinates; infer archive name (for
10                               example 'test')
11-b, --boot-version <String>  Spring Boot version (for example '1.2.0.RELEASE')
12--build <String>             Build system to use (for example 'maven' or
13                               'gradle') (default: maven)
14-d, --dependencies <String>  Comma-separated list of dependency identifiers to
15                               include in the generated project
16--description <String>       Project description
17-f, --force                  Force overwrite of existing files
18--format <String>            Format of the generated content (for example
19                               'build' for a build file, 'project' for a
20                               project archive) (default: project)
21-g, --group-id <String>      Project coordinates (for example 'org.test')
22-j, --java-version <String>  Language level (for example '1.8')
23-l, --language <String>      Programming language  (for example 'java')
24--list                       List the capabilities of the service. Use it to
25                               discover the dependencies and the types that are
26                               available
27-n, --name <String>          Project name; infer application name
28-p, --packaging <String>     Project packaging (for example 'jar')
29--package-name <String>      Package name
30-t, --type <String>          Project type. Not normally needed if you use --
31                               build and/or --format. Check the capabilities of
32                               the service (--list) for more details
33--target <String>            URL of the service to use (default: https://start.
34                               spring.io)
35-v, --version <String>       Project version (for example '0.0.1-SNAPSHOT')
36-x, --extract                Extract the project archive. Inferred if a
37                               location is specified without an extension
38
39examples:
40
41    To list all the capabilities of the service:
42        $ spring init --list
43
44    To creates a default project:
45        $ spring init
46
47    To create a web my-app.zip:
48        $ spring init -d=web my-app.zip
49
50    To create a web/data-jpa gradle project unpacked:
51        $ spring init -d=web,jpa --build=gradle my-dir

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

1❯ spring init --list
2
3...
4
5Parameters
6+-------------+------------------------------------------+------------------------------+
7| Id          | Description                              | Default value                |
8+-------------+------------------------------------------+------------------------------+
9| artifactId  | project coordinates (infer archive name) | demo                         |
10| bootVersion | spring boot version                      | 3.0.6                        |
11| description | project description                      | Demo project for Spring Boot |
12| groupId     | project coordinates                      | com.example                  |
13| javaVersion | language level                           | 17                           |
14| language    | programming language                     | java                         |
15| name        | project name (infer application name)    | demo                         |
16| packageName | root package                             | com.example.demo             |
17| packaging   | project packaging                        | jar                          |
18| type        | project type                             | gradle-project               |
19| version     | project version                          | 0.0.1-SNAPSHOT               |
20+-------------+------------------------------------------+------------------------------+

参考

Support

\ この記事が役に立ったと思ったら、サポートお願いします! /

buy me a coffee
Share

Profile

author

Masa

都内のIT企業で働くエンジニア
自分が学んだことをブログでわかりやすく発信していきながらスキルアップを目指していきます!

buy me a coffee