Spring Boot cli 는 command line  도구로 빠르게 스프링 개발 환경을 구성할 수 있습니다.

Spring Boot cli 설치는 SDKMan 을 사용해서 설치하면 됩니다.

$ sdk install springboot
BASH


사용하기

설치가 끝났으면 spring 이라는 명령어로 실행해 보면 됩니다.

$ spring

usage: spring [--help] [--version] 
       <command> [<args>]

Available commands are:

  run [options] <files> [--] [args]
    Run a spring groovy script
...
BASH

help 명령어 뒤에 command 를 지정하면 상세 옵션을 볼 수 있으며 다음은 새로운 스프링 프로젝트를 생성하는 init 명령어의 상세 사용법을 표시합니다.

$  spring help init
spring init - Initialize a new project using Spring Initializr (start.spring.io)

usage: spring init [options] [location]

Option                       Description                                       
------                       -----------                                       
-a, --artifactId <String>    Project coordinates; infer archive name (for      
                               example 'test') 
BASH


app 실행

Groovy 소스를 run 명령어로 바로 실행할 수 있습니다. spring boot cli 를 사용하면 외부 의존성도 다 결정해 주므로 groovy 를 직접 설치하지 않아도 됩니다.

hello.groovy

@RestController
class WebApplication {

    @RequestMapping("/")
    String home() {
        "Hello World!"
    }

}
GROOVY


다음 명령어로 그루비 app 을 직접 실행할 수 있습니다.

$ spring run hello.groovy

Resolving dependencies.............................

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.5.2)

BASH


기본 포트인 8080 대신 다른 포트를 사용하려면 spring boot 에 포트 옵션을 전달해야 합니다.

스프링 부트에 전달할 옵션은 대시를 2 개 사용 한후에 전달할 옵션을 지정하면 되며 다음은 port 를 9000 으로 사용합니다.

$ spring run hello.groovy -- --server.port=9000
BASH


신규 프로젝트 생성

init 명령어를 사용하면 Spring initialzr(https://start.spring.io/) 의 기능을 쉘에서 바로 실행할 수 있습니다.

다음은 maven 기반으로 3 가지 의존성을 가진 spring boot 프로젝트를 생성합니다.

$ spring init --dependencies=web,lombok,data-jpa my-awesome-project
BASH


지원 기능 보기

init 에서 지원 가능한 모든 기능을 보려면 --list 옵션을 사용하면 됩니다.

$ spring init --list

:: Service capabilities ::  https://start.spring.io

Supported dependencies
+--------------------------------------+--------------------------------------------------------------+--------------------------------+
| Id                                   | Description                                                  | Required version               |
+--------------------------------------+--------------------------------------------------------------+--------------------------------+
| activemq                             | Spring JMS support with Apache ActiveMQ 'Classic'.           |                                |
|                                      |                                                              |                                |
| actuator                             | Supports built in (or custom) endpoints that let you monitor |                                |
|                                      | and manage your application - such as application health,    |                                |
|                                      | metrics, sessions, etc.                                      |                                |
|                                      |                                                              |                                |
| amqp                                 | Gives your applications a common platform to send and        |                                |
|                                      | receive messages, and your messages a safe place to live     |                                |
|                                      | until received.                                              |                                |
BASH


신규 프로젝트 생성

--list 옵션 출력 맨 뒤에 기본 설정이 있으므로 기본 설정을 변경하지 않을 경우 생략해도 됩니다.

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.5.2                        |
| description | project description                      | Demo project for Spring Boot |
| groupId     | project coordinates                      | com.example                  |
| javaVersion | language level                           | 11                           |
| 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               |
+-------------+------------------------------------------+------------------------------+

CODE

주의할 점은 camel case 인 Id 를 커맨드 라인에서 전달할 경우 slug 방식으로 변경해야 한다는 것입니다.

예로 packageName 을 커맨드 라인에서 변경할 경우 --package-name 처럼 적어줘야 합니다.


다음은 JDK 16 을 사용하는 gradle 기반 web 프로젝트를 생성합니다.

$ spring init --build=gradle --format=project --java-version=16 --dependencies=web --packaging=jar --package-name=com.lesstif.demo my-new-project
CODE


같이 보기

Ref