Creating New Projects

spring boot new 命令可以轻松创建 Spring Boot 应用程序。

The spring boot new command makes it easy to create Spring Boot applications.

此命令会从现有功能齐全的 Spring Boot 应用程序的源代码存储库复制内容,以创建一个新的项目。

This command copies the content from the source code repository of an existing fully functional Spring Boot application to create a new project.

要开始,请指定 --name 选项,它创建一个基本的 RESTful Web 应用程序并进行测试。您还可以使用 --from 选项创建具有不同功能的项目,例如 Spring Data JPA 或 Spring Cloud Stream。

To get started, specify the --name option, which creates a basic RESTful web application with a test. You can also use the --from option to create projects with different functionalities, such as Spring Data JPA or Spring Cloud Stream.

要进行进一步的自定义,你可以灵活指定各种选项(如“--group”、“--artifact-id”和“--version”),以及“--package-name”和“--description”。如果你提供了“--package-name”,代码将在克隆源代码存储库之后重构。

For further customization, you have the flexibility to specify various options (such as --group, --artifact-id, and --version) as well as --package-name and --description. If you provide the --package-name, the code is refactored after cloning the source code repository.

以下各节详细介绍了这些主题中的每一个主题。

The following sections provide detailed explanations of each of these topics.

有关向现有应用程序添加其他代码的更多信息,请参阅 spring boot addUser Defined Commands

For more information on adding additional code to an already existing application, see spring boot add and User Defined Commands.

Quick Start

若要在新的目录中快速创建一个简单的 Web 应用程序,请运行以下命令:

To quickly create a simple web application in a new directory, run the following commands:

spring boot new my-app
cd my-app
./mnvw spring-boot:run

这将在“my-app”目录中创建一个基本的 Web 应用程序。“--name”选项在此示例中按位置传递,相当于“spring boot new --name my-app”。

This creates a basic web application in the my-app directory. The --name option is passed positionally in this example, equivalent to spring boot new --name my-app.

如果你希望在当前工作目录中创建项目,请将“.”指定为名称:

If you prefer to create a project in your current working directory, specify . as the name:

mkdir my-new-app
cd my-new-app
spring boot new .
./mvnw spring-boot:run

若要使用不同的功能创建应用程序,请在“spring boot new my-app”命令中使用“--from”选项。由于“--from”在“--name”之后,因此你可以使用位置参数。

To create applications with different functionality, use the option --from in the spring boot new my-app command. Since --from comes after --name you can use positional parameters.

例如:

For example:

spring boot new my-app jpa
cd my-app
./mnvw spring-boot:run

上述命令创建一个包含 Spring Data JPA 功能的新应用程序。

The preceding command creates a new application that includes Spring Data JPA functionality.

在上述示例中,新应用程序的名称(“my-app”)和“--from”选项(“jpa”)都是作为位置参数提供的。这相当于使用“spring boot new --name my-app --from jpa”。

In the preceding example, both the name of the new app (my-app) and the --from option (jpa) are provided as positional parameters. It is equivalent to using spring boot new --name my-app --from jpa.

名称“jpa”已在默认的入门项目目录中注册,并作为到具有某些附加元数据的特定 URL 的别名。以下部分将对此进行更详细的描述。

The name, jpa, is registered with the default getting started project catalog and serves as an alias to a specific URL with some additional metadata. This is described more in the following section.

Creating by Name

项目目录包含演示特定 Spring 技术的代码存储库的列表。类似于 Spring 入门指南中的内容,默认项目目录中的存储库提供了一系列功能。

The project catalog contains a list of code repositories that demonstrate specific Spring technologies. The repositories in the default project catalog, resembling the content in the Spring Getting Started Guides, provide a range of functionalities.

你可以选择注册自己的目录,提供不同复杂程度和功能的代码存储库。

You can have the option to register your own catalogs, offering code repositories of varying complexity and functionality.

项目目录可以帮助你为新项目找到所需的功能。你在使用“spring boot add”命令向现有项目添加代码时,还可以参考项目目录中的项目名称。

The project catalog helps you find the desired functionality for your new projects. You can also refer to the project names from the project catalog when adding code to an existing project by using the spring boot add command.

若要查看默认“入门”目录中注册的项目,请运行以下命令:

To view the registered projects in the default "getting started" catalog, run the following command:

spring project-catalog list

此命令显示了可与“spring boot new”命令一起使用的可用项目目录:

This command displays the available project catalogs that you can use with the spring boot new command:

┌────┬──────────────────────────────────────────────┬───────────────────────┬───────────────────┐
│Name│URL                                           │Description            │Tags               │
├────┼──────────────────────────────────────────────┼───────────────────────┼───────────────────┤
│gs  │https://github.com/rd-1-2022/spring-gs-catalog│Getting Started Catalog│[java-11, boot-2.7]│
└────┴──────────────────────────────────────────────┴───────────────────────┴───────────────────┘
```

若要查看可用项目,请使用以下命令:

To view the available projects, use the following command:

spring project list

此命令提供你可以使用的项目列表。每个项目都有一个可传递给“spring boot new”的名称。下表显示了典型的输出:

This command provides a list of projects that you can use. Each project has a name that you can passed to spring boot new. The following table shows typical output:

┌─────────────┬────────────────────────────────────────────────────────┬─────────────────────┬───────┬───────────────┐
│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 的新项目,请运行以下命令:

To create a new project that uses Spring Data JPA, run the following command:

spring boot new my-jpa jpa

此命令使用来自源代码存储库 URL:“https://github.com/rd-1-2022/rpt-spring-data-jpa”的应用程序。

This command uses the application from the source code repository URL: https://github.com/rd-1-2022/rpt-spring-data-jpa.

您还可以使用项目名称作为 spring boot add 命令的参数。

You can also use the name of the project as the argument to the spring boot add command.

Creating by URL

你可以直接使用项目源存储库的 URL,而不必将项目添加到 Spring CLI 以短名称对其进行引用。

Instead of adding a project to the Spring CLI so as to reference it with a short name, you can use the URL of the project’s source repository directly.

例如,要创建 Spring JPA 项目,可以使用以下命令:

For example, to create the Spring JPA project, you can use the following command:

spring boot new my-jpa https://github.com/rd-1-2022/rpt-spring-data-jpa

Options

spring boot new 命令采用以下选项,您可以通过运行以下命令查看这些选项:

The spring boot new command takes the following options, which you can see by running the following command:

spring boot new --help

该命令显示以下输出:

That command shows the following output:

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]

Options and Default Values

仅指定 --name 选项后,artifactId 会默认为 --name 选项的值。请考虑以下示例:

By specifying just the --name option, the artifactId defaults to the value of the --name option. Consider the following example:

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

In the generated pom.xml, the name (myapp) is used as the artifactId and the name of the project:

	<groupId>com.example</groupId>
	<artifactId>myapp</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>myapp</name>
	<description>RESTful web application</description>

添加 --groupid 选项不仅会更改 groupId 标记的值,还会更改包名。项目会被重构到新的包名中。请考虑以下示例:

Adding the --groupid option changes not only the value of the groupId tag but also the package name. A refactoring of the project to the new package name is performed. Consider the following example:

$ 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 包含以下内容:

The generated pom.xml contains the following content:

	<groupId>com.xkcd</groupId>
	<artifactId>myapp</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>myapp</name>
	<description>RESTful web application</description>

项目的目录结构如下:

The directory structure of the project is as follows:

$ 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