使用 org.springframework.cloud 和 spring-boot-starter-parent 自定义 parent pom

custom parent pom with org.springframework.cloud and spring-boot-starter-parent

我有几个 spring 云项目,也希望将所有常见的依赖项放入我自己的 parent pom 中。许多示例显示了如何使用 <dependencyManagement> 进行操作。但在我使用 spring-boot-starter-parent 和 org.springframework.cloud 的情况下,它似乎无法使用依赖管理,因为 parent 已经变为 'spring-boot-starter-parent' 并且依赖管理也有 org.springframework.cloud。以下是我的 spring 云项目的 pom 文件之一。

<groupId>com.demo</groupId>
<artifactId>demo-customer-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo-customer-service</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath /> 
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencyManagement>
    <dependencies>

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

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

如上,有两个parent:org.springframework.boot启动器parent和云。那么我怎样才能拥有自己的 parent 呢?

请问 parent 和 child pom 文件应该怎样做?

您提供的 pom 可以完美地成为 parent pom。 Parent poms 本身也可以有 parent。它很常用,如果你检查你在 github 上使用的项目,它们有 parent 链。
例如 spring-cloud-build-dependenciesspring-boot-dependencies 作为 parent。 spring-boot-dependenciesspring-boot-build 作为 parent。

在您的 parent pom 中,在 <dependencies></dependencies> 中定义的依赖项将添加到您的 parent 的所有 child 中。如果你所有的 child 都在使用这些依赖项,你可以将它们添加到这个标签中。 (我一般都是在这里添加单元测试依赖,但是你可以添加任何东西)
Parent poms 通常位于 childs pom.xml 之上的一个文件夹。 (但是<relativePath>在别的地方也可以用)

|-parent pom.xml
|-child 项目文件夹
|--child pom.xml