怎么使用 Maven 来构建一个 SpringBoot 程序?

发布于 2020-07-11 15:54:56
关注者
0
被浏览
546
1 个回答
  • 面试哥
    面试哥 2020-07-11
    为面试而生,有面试问题,就找面试哥。

    就像引入其他库一样,我们可以在 Maven 工程中加入 SpringBoot 依赖。然而,最好是从 spring-boot-starter-parent 项目中继承以及声明依赖到 Spring Boot starters。这样做可以使我们的项目可以重用 SpringBoot 的默认配置。

    继承 spring-boot-starter-parent 项目依赖很简单 – 我们只需要在 pom.xml 中定义一个 parent 节点:

    1 <parent>
    2     <groupId>org.springframework.boot</groupId>
    3     <artifactId>spring-boot-starter-parent</artifactId>
    4     <version>2.1.1.RELEASE</version>
    5 </parent>
    

    我们可以在 Maven central 中找到 spring-boot-starter-parent 的最新版本。

    使用 starter 父项目依赖很方便,但并非总是可行。例如,如果我们公司都要求项目继承标准 POM,我们就不能依赖 SpringBoot starter 了。

    这种情况,我们可以通过对 POM 元素的依赖管理来处理:

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

     

    最后,我们可以添加 SpringBoot starter 中一些依赖,然后我们就可以开始了。

     

知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看