spring-boot-starter-parent 作用
一、你的項(xiàng)目 pom.xml 中有這段代碼嗎
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.5.RELEASE</version><relativePath/> </parent>new project > spring initializr 創(chuàng)建一個(gè) Spring Boot 項(xiàng)目時(shí):
或者是干脆從 https://start.spring.io/ 在線生成 Spring Boot 項(xiàng)目:
我們都會(huì)發(fā)現(xiàn)spring-boot-starter-parent的身影,那么他到底是干啥的呢
這是 Spring Boot 的父級(jí)依賴(lài),這樣當(dāng)前的項(xiàng)目就是 Spring Boot 項(xiàng)目了。spring-boot-starter-parent 是一個(gè)特殊的 starter ,它用來(lái)提供相關(guān)的 Maven 默認(rèn)依賴(lài)。
使用它之后,常用的包依賴(lài)可以省去 version 標(biāo)簽,當(dāng)我們搭建web應(yīng)用的時(shí)候,可以像下面這樣添加spring-boot-starter-web依賴(lài):
<!-- Quartz定時(shí)任務(wù) --> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-quartz</artifactId> </dependency>二、這個(gè)parent還提供了哪些特性呢
注意,關(guān)于 parent 提供的一些特性都是依據(jù) maven 環(huán)境的,可以說(shuō)
spring-boot-starter-parent 是 maven 獨(dú)有的,如下是提供的一些特性:
在 dependencies 里的部分配置可以不用填寫(xiě) version 信息,這些 version 信息會(huì)從 spring-boot-dependencies 里得到繼承。
使用 spring-boot-starter-parent 的話,可以這樣,繼承默認(rèn)版本:
<!-- JPA --> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>正常來(lái)說(shuō)項(xiàng)目應(yīng)該添加了以下帶版本的依賴(lài):
4. 識(shí)別過(guò)來(lái)資源過(guò)濾
例如,打包的時(shí)候把 src/main/resources 下所有文件都打包到包中。
比如 exec plugin, surefire, Git commit ID, shade
能夠識(shí)別 application.properties 和 application.yml 類(lèi)型的文件,同時(shí)也能支持 profile-specific 類(lèi)型的文件(如: application-foo.properties and application-foo.yml,這個(gè)功能可以更好的配置不同生產(chǎn)環(huán)境下的配置文件)。
三、覆蓋并使用使用自己的依賴(lài)版本
使用 spring-boot-starter-parent 來(lái)幫我們管理 version 確實(shí)方便了不少,但是往往我們需要自己去配置某些版本,怎么辦呢?
我們可以通過(guò)覆蓋 properties 標(biāo)簽的 property 標(biāo)簽來(lái)達(dá)到修改依賴(lài)版本號(hào)的目的,例如上方的修改默認(rèn)的編碼方式、以及默認(rèn)jdk版本:
<properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>為什么這樣可以實(shí)現(xiàn)?
我們了解 Spring Boot Dependencies 定義了所有 Spring Boot 項(xiàng)目的默認(rèn)依賴(lài)關(guān)系管理。如果我們想要使用特定依賴(lài)項(xiàng)的新版本,我們可以通過(guò)在項(xiàng)目 pom 中指定新屬性來(lái)覆蓋該版本。
下面的摘錄顯示了由 Spring Boot Dependencies 父 pom 管理的一些重要依賴(lài)項(xiàng)。由于 Spring Boot Starter Parent 繼承自 spring-boot-dependencies,因此它也共享所有這些特性。
<properties><activemq.version>5.13.4</activemq.version>...<ehcache.version>2.10.2.2.21</ehcache.version><ehcache3.version>3.1.1</ehcache3.version>...<h2.version>1.4.192</h2.version><hamcrest.version>1.3</hamcrest.version><hazelcast.version>3.6.4</hazelcast.version><hibernate.version>5.0.9.Final</hibernate.version><hibernate-validator.version>5.2.4.Final</hibernate-validator.version><hikaricp.version>2.4.7</hikaricp.version><hikaricp-java6.version>2.3.13</hikaricp-java6.version><hornetq.version>2.4.7.Final</hornetq.version><hsqldb.version>2.3.3</hsqldb.version><htmlunit.version>2.21</htmlunit.version><httpasyncclient.version>4.1.2</httpasyncclient.version><httpclient.version>4.5.2</httpclient.version><httpcore.version>4.4.5</httpcore.version><infinispan.version>8.2.2.Final</infinispan.version><jackson.version>2.8.1</jackson.version>....<jersey.version>2.23.1</jersey.version><jest.version>2.0.3</jest.version><jetty.version>9.3.11.v20160721</jetty.version><jetty-jsp.version>2.2.0.v201112011158</jetty-jsp.version><spring-security.version>4.1.1.RELEASE</spring-security.version><tomcat.version>8.5.4</tomcat.version><undertow.version>1.3.23.Final</undertow.version><velocity.version>1.7</velocity.version><velocity-tools.version>2.0</velocity-tools.version><webjars-hal-browser.version>9f96c74</webjars-hal-browser.version><webjars-locator.version>0.32</webjars-locator.version><wsdl4j.version>1.6.3</wsdl4j.version><xml-apis.version>1.4.01</xml-apis.version> </properties>所以,我們更改了 property 后,也實(shí)現(xiàn)了我們的需求,當(dāng)然我們還有直接注明 version 的方式呢,如下:
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version> </dependency>四、一個(gè)完整的pom.xml實(shí)例
<?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>club.sscai.tmax</groupId><artifactId>tmax</artifactId><version>0.0.1-SNAPSHOT</version><name>tmax</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.5.RELEASE</version><relativePath/></parent><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 數(shù)據(jù)庫(kù)連接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.10</version></dependency><!-- Mysql Connector --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><!-- JPA --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- Lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.4</version></dependency><!-- Swagger API文檔 --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.7.0</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.7.0</version></dependency><!-- 熱更新 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>總結(jié)
以上是生活随笔為你收集整理的spring-boot-starter-parent 作用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 1.Spring Cloud Aliba
- 下一篇: Logstash配置多个Input、Fi