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自学社团。后台回复相应技术名称/技术点即可获得。(本人学习宗旨:学会了就要免费分享)