The Spring Boot Starter Parent – Spring Boot启动器父母

最后修改: 2019年 6月 10日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

In this tutorial, we’ll learn about spring-boot-starter-parent. We’ll discuss how we can benefit from it for better dependency management, default configurations for plugins, and to quickly build our Spring Boot applications.

在本教程中,我们将了解spring-boot-starter-parent。我们将讨论如何受益于它,以便更好地进行依赖性管理、插件的默认配置,以及快速构建我们的Spring Boot应用程序。

We’ll also see how we can override the versions of existing dependencies and properties provided by starter-parent.

我们还将看到我们如何覆盖由starter-parent.提供的现有依赖和属性的版本。

2. Spring Boot Starter Parent

2.Spring船启动器父母

The spring-boot-starter-parent project is a special starter project that provides default configurations for our application and a complete dependency tree to quickly build our Spring Boot project. It also provides default configurations for Maven plugins, such as maven-failsafe-plugin, maven-jar-plugin, maven-surefire-plugin, and maven-war-plugin.

spring-boot-starter-parent项目是一个特殊的启动项目,它为我们的应用程序提供了默认配置和完整的依赖树,以快速构建我们的Spring Boot项目。它还为Maven插件提供了默认配置,如maven-fails-pluginmaven-jar-pluginmaven-surefire-plugin以及maven-war-plugin

Beyond that, it also inherits dependency management from spring-boot-dependencies, which is the parent to the spring-boot-starter-parent.

除此之外,它还继承了spring-boot-dependencies的依赖性管理,spring-boot-starter-parent的父级。

We can start using it in our project by adding it as a parent in our project’s pom.xml:

我们可以通过在项目的pom.xml中把它作为父级添加到我们的项目中开始使用它。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.0</version>
</parent>

We can always get the latest version of spring-boot-starter-parent from Maven Central.

我们可以随时从Maven中心获得最新版本的spring-boot-starter-parent

3. Managing Dependencies

3.管理依赖性

Once we’ve declared the starter parent in our project, we can pull any dependency from the parent by just declaring it in our dependencies tag. We also don’t need to define versions of the dependencies; Maven will download jar files based on the version defined for the starter parent in the parent tag.

一旦我们在项目中声明了起始父级,我们就可以从父级中提取任何依赖,只需在dependencies标签中声明。我们也不需要定义依赖的版本;Maven会根据父级标签中为起始父级定义的版本下载jar文件。

For example, if we’re building a web project, we can add spring-boot-starter-web directly, and we don’t need to specify the version:

例如,如果我们正在建立一个网络项目,我们可以直接添加spring-boot-starter-web,我们不需要指定版本。

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

4. The Dependency Management Tag

4.依赖性管理标签

To manage a different version of a dependency provided by the starter parent, we can declare dependency and its version explicitly in the dependencyManagement section:

为了管理由启动器父类提供的依赖的不同版本,我们可以在dependencyManagement部分明确声明依赖及其版本。

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.4.0</version>
        </dependency>
    </dependencies>
</dependencyManagement>

5. Properties

5.财产

To change the value of any property defined in the starter parent, we can re-declare it in our properties section.

要改变在启动器父体中定义的任何属性的值,我们可以在我们的属性部分重新声明它。

The spring-boot-starter-parent via its parent spring-boot-dependencies uses properties for configuring all the dependencies versions, Java version, and Maven plugin versions. Therefore, it makes it easy for us to control these configurations by just changing the corresponding property.

spring-boot-starter-parent通过其父spring-boot-dependencies使用属性来配置所有的依赖版本、Java版本和Maven插件版本。因此,它让我们只需改变相应的属性就能轻松控制这些配置。

If we want to change the version of any dependency that we want to pull from the starter parent, we can add the dependency in the dependency tag and directly configure its property:

如果我们想改变任何我们想从启动器父类中提取的依赖的版本,我们可以在依赖标签中添加依赖并直接配置其属性。

<properties>
    <junit.version>4.11</junit.version>
</properties>

6. Other Property Overrides

6.其他属性重写

We can also use properties for other configurations, such as managing plugin versions, or even some base configurations, like managing the Java version, and source encoding. We just need to re-declare the property with a new value.

我们也可以将属性用于其他配置,如管理插件版本,甚至一些基本配置,如管理Java版本,以及源编码。我们只需要用一个新的值来重新声明这个属性。

For example, to change the Java version, we can indicate it in the java.version property:

例如,要改变Java版本,我们可以在java.version属性中指出。

<properties>
    <java.version>1.8</java.version>
</properties>

7. Spring Boot Project Without Starter Parent

7.没有启动器的Spring Boot项目

Sometimes we have a custom Maven parent, or we prefer to declare all our Maven configurations manually.

有时我们有一个自定义的Maven父体,或者我们喜欢手动声明所有的Maven配置。

In that case, we can opt to not use the spring-boot-starter-parent project. But we can still benefit from its dependency tree by adding a dependency, spring-boot-dependencies, in our project in import scope.

在这种情况下,我们可以选择不使用spring-boot-starter-parent项目。但是我们仍然可以通过在我们的项目中import范围内添加一个依赖项spring-boot-dependencies来从它的依赖树中获益。

Let’s illustrate this with a simple example in which we want to use another parent other than the starter parent:

让我们用一个简单的例子来说明这一点,在这个例子中,我们想使用除起始父本之外的另一个父本。

<parent>
    <groupId>com.baeldung</groupId>
    <artifactId>spring-boot-parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</parent>

Here we used parent-modules, a different project, as our parent dependency.

这里我们使用parent-modules,一个不同的项目,作为我们的父级依赖。

Now, in this case, we can still get the same benefits of dependency management by adding it in import scope and pom type:

现在,在这种情况下,我们仍然可以通过在import范围和pom类型中添加它来获得依赖性管理的相同好处。

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.2.6.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Furthermore, we can pull in any dependency by just declaring it in dependencies, as we did in our previous examples. No version numbers are needed for those dependencies.

此外,我们可以通过在dependencies,中声明任何依赖关系,就像我们在以前的例子中做的那样。这些依赖关系不需要版本号。

8. Conclusion

8.结语

In this article, we gave an overview of spring-boot-starter-parent, and the benefits of adding it as a parent in any child project.

在这篇文章中,我们概述了spring-boot-starter-parent,以及在任何子项目中添加它作为父项目的好处。

Next, we learned how to manage dependencies. We can override dependencies in dependencyManagement or via properties.

接下来,我们学习了如何管理依赖关系。我们可以在dependencyManagement中或通过属性覆盖依赖关系。

The source code for the snippets used in this article is available over on Github, one using the starter parent and the other a custom parent.

本文中使用的片段的源代码可在Github上获得,其中一个使用启动器父类,另一个使用自定义父类。