Administrator
发布于 2024-09-03 / 1 阅读
0
0

SpringBoot相关的parent和dependency

目前,主要有以下几种:

  • spring-boot-starter-parent
  • spring-boot-dependencies
  • spring-boot-starter

spring-boot-starter-parent vs spring-boot-dependencies

1.1 简介

spring-boot-starter-parent 是Spring Boot项目的Maven父项目(POM),它提供了一系列默认的配置和依赖管理,用于简化Spring Boot应用程序的构建和管理。

1.2 特点

  • 提供了一组默认的插件配置,如编译插件、打包插件等,以及一些常用的属性设置。
  • 继承自Spring Boot的“Bill of Materials”(BOM),用于统一管理Spring Boot相关依赖的版本。
  • 配置了默认的编码格式、Java版本等。
  • 继承了Spring Boot的父项目后,无需显式指定Spring Boot的版本,因为它已经被固定在了spring-boot-starter-parent中。

2. spring-boot-dependencies

2.1 简介

spring-boot-dependencies 是Spring Boot提供的另一个重要的Maven项目,用于集中管理所有Spring Boot相关依赖的版本信息。

2.2 特点

  • 包含了Spring Boot中所有的starter依赖的版本信息,包括Spring Framework、Spring Boot自身以及其他常用的第三方库。
  • 使用了部分来管理依赖的版本信息,但是不会直接引入依赖。
  • 提供了一种简化依赖声明的方式,只需要声明spring-boot-starter-*相关的依赖,而无需指定版本号,因为版本号已经被spring-boot-dependencies管理了。

3. 异同点对比

3.1 相同点

  • 都是Spring Boot提供的Maven父项目。
  • 都用于简化Spring Boot项目的构建和依赖管理。
  • 都包含了一些默认的配置和依赖版本管理。

3.2 不同点

  • spring-boot-starter-parent 主要用于配置项目的基本设置,如插件配置、默认属性等,而 spring-boot-dependencies 则主要用于集中管理Spring Boot相关依赖的版本信息。

  • spring-boot-starter-parent 中会继承 spring-boot-dependencies 中定义的依赖版本信息,从而不需要在pom.xml中显式声明依赖的版本。

  • spring-boot-starter-parent 可以作为Maven父项目,被项目继承,而 spring-boot-dependencies 只需要在项目的 dependencyManagement 中引入即可。


样例分析

spring-boot-starter-parent 本身是一个pom,他的parent是spring-boot-dependencies。
我们来看下spring-boot-starter-parent中,包含了哪些东西:

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

上面定义了parent是谁


定义了一些属性:

 <properties>
    <java.version>17</java.version>
    <resource.delimiter>@</resource.delimiter>
    <maven.compiler.release>${java.version}</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <spring-boot.run.main-class>${start-class}</spring-boot.run.main-class>
  </properties>    

定义了resource中占位符替换规则

<resources>
      <resource>
        <directory>${basedir}/src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
          <include>**/application*.yml</include>
          <include>**/application*.yaml</include>
          <include>**/application*.properties</include>
        </includes>
      </resource>
      <resource>
        <directory>${basedir}/src/main/resources</directory>
        <excludes>
          <exclude>**/application*.yml</exclude>
          <exclude>**/application*.yaml</exclude>
          <exclude>**/application*.properties</exclude>
        </excludes>
      </resource>
    </resources> 

定义了pluginManagement

<pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
            <parameters>true</parameters>
          </configuration>
        </plugin>    
          
         <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <configuration>
            <archive>
              <manifest>
                <mainClass>${start-class}</mainClass>
                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
              </manifest>
            </archive>
          </configuration>
        </plugin>
          
         <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-resources-plugin</artifactId>
          <configuration>
            <propertiesEncoding>${project.build.sourceEncoding}</propertiesEncoding>
            <delimiters>
              <delimiter>${resource.delimiter}</delimiter>
            </delimiters>
            <useDefaultDelimiters>false</useDefaultDelimiters>
          </configuration>
        </plugin>
          
        
         <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <executions>
            <execution>
              <id>repackage</id>
              <goals>
                <goal>repackage</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <mainClass>${spring-boot.run.main-class}</mainClass>
          </configuration>
        </plugin>

spring-boot-starter

我们能看到很多starter包,比如spring-boot-starter-jdbc、spring-boot-starter-security、spring-boot-starter-data-redis等。

而这些各种功能的starter包,都需要依赖最基础的包,即 spring-boot-starter包 。
也就是说,spring-boot-starter是最小的基础包,他定义了springboot的版本、spring-boot-autoconfigure、jakarta.annotation-api、spring-core等


详细如下:

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>3.3.3</version>
<name>spring-boot-starter</name>    
          
 <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot</artifactId>
      <version>3.3.3</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
      <version>3.3.3</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-logging</artifactId>
      <version>3.3.3</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>jakarta.annotation</groupId>
      <artifactId>jakarta.annotation-api</artifactId>
      <version>2.1.1</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>6.1.12</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.yaml</groupId>
      <artifactId>snakeyaml</artifactId>
      <version>2.2</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

评论