13.2.2 Using Spring Boot without the parent POM

不是每个人都喜欢继承spring-boot-starter-parent POM,比如你可能需要使用公司的标准parent,或只是倾向于显式声明所有的Maven配置,和Java一样POM也是不允许多继承的,因此有的时候,我们依然保留spring-boot-starter-parent POM便捷的依赖管理(对plugin management不能采用此方式),但又不需要继承spring-boot-starter-parent POM,可以使用如下做法:

<dependencyManagement>
     <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.5.7.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

但是这样的设置是不允许你如13.2.1. Inheriting the starter parent中所说的使用属性<properties>的方式覆盖个别依赖(override individual dependencies using a property),为了实现这个目的,你需要在项目的dependencyManagement 节点中,在spring-boot-dependencies实体前插入一个节点。例如,为了将Spring Data升级到另一个发布版本,你需要将以下配置添加到自己项目的pom.xml中:

<dependencyManagement>
    <dependencies>
        <!-- Override Spring Data release train provided by Spring Boot -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-releasetrain</artifactId>
            <version>Fowler-SR2</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.5.7.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

解释一下,为何不能像Dependency Management一样保留Plugin Management呢?与依赖配置不同的是,通常所有项目对于任意一个依赖的配置都应该是统一的,但插件却不是这样,例如你可以希望模块A运行所有单元测试,模块B要跳过一些测试,这时就需要配置maven-surefire-plugin来实现,那样两个模块的插件配置就不一致了。这也就是说,简单的把插件配置提取到父POM的pluginManagement中往往不适合所有情况,那我们在使用的时候就需要注意了,只有那些普适的插件配置才应该使用pluginManagement提取到父POM中。

关于插件pluginManagement,Maven并没有提供与import scope依赖类似的方式管理,那我们只能借助继承关系,不过好在一般来说插件配置的数量远没有依赖配置那么多,因此这也不是一个问题。

results matching ""

    No results matching ""