一文学会 maven集成 springboot 依赖

1、maven集成springboot的方式

maven 集成 springboot 主要使用两种方式:1)、通过继承的方式,2)、通过导入依赖的方式

2、通过继承的方式
<!-- 在 pom 文件中引入以下依赖 -->
<parent>
    <artifactId>spring-boot-starter-parent</artifactId>
    <groupId>org.springframework.boot</groupId>
    <version>${spring-boot.version}</version>
</parent>
3、通过导入 spring-boot-dependencies 依赖的方式
<!-- 在 pom 文件中引入以下依赖 -->
<dependencyManagement>
    <dependencies>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-dependencies</artifactId>
           <version>${spring-boot.version}</version>
       </dependency>
    </dependencies>
</dependencyManagement>

两种方式的差别在于,spring-boot-starter-parent 也依赖于 spring-boot-dependencies ,spring-boot-starter-parent 的 pom文件如下:

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-dependencies</artifactId>
   <version>2.7.5</version>
</parent>
<artifactId>spring-boot-starter-parent</artifactId>
<packaging>pom</packaging>
<name>spring-boot-starter-parent</name>

spring-boot-starter-parent 继承了 spring-boot-dependencies 定义了一些常用的项目相关的配置,比如: properties(参数),plugins(插件)等。

<properties>
  <java.version>1.8</java.version>
  <resource.delimiter>@</resource.delimiter>
  <maven.compiler.source>${java.version}</maven.compiler.source>
  <maven.compiler.target>${java.version}</maven.compiler.target>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<build>
 <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.jetbrains.kotlin</groupId>
          <artifactId>kotlin-maven-plugin</artifactId>
          <version>${kotlin.version}</version>
          <configuration>
            <jvmTarget>${java.version}</jvmTarget>
            <javaParameters>true</javaParameters>
          </configuration>
          <executions>
            <execution>
              <id>compile</id>
              <phase>compile</phase>
              <goals>
                <goal>compile</goal>
              </goals>
            </execution>
            <execution>
              <id>test-compile</id>
              <phase>test-compile</phase>
              <goals>
                <goal>test-compile</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
            <parameters>true</parameters>
          </configuration>
        </plugin>     
      </plugins>
    </pluginManagement>
</build>
4、两种方式对比

1)、通过继承方式,插件的导入只需要引入,直接定义坐标即可;如果使用 spring-boot-dependencies 集成的方式,需要额外配置该参数。

2)、通过继承方式,在导入依赖的时候,可以直接定义 properties 参数覆盖依赖的版本;如果使用 spring-boot-dependencies 集成的方式,如果需要换版本,需要显式导入依赖并指定版本号。

注:继承缺点是不利于扩展,,一旦继承了 springboot 的父项目,就无法继承其他父项目了;在设计原则上,推荐使用导入依赖的方式。具体使用哪种看项目需求。

        本人是一个从小白自学计算机技术,对运维、后端、各种中间件技术、大数据等有一定的学习心得,想获取自学总结资料(pdf版本)或者希望共同学习,关注微信公众号:it自学社团。后台回复相应技术名称/技术点即可获得。(本人学习宗旨:学会了就要免费分享)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值