在官方文檔的第三部分的13塊講述了引用的管理,官方推薦的是使用Maven和Gradle。
我一直在用的是maven,而且使用maven有些優勢–spring-boot-starter-parent,這個部件是maven獨有的。
這次我們從這里開始學習。
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把默認的占位符${…}改為了@..@(這點大家還是看下原文自己理解下吧,我個人用的也比較少
since the default config files accept Spring style placeholders (${…}) the Maven filtering is changed to use @..@ 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中的一些引用不想使用默認的版本,可以直接加上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之后。
————————————————
版權聲明:本文為CSDN博主「月未明」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_35981283/article/details/77802771