1. Overview
1.概述
Spring Boot provides the parent POM for an easier creation of Spring Boot applications.
Spring Boot提供了父级POM,以方便创建Spring Boot应用程序。
However, using the parent POM may not always be desirable, if we already have a parent to inherit from.
然而,如果我们已经有了一个可以继承的父本,那么使用父本POM可能并不总是可取的。
In this quick tutorial, we’re going to take a look at how we can still use Boot without the parent starter.
在这个快速教程中,我们将看看如何在没有父启动器的情况下仍然使用Boot。
2. Spring Boot Without Parent POM
2.Spring靴无家长POM
The parent pom.xml takes care of dependency and plugin management. For that reason, inheriting from it provides valuable support in an application, so it is usually the preferred course of action when creating a Boot application. You can find more details on how to build an application based on the parent starter in our previous article.
父级pom.xml负责处理依赖和插件管理。由于这个原因,从它那里继承在应用程序中提供了有价值的支持,所以在创建Boot应用程序时,它通常是首选的行动方案。您可以在我们之前的文章中找到有关如何基于父启动器构建应用程序的更多细节。
In practice though, we may be constrained by design rules or other preferences to use a different parent.
但在实践中,我们可能会受到设计规则或其他偏好的限制而使用不同的母体。
Fortunately, Spring Boot offers an alternative to inheriting from the parent starter, that can still afford us some of its advantages.
幸运的是,Spring Boot提供了一种替代从父级启动器中继承的方法,它仍然可以为我们提供一些优势。
If we don’t make use of the parent POM, we can still benefit from dependency management by adding the spring-boot-dependencies artifact with scope=import:
如果我们不使用父POM,我们仍然可以通过添加spring-boot-dependencies artifact与scope=import来从依赖管理中获益。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Next, we can start simply start adding Spring dependencies and making use of Spring Boot features:
接下来,我们可以开始简单地开始添加Spring依赖,并利用Spring Boot功能。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
On the other hand, without the parent POM, we no longer benefit from plugin management. This means we need to add the spring-boot-maven-plugin explicitly:
另一方面,如果没有父级POM,我们就无法再从插件管理中获益。这意味着我们需要明确添加spring-boot-maven-plugin。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3. Overriding Dependency Versions
3.覆盖依赖性版本
If we want to use a different version for a certain dependency than the one managed by Boot, we need to declare it in the dependencyManagement section, before spring-boot-dependencies is declared:
如果我们想为某个依赖关系使用不同于Boot管理的版本,我们需要在dependencyManagement部分声明它,然后再声明spring-boot-dependencies。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.4.0</version>
</dependency>
</dependencies>
// ...
</dependencyManagement>
By contrast, just declaring the version for the dependency outside the dependencyManagement tag will no longer work.
相比之下,仅仅在dependencyManagement标签之外为依赖关系声明版本将不再有效。
4. Conclusion
4.结论
In this quick tutorial, we’ve seen how we can use Spring Boot without the parent pom.xml.
在这个快速教程中,我们已经看到了如何在没有父级pom.xml.的情况下使用Spring Boot。
The source code for the examples can be found over on GitHub.
这些例子的源代码可以在GitHub上找到。