Spring Boot (二):關於Spring Boot的pom 配置。spring-boot-starter-parent.pom
關於依賴
簡介
Spring Boot 自帶了一個它能夠支援的依賴表,在用的時候不需要提供這些依賴的版本資訊,Spring Boot會幫你管理好,更新Spring Boot的時候,依賴也會跟著更新,從而保持一致。
這個依賴list包含了Spring Boot能夠包含的所有module模組,每一個發行版Spring Boot都對應著一個基礎版本的Spring框架,不建議去定義版本號。
spring-boot-starter-parent.pom
文件說這個parent.pom檔案裡面包含了
- JAVA 1.8預設編譯level
- UTF-8的編碼
- 一個繼承自spring-boot-dependencies的依賴管理部分,來管理各種依賴,這些依賴可以在你自己的pom檔案裡面省略標籤,不用寫版本號了。
- An execution of the repackage goal with a repackage execution id.
- Sensible resource filtering.
- Sensible plugin configuration
- Sensible resource filtering for application.properties and application.yml including profile-specific files (for example, application-dev.properties and application-dev.yml)
- 也就是說各種檔案會幫你打包好。讓我們一探parent 配置檔案。
- control + 左鍵就可以進入。
- 首先最上面有一個parent父配置檔案,用的是相對路徑。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath>../../spring-boot-dependencies</ relativePath>
</parent>
- 接下來是一些定義的java版本號,編碼
<properties>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<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>
- 之後就是外掛等,就不看了,去看看它的父pom。進入父pom的第一眼就是各種依賴,有很多。版本號也均寫好了。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test-autoconfigure</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator-autoconfigure</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
看完了就驗證了文件所說的那個樣子一共是兩個pom,一個負責檔案的打包,外掛,版本號等,一個負責管理各種各樣的依賴,(最下面也有一些外掛)分工明確。
覆蓋並使用使用自己的依賴版本
1.在使用parent的情況下:
在使用spring-boot-starter-parent.pom的時候需要指明版本號,當使用其它starter的時候可以省略版本號。你可以通過覆蓋properties標籤的property標籤來達到修改依賴版本號的目的,但是!請看下面:在依賴pom裡面確實定義了很多version來供下面使用,覆蓋的確可以更改。往下翻就知道Spring Boot大家族的版本號是一致的,不是properties標籤中定義的,文件也說不要去修改這些。
properties屬性
<properties>
<activemq.version>5.15.7</activemq.version>
<antlr2.version>2.7.7</antlr2.version>
<appengine-sdk.version>1.9.67</appengine-sdk.version>
<artemis.version>2.6.3</artemis.version>
<aspectj.version>1.9.2</aspectj.version>
<assertj.version>3.11.1</assertj.version>
<atomikos.version>4.0.6</atomikos.version>
<bitronix.version>2.1.4</bitronix.version>
<build-helper-maven-plugin.version>3.0.0</build-helper-maven-plugin.version>
<byte-buddy.version>1.9.3</byte-buddy.version>
<caffeine.version>2.6.2</caffeine.version>
<cassandra-driver.version>3.6.0</cassandra-driver.version>
<classmate.version>1.4.0</classmate.version>
<commons-codec.version>1.11</commons-codec.version>
<commons-dbcp2.version>2.5.0</commons-dbcp2.version>
<commons-lang3.version>3.8.1</commons-lang3.version>
<commons-pool.version>1.6</commons-pool.version>
<commons-pool2.version>2.6.0</commons-pool2.version>
已經寫好的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test-autoconfigure</artifactId>
<version>2.1.0.RELEASE</version>
2.不繼承parent如何引入自己的父級依賴並且覆蓋相應的版本號?
如果你公司有自己的父級模組,這個時候就不能通過parent來繼承這個spring-boot-starter-parent pom了。you can still keep the benefit of the dependency management (but not the plugin management) by using a scope=import dependency。
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
如果需要覆蓋這樣即可:
<dependencyManagement>
<dependencies>
<!-- Override Spring Data release train provided by Spring Boot -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Fowler-SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>