spring-boot-starter-parent_SpringBoot入门篇(2)springbootstarterparent分析

点击上方 蓝字可以订阅哦

上一章SpringBoot简介与HelloWorld我们通过简单的HelloWorld工程运行起来了第一个SpringBoot项目,并且做了一些简单的介绍。这次我们详细说一下项目的父工程spring-boot-starter-parent,详细见下面的代码

  <parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.2.RELEASEversion>
parent>

1、spring-boot-starter-parent的「parent」节点

  <parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>2.2.2.RELEASEversion>
<relativePath>../../spring-boot-dependenciesrelativePath>
parent>

spring-boot-dependencies这个组件又是什么作用呢?

1.1、 spring-boot-dependencies的properties节点

我们看定义POM,这个才是SpringBoot项目的真正管理依赖的项目,里面定义了SpringBoot相关的版本

<properties>
<activemq.version>5.15.11activemq.version>
<antlr2.version>2.7.7antlr2.version>
<appengine-sdk.version>1.9.77appengine-sdk.version>
<artemis.version>2.10.1artemis.version>
<aspectj.version>1.9.5aspectj.version>
<assertj.version>3.13.2assertj.version>
<atomikos.version>4.0.6atomikos.version>
<awaitility.version>4.0.1awaitility.version>
<bitronix.version>2.1.4bitronix.version>
<build-helper-maven-plugin.version>3.0.0build-helper-maven-plugin.version>
<byte-buddy.version>1.10.4byte-buddy.version>
<caffeine.version>2.8.0caffeine.version>
<cassandra-driver.version>3.7.2cassandra-driver.version>
<classmate.version>1.5.1classmate.version>
<commons-codec.version>1.13commons-codec.version>
<commons-dbcp2.version>2.7.0commons-dbcp2.version>
<commons-lang3.version>3.9commons-lang3.version>
<commons-pool.version>1.6commons-pool.version>
<commons-pool2.version>2.7.0commons-pool2.version>
<couchbase-cache-client.version>2.1.0couchbase-cache-client.version>
<couchbase-client.version>2.7.11couchbase-client.version>
<db2-jdbc.version>11.5.0.0db2-jdbc.version>
<dependency-management-plugin.version>1.0.8.RELEASEdependency-management-plugin.version>
等等。。。。。。
properties>
1.2、spring-boot-dependencies的dependencyManagement节点

在这里,dependencies定义了SpringBoot版本的依赖的组件以及相应版本。

  <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-bootartifactId>
<version>2.2.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-testartifactId>
<version>2.2.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-test-autoconfigureartifactId>
<version>2.2.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-actuatorartifactId>
<version>2.2.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-actuator-autoconfigureartifactId>
<version>2.2.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-autoconfigureartifactId>
<version>2.2.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-autoconfigure-processorartifactId>
<version>2.2.2.RELEASEversion>
dependency>
等等。。。。。。
dependencies>
dependencyManagement>

spring-boot-starter-parent通过继承spring-boot-dependencies从而实现了SpringBoot的版本依赖管理。

2、spring-boot-starter-parent的「properties」节点

接下来我们回过头,继续看spring-boot-starter-parentproperties节点,在这里spring-boot-starter-parent定义了

  1. 工程的UTF-8编码。

  2. 工程的Java版本为1.8

  3. Maven打包编译的版本

  4. 工程代码的编译源文件编码格式为UTF-8

  5. 工程编译后的文件编码格式为UTF-8

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

3、spring-boot-starter-parent的「build」节点

接下来看POM的build节点,分别定义了resources资源和pluginManagement

    <resources>
<resource>
<filtering>truefiltering>
<directory>${basedir}/src/main/resourcesdirectory>
<includes>
<include>**/application*.ymlinclude>
<include>**/application*.yamlinclude>
<include>**/application*.propertiesinclude>
includes>
resource>
<resource>
<directory>${basedir}/src/main/resourcesdirectory>
<excludes>
<exclude>**/application*.ymlexclude>
<exclude>**/application*.yamlexclude>
<exclude>**/application*.propertiesexclude>
excludes>
resource>
resources>

我们详细看一下resources节点,里面定义了资源过滤,针对applicationymlproperties格式进行了过滤,可以支持支持不同环境的配置,比如application-dev.ymlapplication-test.ymlapplication-dev.propertiesapplication-dev.properties等等。

pluginManagement则是引入了相应的插件和对应的版本依赖,留下一个小作业,您可以自己打开对应的POM文件,看一下里面究竟有哪些插件。

4、不使用spring-boot-starter-parent启用SpringBoot项目

到此为止,我们看了spring-boot-starter-parent。现在有一个问题,如果我们不通过parent的方式引入SpringBoot,因为有时候公司的架构以及有了对应的其他parent,我们怎么引入SpringBoot呢。可以通过dependencyManagement的方式引入spring-boot-dependencies,引入方式见下面:

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>2.2.2.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>

这样我们的包的依赖问题就解决了,但是还有一些细节需要处理了,就是parent项目中的propertiesbuild节点的内容,需要大家结合项目进行补充和定义。这样就解决了在旧项目中引入SpringBoot·的问题

总结

好了,一篇文章给大家简单展示了SpringBoot项目的spring-boot-starter-parent的分析,有问题欢迎留言讨论。

文章推荐 SpringBoot简介与HelloWorld Java进阶必备——互联网后端全套基础设施 【收藏必备】JVM 完整深入解析(12000字噢) 69 个经典 Spring 面试题(含答案) 史上最全的Java日志框架(15000字) 面试时你应该这样介绍自己的项目经验?

ab052cec8649bebeb27ac18f97754917.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值