Spring Boot —— 版本自動管理
阿新 • • 發佈:2018-12-29
(spring-boot-starter-parent)
Maven的使用者可以通過繼承spring-boot-starter-parent專案來獲得一些合理的預設配置。這個parent提供了以下特性:
- 預設使用Java 8
- 使用UTF-8編碼
- 一個引用管理的功能,在dependencies裡的部分配置可以不用填寫version資訊,這些version資訊會從spring-boot-dependencies裡得到繼承。
- 識別過來資源過濾(Sensible resource filtering.)
- 識別外掛的配置(Sensible plugin configuration (exec plugin, surefire, Git commit ID, shade).)
- 能夠識別application.properties和application.yml型別的檔案,同時也能支援profile-specific型別的檔案(如: application-foo.properties and application-foo.yml,這個功能可以更好的配置不同生產環境下的配置檔案)。
- maven把預設的佔位符${…}改為了@[email protected](這點大家還是看下原文自己理解下吧,我個人用的也比較少
since the default config files accept Spring style placeholders (${…}) the Maven filtering is changed to use @ [email protected] placeholders (you can override that with a Maven property resource.delimiter).)
spring-boot-starter-parent的引用
<!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.BUILD-SNAPSHOT</version> </parent>
位置一般都是放在dependencies之前。
如果dependencies中的一些引用不想使用預設的版本,可以直接加上version資訊,把預設的覆蓋掉。
另外官方提供的覆蓋預設配置的方式如下:
<properties>
<spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>
在properties中註明某個引用要使用的版本。具體使用哪種方式還是看個人習慣。
如果不想使用spring-boot-starter-parent,也可以自己來配置所要使用的版本:
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</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>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
這裡的引用宣告是藉助scope=import 來實現的。
如果想要把專案打包成一個可執行的jar包,需要新增maven的一下元件:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
位置一般都是放在dependencies之後。