使用 Boot Add 的指南

您可以使用 `project list` 命令中所有可用的專案,向現有專案新增程式碼和配置。

CLI 透過以下方式實現:

  • 合併 Maven 構建檔案,以便將任何缺失的專案屬性、依賴項、依賴管理和外掛新增到目標專案中。

  • 執行包重構,以便將要複製的程式碼以相同的包結構複製到目標專案中。

  • 在目標專案的 Spring Boot 主應用中新增任何缺失的註解。

  • 將 `README.adoc`(或 .md)檔案重新命名為 `README-<專案名稱>.adoc`,以便您可以描述所新增程式碼的附加資訊。

  • 合併 `application.yaml` 和 `application.properties` 檔案。

目前,執行此任務的啟發式演算法尚未完全完善,因此如果您是早期使用者,可能會遇到一些問題。

例如,假設我們添加了入門目錄

spring catalog add gs https://github.com/rd-1-2022/spring-gs-catalog

這將提供以下專案供您選擇

┌──────────┬────────────────────────────────────────────────────────┬────────────────────────────────────────────────────────────────┬───────┬──────────────┐
│Name      │URL                                                     │Description                                                     │Catalog│Tags          │
├──────────┼────────────────────────────────────────────────────────┼────────────────────────────────────────────────────────────────┼───────┼──────────────┤
│web       │https://github.com/rd-1-2022/rpt-rest-service           │Hello, World RESTful web service.                               │gs     │[rest, web]   │
├──────────┼────────────────────────────────────────────────────────┼────────────────────────────────────────────────────────────────┼───────┼──────────────┤
│jpa       │https://github.com/rd-1-2022/rpt-spring-data-jpa        │Learn how to work with JPA data persistence using Spring Data   │gs     │[jpa, h2]     │
│          │                                                        │JPA.                                                            │       │              │
├──────────┼────────────────────────────────────────────────────────┼────────────────────────────────────────────────────────────────┼───────┼──────────────┤
│eureka    │https://github.com/rd-1-2022/eureka                     │Spring Cloud Eureka Server                                      │gs     │[cloud,       │
│          │                                                        │                                                                │       │eureka]       │
└──────────┴────────────────────────────────────────────────────────┴────────────────────────────────────────────────────────────────┴───────┴──────────────┘

我們可以建立一個新的 web 專案,然後透過執行以下命令向該專案新增 JPA 功能

spring boot new demo web --package-name com.xkcd
cd demo
spring boot add jpa

專案樹現在包含 web 應用和 JPA 功能

$ tree
.
├── LICENSE
├── mvnw
├── mvnw.cmd
├── pom.xml
├── README.adoc
├── README-jpa.md
└── src
    ├── main
    │   └── java
    │       └── com
    │           └── xkcd
    │               ├── Application.java
    │               ├── customer
    │               │   ├── CustomerCommandLineRunner.java
    │               │   ├── Customer.java
    │               │   └── CustomerRepository.java
    │               └── greeting
    │                   ├── GreetingController.java
    │                   └── Greeting.java
    └── test
        └── java
            └── com
                └── xkcd
                    ├── customer
                    │   └── CustomerRepositoryTests.java
                    └── greeting
                        └── GreetingControllerTests.java

約定

當您執行 `spring boot add` 時,為了執行智慧的程式碼庫合併,專案必須遵循以下約定

  • 您應該將主要的 `@SpringBootApplication` 放置在包層次結構的根目錄下,所有其他程式碼則放在子包中。

  • `@SpringBootApplication` 類中不應有額外的 `@Bean` 註解。任何配置都應放在單獨的 `@Configuration` 類中。

限制

目前,僅支援單模組 Maven 專案。計劃在 1.0 版本中支援單模組 Gradle 專案。尚無支援多模組專案的時間表。