Boot Add 使用指南

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

CLI 透過以下方式實現:

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

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

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

  • README.adoc(或 .md)檔案重新命名為 README-<project-name>.adoc,以便您可以描述新增的程式碼的其他資訊。

  • 合併 application.yamlapplication.properties 檔案。

目前,執行此任務的啟發式演算法尚未 100% 完成,因此早期使用者可能會遇到一些問題。

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

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 專案。尚未確定支援多模組專案的時間表。

© . This site is unofficial and not affiliated with VMware.