pom.xml文件中两种父配置方法及注意事项

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

如果你不想使用某个依赖默认的版本,您还可以通过覆盖自己的项目中的属性来覆盖各个依赖项,例如,要升级到另一个Spring Data版本系列,您可以将以下内容添加到pom.xml中。

<properties>
    <spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>

详细依赖可查看:spring-boot-parent-1.5.3.RELEASE.pom


但是如果项目有自己的继承(有自己的parent),则需要如下:

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

该设置不允许您使用如上所述的属性(properties)覆盖各个依赖项,要实现相同的结果,您需要在spring-boot-dependencies项之前的项目的dependencyManagement中添加一个配置,例如,要升级到另一个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.1.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

详细依赖可查看:spring-boot-dependencies-1.5.3.RELEASE.pom。spring-boot-dependencies 根本就没有对应的jar包,它只是一个 pom。

配置有了它之后,我们在 子项目中使用到的相关依赖,就不需要声明version了。

但是在子项目的 dependencies 中,不需要(也不能)再次添加对 spring-boot-dependencies 的声明了,否则 子项目 将无法编译通过。

需要注意的是,导入后再重新定义既繁琐又不安全。比如,spring-boot定义了8个tomcat依赖项,如果你导入后只重定义了部分tomcat依赖项及其版本,则将造成版本不一致的问题。
那么导入时有没有既简单又安全的方法呢?我们可先继承后导入!
1、先建一个过渡性工程,继承后定制依赖项的版本。
<project>
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>1.1.9.RELEASE</version>
  </parent>
  <groupId>mycomp</groupId>
  <artifactId>myproject-spring-boot-bom</artifactId>
  <version>1.1.9</version>

  <packaging>pom</packaging>

  <properties>
    <spring.version>4.1.6.RELEASE</spring.version>
  </properties>
</project>
2、然后导入到自己的工程里。
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>mycomp</groupId>
  <artifactId>myproject</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>mycomp</groupId>
        <artifactId>myproject-spring-boot-bom</artifactId>
        <version>1.1.9</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>
这样,虽然多建了一个过渡性工程,但定制依赖项版本同继承时一样简单。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值