Loading

Spring Boot Starter Parent

每次使用Spring Initialize 初始化项目的时候pom.xml 发现都会多一项parent 声明

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

那spring-boot-starter-parent 到底是干什么的呢?

一、 Spring Boot Starter Parent

spring-boot-starter-parent 是一个特殊的starter 项目,它为我们提供了项目默认配置和完整的依赖树,以便快速建立一个spring boot 项目

同时也为maven 插件提供了默认配置,如:maven-compiler-plugin, maven-failsafe-plugin, maven-jar-plugin, maven-surefire-plugin, maven-war-plugin

二、 项目功能

spring-boot-starter-parent 提供了以下功能:

  1. Java 1.8 作为默认的编译版本

  2. 源码使用UTF-8 格式编码

  3. 继承自spring-boot-dependencies,这里管理了常见依赖的版本。可以让我们在项目的pom.xml中省略这些依赖的标签。

    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    
  4. 自动化的资源过滤:默认把src/main/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>
    
  5. 自动化的插件管理:默认为maven 插件提供了默认配置,如:maven-compile-plugin, maven-war-plugin

  6. 自动对application.properties和application.yml进行资源过滤,包括通过profile 定义的不同环境的配置(如:application-dev.properties和application-dev.yml)

注意:由于application.properties和application.yml文件接受Spring样式占位符 $ {...} ,因此 Maven 过滤更改为使用 @ .. @ 占位符,当然开发者可以通过设置名为 resource.delimiter 的Maven 属性来覆盖 @ .. @ 占位符。

三、配置覆盖

我们可以用properties 标签来重写spring-boot-starter-parent 中定义的版本信息

例如,如果想用Java 11 以及不同版本的SLF4J 可以在pom.xml 中添加以下内容

<properties>
    <java.version>11</java.version>
    <slf4j.version>1.7.30</slf4j.version>
</properties>

所有的版本依赖列表:Dependency versions Appendix

同样也可以指定依赖的version 进行版本覆盖:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
    <scope>test</scope>
</dependency>

四、不依赖Parent 的项目

如果因为种种原因不能使用spring-boot-starter-parent,但是仍然可以通过自定义dependencyManagement 来获取spring boot 的依赖树

<dependencyManagement>
    <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.6.7</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

这样我们仍然可以像之前一样,只在dependencies 中声明依赖,而不要申明version 信息。但是其他的,比如:打包插件、编译的Java 版本、文件的编码格式等就需要自己去配置了。

五、原理

在本地仓库找到spring-boot-starter-parent-2.6.7.pom 文件,内容大致如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.6.7</version>
  </parent>
  <artifactId>spring-boot-starter-parent</artifactId>
  <packaging>pom</packaging>
  <name>spring-boot-starter-parent</name>
  <description>Parent pom providing dependency and plugin management for applications built with Maven</description>
  <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>
  <url>https://spring.io/projects/spring-boot</url>
  <licenses>
    <license>
      <name>Apache License, Version 2.0</name>
      <url>https://www.apache.org/licenses/LICENSE-2.0</url>
    </license>
  </licenses>
  <developers>
    <developer>
      <name>Pivotal</name>
      <email>info@pivotal.io</email>
      <organization>Pivotal Software, Inc.</organization>
      <organizationUrl>https://www.spring.io</organizationUrl>
    </developer>
  </developers>
  <scm>
    <url>https://github.com/spring-projects/spring-boot</url>
  </scm>
  <build>
    <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>
      <plugins>
        ...
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
            <parameters>true</parameters>
          </configuration>
        </plugin>
		...
      </plugins>
    </pluginManagement>
  </build>
</project>

可以看到插件的配置、资源的声明以及Java 版本、编码格式等信息

spring-boot-starter-parent 同时又继承自spring-boot-dependencies.pom,spring-boot-dependencies-2.6.7.pom 文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-dependencies</artifactId>
  <version>2.6.7</version>
  <packaging>pom</packaging>
  <name>spring-boot-dependencies</name>
  <description>Spring Boot Dependencies</description>
  <url>https://spring.io/projects/spring-boot</url>
  <licenses>
    <license>
      <name>Apache License, Version 2.0</name>
      <url>https://www.apache.org/licenses/LICENSE-2.0</url>
    </license>
  </licenses>
  <developers>
    <developer>
      <name>Pivotal</name>
      <email>info@pivotal.io</email>
      <organization>Pivotal Software, Inc.</organization>
      <organizationUrl>https://www.spring.io</organizationUrl>
    </developer>
  </developers>
  <scm>
    <url>https://github.com/spring-projects/spring-boot</url>
  </scm>
  <properties>
    <activemq.version>5.16.4</activemq.version>
    ...
  </properties>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-amqp</artifactId>
        <version>${activemq.version}</version>
      </dependency>
      <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-blueprint</artifactId>
        <version>${activemq.version}</version>
      </dependency>
      <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-broker</artifactId>
        <version>${activemq.version}</version>
      </dependency>
      ...
    </dependencies>
  </dependencyManagement>
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>${maven-compiler-plugin.version}</version>
        </plugin>
        ...
      </plugins>
    </pluginManagement>
  </build>
</project>

在这里可以看到所有依赖的版本定义,以及dependencyManagement、pluginManagement 节点,这就是项目依赖可以省略version 的原因

六、参考文档

posted @ 2022-05-09 10:58  Nowin  阅读(2878)  评论(0编辑  收藏  举报