zoukankan      html  css  js  c++  java
  • Spring Boot 不使用默认的 parent,改用自己的项目的 parent

    在初学spring boot时,官方示例中,都是让我们继承一个spring的 spring-boot-starter-parent这个parent:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    但是,一般情况下,在我们自己的项目中,会定义一下自己的 parent 项目,这种情况下,上面的这种做法就行不通了。那么,该如何来做呢?其实,在spring的官网也给出了变通的方法的:

    在我们自己 parent 项目中,加下下面的声明

    <dependencyManagement>
        <dependencies>
            <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>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    请注意,它的 type 是 pom,scope 是 import,这种类型的 dependency 只能在 dependencyManagement 标签中声明。

    然后,把我们项目中的 子项目 中,parent 的声明,修改为我们自己项目的 parent 项目就可以了,比如,我的是:

        <parent>
            <groupId>org.test</groupId>
            <artifactId>spring</artifactId>
            <version>0.1-SNAPSHOT</version>
        </parent>
    • 1
    • 2
    • 3
    • 4
    • 5

    有一点,需要注意一下。 
    在 子项目 的 dependencies 中,不需要(也不能)再次添加对 spring-boot-dependencies 的声明了,否则 子项目 将无法编译通过。 
    即,在 子项目 中,下面的配置是多余的:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
    </dependency>
    • 1
    • 2
    • 3
    • 4

    为什么会这个样子呢? 
    因为 spring-boot-dependencies 根本就没有对应的jar包,它只是一个 pom 配置,可以去 maven仓库 看一下。 
    它里面定义了 非常多 的依赖声明。

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    如,spring-boot-starter-web 和 spring-boot-starter-test 在 spring-boot-dependencies 中的声明分别为:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>1.5.1.RELEASE</version>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>1.5.1.RELEASE</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    参考文档 
    spring 官方文档

    版权声明:本文为博主原创文章,欢迎转载,但是必须在文章的开头声明出处! http://blog.csdn.net/rainbow702/article/details/55046298
  • 相关阅读:
    POJ 1887 Testing the CATCHER
    HDU 3374 String Problem
    HDU 2609 How many
    POJ 1509 Glass Beads
    POJ 1458 Common Subsequence
    POJ 1159 Palindrome
    POJ 1056 IMMEDIATE DECODABILITY
    POJ 3080 Blue Jeans
    POJ 1200 Crazy Search
    软件体系结构的艺术阅读笔记1
  • 原文地址:https://www.cnblogs.com/yaowen/p/8623166.html
Copyright © 2011-2022 走看看