目錄
1、項目pom文件依賴,父級項目spring-boot-starter-parent
2、再看spring-boot-starter-parent
3、spring-boot-dependencies-2.2.2.RELEASE.pom
使用簡單的Springboot生成項目很簡單,簡單歸簡單,那他究竟默認都幫我們引入了哪些包。pom文件的依賴關係又是怎樣的。
讓我們一探究竟。
1、項目pom文件依賴,父級項目spring-boot-starter-parent
以一個簡單的springboot工程來查看其pom文件,可以看出父級項目是spring-boot-starter-parent
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--父級項目 與聚合module方便把幾個項目合在一起不同 父級項目是爲了減少差異-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、再看spring-boot-starter-parent
點擊進spring-boot-starter-parent再看其pom文件,spring-boot-starter-parent-2.2.2.RELEASE.pom ,註釋說明了該文件的一些注意點。
<?xml version="1.0" encoding="utf-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<!-- 定義查找順序,先從relativePath元素地址值查找,再到本地查找,再到遠程倉庫查找 -->
<relativePath>../../spring-boot-dependencies</relativePath>
</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>
<url>https://projects.spring.io/spring-boot/#/spring-boot-starter-parent</url>
<properties>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- @會獲取其他配置文件 以@開頭的值 填充到application.properties中-->
<resource.delimiter>@</resource.delimiter>
<maven.compiler.source>${java.version}</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<!-- resource 可以使用變量 ${}則可以引入,與resource.delimiter對應
filtering爲true的將會對對應的資源文件使用屬性替換,將application*.properties${}的地方通過@前綴修飾的值進行替換
-->
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory>
<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.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
3、spring-boot-dependencies-2.2.2.RELEASE.pom
根據spring-boot-starter-parent-2.2.2.RELEASE.pom 的父級項目spring-boot-dependencies再繼續分析,下面的文件省去原有大量的properties和dependency
<?xml version="1.0" encoding="utf-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<packaging>pom</packaging>
<name>Spring Boot Dependencies</name>
<description>Spring Boot Dependencies</description>
<url>https://projects.spring.io/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>[email protected]</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>
<!-- 常量定義區 在pom文件中可以直接引用 版本編碼等-->
<properties>
<activemq.version>5.15.11</activemq.version>
<!---->
</properties>
<!-- 管理依賴版本號 與dependency不同的是,dependency是子項目直接包含的依賴,而對於繼承的所有子類項目,dependencyManagement可以加固和集中管理
特別是我們有一系列的工程的時候,我們具體聲明瞭版本號和scope在父類項目中後,對於子類項目有統一的管理,而不需要在每個子模塊都聲明版本號
-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<!-- plugin,maven本身其實就是一個插件執行框架,每個任務包括編譯等的執行其實都是通過插件執行的,pluginManagement和plugin的關係
dependencyManagement和dependency的關係類似
-->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.johnzon</groupId>
<artifactId>johnzon-maven-plugin</artifactId>
<version>${johnzon.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
4、總結
總結一下,繼承 spring-boot-starter-parent 來作爲父項目, spring-boot-starter-parent的父項目又是Spring Boot Dependencies,
已經定義好了很多包的依賴,可以統一項目的依賴,避免後續的版本衝突。
Maven users can inherit from the spring-boot-starter-parent project to obtain sensible defaults. The parent project provides the following features:
Java 1.8 as the default compiler level.
UTF-8 source encoding.
A Dependency Management section, inherited from the spring-boot-dependencies pom, that manages the versions of common dependencies. This dependency management lets you omit <version> tags for those dependencies when used in your own pom.
An execution of the repackage goal with a repackage execution id.
Sensible resource filtering.
Sensible plugin configuration (exec plugin, Git commit ID, and shade).
Sensible resource filtering for application.properties and application.yml including profile-specific files (for example, application-dev.properties and application-dev.yml)
Note that, since the application.properties and application.yml files accept Spring style placeholders (${…}), the Maven filtering is changed to use @..@ placeholders. (You can override that by setting a Maven property called resource.delimiter.)
Java
默認使用Java 8
使用UTF-8編碼
一個引用管理的功能,在dependencies裏的部分配置可以不用填寫version信息,這些version信息會從spring-boot-dependencies裏得到繼承。
識別資源過濾
識別插件的配置(exec plugin, surefire, Git commit ID, shade).能夠識別application.properties和application.yml類型的文件,同時也能支持profile-specific類型的文件(如: application-foo.properties and application-foo.yml,這個功能可以更好的配置不同生產環境下的配置文件)。
摘自官方文檔https://docs.spring.io/spring-boot/docs/1.3.0.M5/reference/html/using-boot-build-systems.html