建立新專案
spring boot new
命令讓建立 Spring Boot 應用程式變得容易。
這個命令會複製現有完整功能的 Spring Boot 應用程式的原始碼儲存庫內容,以建立新專案。
若要開始使用,請指定 --name
選項,這會建立一個包含測試的基本 RESTful 網路應用程式。您也可以使用 --from
選項來建立具有不同功能的專案,例如 Spring Data JPA 或 Spring Cloud Stream。
為了進一步自訂,您可以彈性地指定各種選項 (例如 --group
、--artifact-id
和 --version
),以及 --package-name
和 --description
。如果您提供 --package-name
,則在複製原始碼儲存庫後,程式碼會進行重構。
以下章節提供對這些主題的詳細說明。
如需將額外程式碼新增至現有應用程式的更多資訊,請參閱 spring boot add 和 使用者定義命令。
快速開始
若要在新目錄中快速建立一個簡單的網路應用程式,請執行以下命令
spring boot new my-app
cd my-app
./mnvw spring-boot:run
這會在 my-app
目錄中建立一個基本的網路應用程式。在此範例中,--name
選項是透過位置參數傳遞的,相當於 spring boot new --name my-app
。
如果您偏好在目前的工作目錄中建立專案,請指定 .
作為名稱
mkdir my-new-app
cd my-new-app
spring boot new .
./mvnw spring-boot:run
若要建立具有不同功能的應用程式,請在 spring boot new my-app
命令中使用 --from
選項。由於 --from
在 --name
之後,您可以使用位置參數。
例如
spring boot new my-app jpa
cd my-app
./mnvw spring-boot:run
前述命令會建立一個包含 Spring Data JPA 功能的新應用程式。
在前述範例中,新應用程式的名稱 (my-app
) 和 --from
選項 (jpa
) 都是以位置參數提供的。它相當於使用 spring boot new --name my-app --from jpa
。
名稱 jpa
已在預設的入門專案 Catalog 中註冊,並作為特定 URL 的別名,並帶有一些額外的元數據。這將在以下章節中更詳細地說明。
依名稱建立
專案 Catalog 包含一個程式碼儲存庫列表,這些儲存庫示範了特定的 Spring 技術。預設專案 Catalog 中的儲存庫,類似於 Spring 入門指南中的內容,提供了一系列功能。
您可以選擇註冊自己的 Catalog,提供具有不同複雜度和功能的程式碼儲存庫。
專案 Catalog 可協助您為新專案找到所需的功能。您也可以在使用 spring boot add
命令將程式碼新增至現有專案時,參考專案 Catalog 中的專案名稱。
若要檢視預設「入門」Catalog 中已註冊的專案,請執行以下命令
spring project-catalog list
這個命令會顯示您可以使用 spring boot new
命令的可用專案 Catalog。
┌────┬──────────────────────────────────────────────┬───────────────────────┬───────────────────┐
│Name│URL │Description │Tags │
├────┼──────────────────────────────────────────────┼───────────────────────┼───────────────────┤
│gs │https://github.com/rd-1-2022/spring-gs-catalog│Getting Started Catalog│[java-11, boot-2.7]│
└────┴──────────────────────────────────────────────┴───────────────────────┴───────────────────┘
```
若要檢視可用的專案,請使用以下命令
spring project list
這個命令會提供您可以使用的專案列表。每個專案都有一個名稱,您可以將其傳遞給 spring boot new
。下表顯示了典型的輸出
┌─────────────┬────────────────────────────────────────────────────────┬─────────────────────┬───────┬───────────────┐
│Name │URL │Description │Catalog│Tags │
├─────────────┼────────────────────────────────────────────────────────┼─────────────────────┼───────┼───────────────┤
│web │https://github.com/rd-1-2022/rest-service │Hello, World RESTful │gs │[rest, web] │
│ │ │web service. │ │ │
├─────────────┼────────────────────────────────────────────────────────┼─────────────────────┼───────┼───────────────┤
│jpa │https://github.com/rd-1-2022/rpt-spring-data-jpa │Learn how to work │gs │[jpa, h2] │
│ │ │with JPA data │ │ │
│ │ │persistence using │ │ │
│ │ │Spring Data JPA. │ │ │
├─────────────┼────────────────────────────────────────────────────────┼─────────────────────┼───────┼───────────────┤
│scheduling │https://github.com/rd-1-2022/rpt-spring-scheduling-tasks│How to schedule tasks│gs │[scheduling] │
├─────────────┼────────────────────────────────────────────────────────┼─────────────────────┼───────┼───────────────┤
│config-client│https://github.com/rd-1-2022/rpt-config-client │Using the Config │gs │[config] │
│ │ │Client library │ │ │
├─────────────┼────────────────────────────────────────────────────────┼─────────────────────┼───────┼───────────────┤
│eureka │https://github.com/rd-1-2022/eureka │Spring Cloud Eureka │gs │[cloud, eureka]│
│ │ │Server │ │ │
├─────────────┼────────────────────────────────────────────────────────┼─────────────────────┼───────┼───────────────┤
│graphql │https://github.com/rd-1-2022/rpt-spring-graphql │Spring GraphQL │gs │[graphql] │
└─────────────┴────────────────────────────────────────────────────────┴─────────────────────┴───────┴───────────────┘
若要建立一個使用 Spring Data JPA 的新專案,請執行以下命令
spring boot new my-jpa jpa
這個命令使用來自原始碼儲存庫 URL 的應用程式:github.com/rd-1-2022/rpt-spring-data-jpa
。
您也可以使用專案的名稱作為 spring boot add 命令的引數。 |
依 URL 建立
除了將專案新增到 Spring CLI 以使用簡短名稱參考它之外,您也可以直接使用專案原始碼儲存庫的 URL。
例如,若要建立 Spring JPA 專案,您可以使用以下命令
spring boot new my-jpa https://github.com/rd-1-2022/rpt-spring-data-jpa
選項
spring boot new
命令接受以下選項,您可以透過執行以下命令來查看這些選項
spring boot new --help
該命令顯示以下輸出
NAME
boot new - Create a new Spring Boot project from an existing project
SYNOPSIS
boot new --from String --name String --group-id String --artifact-id String --version String --description String --package-name String --path String --help
OPTIONS
--name String
Name of the new project
[Mandatory]
--from String
Create project from existing project name or URL
[Optional]
--group-id String
Group ID of the new project
[Optional]
--artifact-id String
Artifact ID of the new project
[Optional]
--version String
Version of the new project
[Optional]
--description String
Description of the new project
[Optional]
--package-name String
Package name for the new project
[Optional]
--path String
Path to run the command in, most of the time this is not necessary to specify and the default value is the current working directory.
[Optional]
--help or -h
help for boot new
[Optional]
選項和預設值
僅指定 --name
選項時,artifactId
預設為 --name
選項的值。請考慮以下範例
spring boot new --name myapp
Cloning project from https://github.com/rd-1-2022/rest-service
Created project in directory 'myapp'
在產生的 pom.xml
中,名稱 (myapp
) 用作專案的 artifactId
和 name
。
<groupId>com.example</groupId>
<artifactId>myapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>myapp</name>
<description>RESTful web application</description>
新增 --groupid
選項不僅會變更 groupId
標籤的值,也會變更套件名稱。系統會執行專案重構以使用新的套件名稱。請考慮以下範例
$ spring boot new --name myapp --group-id com.xkcd
Cloning project from https://github.com/rd-1-2022/rest-service
Refactoring package to com.xkcd.myapp
Created project in directory 'myapp'
產生的 pom.xml
包含以下內容
<groupId>com.xkcd</groupId>
<artifactId>myapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>myapp</name>
<description>RESTful web application</description>
專案的目錄結構如下所示
$ tree myapp/
myapp/
├── LICENSE
├── mvnw
├── mvnw.cmd
├── pom.xml
├── README.adoc
└── src
├── main
│ └── java
│ └── com
│ └── xkcd
│ └── myapp
│ ├── Application.java
│ └── greeting
│ ├── GreetingController.java
│ └── Greeting.java
└── test
└── java
└── com
└── xkcd
└── myapp
└── greeting
└── GreetingControllerTests.java