前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring boot自定义parent POM

Spring boot自定义parent POM

作者头像
程序那些事
发布2020-07-08 10:45:03
9480
发布2020-07-08 10:45:03
举报
文章被收录于专栏:程序那些事程序那些事

概述

在之前的Spring Boot例子中,我们都会用到这样的parent POM。

代码语言:javascript
复制
    <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该怎么处理呢?

本文将会解决这个问题。

不使用Parent POM来引入Spring boot

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

在实际中,如果我们因为种种原因,不能使用Spring boot自带的parent,那么我们可以这样做:

代码语言:javascript
复制
<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的依赖了,例如:

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

另一方面,如果不使用parent POM,Spring boot自带的plugin,需要我们自己引入:

代码语言:javascript
复制
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

覆盖依赖项版本

如果我们需要使用和parent POM中定义的不同的依赖项版本,则可以在dependencyManagement中重写。

代码语言:javascript
复制
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>1.5.5.RELEASE</version>
        </dependency>
    </dependencies>
    // ...
</dependencyManagement>

当然,你也可以在每次引入依赖的时候,指定所需要的版本。

更多教程请参考 flydean的博客

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-02-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序那些事 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概述
  • 不使用Parent POM来引入Spring boot
  • 覆盖依赖项版本
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档