文章目录
【Spring Boot】022-不使用spring-boot-starter-parent
一、spring-boot-starter-parent 概述
1、pom.xml 中默认生成的写法
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
2、spring-boot-starter-parent 是什么
简介
是 Spring Boot 的父级依赖,加上此配置之后,当前的项目就是 Spring Boot 项目了。spring-boot-starter-parent 是一个特殊的 starter,它用来提供相关的 Maven 默认依赖。
我们的项目的父级依赖是 spring-boot-starter-parent , spring-boot-starter-parent 的父级依赖是 spring-boot-dependencies 。
提供的默认配置
spring-boot-starter-parent 中的默认配置可以通过覆盖的方式修改!
- Java版本默认使用 1.8;
- 编码格式默认使用 UTF-8;
- 提供 Dependency Management 进行项目依赖的版本管理;
- 默认的资源过滤和插件配置;
修改默认配置
修改 Java 版本
<properties>
<java.version>1.8</java.version>
</properties>
修改编码方式
<properties>
<project.build.sourceEncoding>GBK</project.build.sourceEncoding>
</properties>
添加依赖省略版本信息
添加依赖的一般写法
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.5.3</version>
</dependency>
使用 spring-boot-starter-parent 的写法
会自动继承 spring-boot-starter-parent 中的写法!
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
二、不使用 spring-boot-starter-parent
自定义项目的父级依赖!
1、自定义 parent ,实现项目项目版本的统一管理
删除 pom.xml 中的代码
不再使用父级依赖 spring-boot-starter-parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
在 pom.xml 中添加 dependencyManagement
<dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<!-- 此处需要带上版本信息 -->
<version>2.5.3</version>
</dependency>
</dependencies>
</dependencyManagement>
在 properties 中定义版本信息并引用
定义版本信息
<properties>
<jpa.version>2.5.3</jpa.version>
</properties>
引用版本信息
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<!-- 此处需要引用版本信息 -->
<version>${jpa.version}</version>
</dependency>
2、手动配置 Java 版本、编码格式等
Java 版本
直接仿写 spring-boot-starter-parent 默认配置!
<!-- 编译时指定jdk版本-->
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
编码格式
创建 Spring Boot 项目的时候默认自带,如果普通的 Maven 项目配置成 Spring Boot 项目,则加入以下配置!直接仿写 spring-boot-starter-parent 默认配置即可!
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>