五、建立 Spring Boot 项目时,当父依赖不再是 spring-boot-starter-parent 怎么办

编程知识 更新时间:2023-04-05 04:46:20

(一)问题
  现阶段建立 Spring Boot 项目,使用 IDEA 自动创建项目时,会导入如下父依赖:

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

  但在公司时,我们可能会需要自己公司内部的父依赖,那么就不能再依赖 spring-boot-starter-parent。但是若我们直接删除这个父依赖,就会报错(PS:主要是版本错误),遇到这种情况如何解决?

(二)解决办法
1、删除 spring-boot-starter-parent 依赖

2、在 dependencyManagement 标签下,添加 spring-boot-dependencies 依赖,并且版本保持和原 spring-boot-starter-parent 一致

<dependencyManagement>
		<dependencies>
			<!--
			    使用场景:当父依赖是公司内部依赖时(PS:不是spring-boot-starter-parent),需要
			这样做。
			 -->
			<!-- Spring Boot 依赖 -->
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-dependencies</artifactId>
				<version>2.0.6.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>

			<!-- Spring Cloud 依赖 -->
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

3、此时编译若报错,一般是编译插件版本问题,我们需要去 spring-boot-starter-parent 找到 编译插件,同时版本也拿过来

<!-- Maven Java 编译器插件 -->
			<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
<!-- Maven Java 编译器插件 -->
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>${maven-compiler-plugin.version}</version>
			</plugin>

4、设置 编译源 和 编译后 Java 版本

		<java.version>1.8</java.version>
		<mavenpiler.source>${java.version}</mavenpiler.source>
		<mavenpiler.target>${java.version}</mavenpiler.target>

更多推荐

五、建立 Spring Boot 项目时,当父依赖不再是 spring-boot-starter-parent 怎么办

本文发布于:2023-04-05 04:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/33ab292b09506a7074c5c4e2ce1a3007.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:项目   Boot   Spring   spring   parent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 44846文章数
  • 14阅读数
  • 0评论数