Spring boot自定义parent POM

概述

在以前的Spring Boot例子中,咱们都会用到这样的parent POM。web

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

这个parent指定了spring-boot所须要的依赖。可是有时候若是咱们的项目已经有一个parent了,这时候须要引入spring boot该怎么处理呢?spring

本文将会解决这个问题。maven

不使用Parent POM来引入Spring boot

parent pom.xml主要处理的是依赖和使用的插件管理。使用起来会很是简单,这也是咱们在Spring boot中经常使用的方式。spring-boot

在实际中,若是咱们由于种种缘由,不能使用Spring boot自带的parent,那么咱们能够这样作:ui

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

将spring-boot-dependencies做为一个依赖放入dependencyManagement标签便可。注意,这里的scope要使用import。插件

接下来,咱们就能够随意使用spring boot的依赖了,例如:code

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

另外一方面,若是不使用parent POM,Spring boot自带的plugin,须要咱们本身引入:xml

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

覆盖依赖项版本

若是咱们须要使用和parent POM中定义的不一样的依赖项版本,则能够在dependencyManagement中重写。教程

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

固然,你也能够在每次引入依赖的时候,指定所须要的版本。ci

更多教程请参考 flydean的博客

相关文章
相关标签/搜索