Guide to Using Boot Add

您可以使用“project list”命令中提供的所有项目将代码和配置添加到现有项目中。

You can use all the projects that are available in the project list command to add code and configuration to an existing project.

CLI 是通过以下方式完成此操作的:

The CLI does this by:

  • Merging the Maven build file, so that any missing project properties, dependencies, dependency management, and plug-ins are added into the target project.

  • Performing a package refactoring so that the code to be copied into the target project with the same package structure.

  • Adding any missing annotations on the Spring Boot main application in the target project.

  • Renaming the README.adoc (or .md) file to README-<project-name>.adoc so that you can describe additional information about what code was added.

  • Merging application.yaml and application.properties files.

目前执行此任务的启发式方法并非 100% 完整,所以如果你是一位早期采用者,可能会遇到一些问题。

The heuristic to perform this task is not 100% complete at this time, so expect a few bumps if you are an early adopter.

例如,假设我们已添加快速入门目录:

For example, assume we have added the getting started catalog:

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

这会提供以下可供选择的项目:

This gives us the following projects from which to select:

┌──────────┬────────────────────────────────────────────────────────┬────────────────────────────────────────────────────────────────┬───────┬──────────────┐
│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 功能:

We can create a new web project and then add JPA functionality to that project by running the following commands:

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

项目树现在包含 Web 应用程序和 JPA 功能:

The project tree now contains both the web application and the JPA functionality:

$ 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

Conventions

当你运行 spring boot add 时,为了对代码库执行智能合并,项目中必须遵循以下约定:

To perform an intelligent merge of the code base when you run spring boot add, the following conventions in the project must be followed:

  • You should place the main @SpringBootApplication at the root of the package hierarchy with all other code in subpackages.

  • There should be no additional @Bean annotations in the @SpringBootApplication class. Any configuration should live in a separate @Configuration class.

Limitations

目前,仅支持单模块 Maven 项目。计划在 1.0 版本中支持单模块 Gradle 项目。尚未确定对多模块项目提供支持的时间表。

Currently, only single-module Maven projects are supported. Support for single-module Gradle projects is planned for the 1.0 release. No timeline is defined for supporting multi-module projects.