maven 公共模块依赖_「spring-boot 源码解析」spring-boot 依赖管理
問(wèn)題
maven 工程,依賴管理是非常基本又非常重要的功能,現(xiàn)在的工程越來(lái)越龐大,依賴越來(lái)越多,各種二方包、三方包太多太多,依賴沖突處理起來(lái)真是讓人頭疼,經(jīng)常需要涉及到多個(gè)地方需要調(diào)整。
微信公眾號(hào):逸飛兮(專注于java知識(shí)領(lǐng)域的源碼分析,從源碼中理解框架/工具原理、驗(yàn)證CS專業(yè)知識(shí))
解決方案
使用統(tǒng)一的依賴管理模塊來(lái)管理工程中的 所有 依賴。
spring-boot 工程常使用 spring-boot-dependencies、spring-boot-starter-parent 管理工程依賴。
spring-boot 的最上級(jí)工程是 spring-boot-build,以下開始一步一步深入了解 spring-boot 依賴解決方案。
spring-boot 中的方案
spring-boot-build
spring-boot 的最上層工程,指定了 maven profiles、maven repositories、maven pluginRepositories、maven build pluginManagement。
- profiles:中包含代碼風(fēng)格檢查、代碼風(fēng)格 format;更方便導(dǎo)入 eclipse;maven 倉(cāng)庫(kù)
- repositories:允許在開發(fā)過(guò)程中導(dǎo)入快照和里程碑 BOM。這個(gè)部分在 install/deploy 期間被 flatten 插件刪除。包含 maven 中央倉(cāng)庫(kù)、spring 快照倉(cāng)庫(kù)、spring 里程碑倉(cāng)庫(kù)
- pluginRepositories:插件倉(cāng)庫(kù),包含 maven 中央倉(cāng)庫(kù)、spring 快照倉(cāng)庫(kù)、spring 里程碑倉(cāng)庫(kù)
- pluginManagement:構(gòu)建插件管理,這個(gè)插件的配置只用于存儲(chǔ) Eclipse m2e 設(shè)置,它對(duì) Maven 構(gòu)建本身沒(méi)有影響。
spring-boot-dependencies
dependencies 的父工程是 spring-boot-build ,不包含代碼,只用 pom 來(lái)管理依賴,pom.xml 如下:
org.springframework.boot spring-boot-build ${revision}../..spring-boot-dependenciespom從 pom 中可以看出,spring-boot-dependencies 中除了引入了(3 個(gè))插件,更多的是做版本的管理。
其中,引入的插件是:
- flatten-maven-plugin:對(duì) pom 精簡(jiǎn)插件
- xml-maven-plugin:1. 根據(jù) schema 驗(yàn)證 XML 文件;2. 使用 XSLT 樣式轉(zhuǎn)換 XML 文件
- build-helper-maven-plugin:指定多個(gè)源碼目錄
dependencyManagement 中差不多管理了 spring-boot 工程中所有的依賴。
pluginManagement 中管理了常用的各種 maven 插件,這里就不詳述了。
其中包含了 maven-clean-plugin、maven-compiler-plugin、maven-assembly-plugin、maven-war-plugin、maven-jar-plugin、 spring-boot-maven-plugin ,其中 spring-boot-maven-plugin 插件對(duì)于 spring-boot 工程非常重要,會(huì)把 maven 打包成的 jar 重新打包成可執(zhí)行 jar。
spring-boot-starter-parent(重要)
既然有了 spring-boot-dependencies 這么豐富的依賴、插件版本管理,那么還搞一個(gè) spring-boot-starter-parent 呢?
spring-boot-starter-parent 的父工程是 spring-boot-dependencies ,不包含代碼,只用 pom 來(lái)管理依賴,pom.xml 如下:
org.springframework.boot spring-boot-dependencies ${revision}../../spring-boot-dependenciesspring-boot-starter-parentpomSpring Boot Starter ParentParent pom providing dependency and plugin management for applications built with Maven${basedir}/../../..1.8@UTF-8UTF-8${java.version}${java.version}${basedir}/src/main/resourcestrue**/application*.yml**/application*.yaml**/application*.properties${basedir}/src/main/resources**/application*.yml**/application*.yaml**/application*.propertiesorg.springframework.boot spring-boot-maven-plugin repackagerepackage${start-class}特性
- 默認(rèn)編譯版本:Java 1.8
- 源碼編碼:UTF-8
- 繼承自 spring-boot-dependencies 的 dependencyManagement
- spring-boot-maven-plugin 的 goal 設(shè)置為 repackage
- maven 資源過(guò)濾(application*.yml、application*.yaml、application*.properties 等)、插件配置
- 資源分隔符:“@”,在 application*.yml 中使用@來(lái)引用 maven 屬性,常見用法如下:spring.application.name=@artifactId@
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 .)
譯:
注意,由于 application.properties 和 application.yml 文件接受 spring 樣式的占位符($…),所以 maven filter 將更改為使用@…@占位符。(可以通過(guò)設(shè)置名為 resource.delimiter 的 maven 屬性來(lái)覆蓋該屬性。)
spring-boot-parent
org.springframework.boot spring-boot-dependencies ${revision}../spring-boot-dependenciesspring-boot-parentpomdependencyManagement
包含兩個(gè)部分:
- 內(nèi)部未發(fā)布的 spring-boot 依賴
- 附加的 Spring 引導(dǎo)依賴項(xiàng) (對(duì)用戶無(wú)效)
因此,這里所加入的依賴管理,用戶不需要關(guān)心,很好,省心。
dependencies
公共的依賴,主要是一些 測(cè)試 依賴,如:junit、hamcrest、mockito、spring-test,還有斷言依賴:assertj。
plugins
添加了 spring-boot 公用的一些插件,如:maven-compiler-plugin、maven-jar-plugin、maven-war-plugin、maven-source-plugin 等
profiles
用戶基本不用關(guān)心。省略
選擇
spring-boot-dependencies 和 spring-boot-starter-parent、 spring-boot-parent 都提供了依賴管理的功能,那我們?cè)陂_發(fā)的過(guò)程中,到底使用哪個(gè)呢?
- spring-boot-parent :目的不是提供給用戶使用的,使用 spring-boot 開源項(xiàng)目用于管理 spring-boot-project 整個(gè)大工程中的除了 spring-boot-starters (提供給我們使用的各個(gè)開箱即用的三方包) 的其他模塊的。
- spring-boot-starter-parent:我們通過(guò) Spring Initializr 構(gòu)建一個(gè) spring-boot 項(xiàng)目的時(shí)候,官方默認(rèn)是讓我們使用的 spring-boot-starter-parent ,大致可以認(rèn)為官方建議使用此方式管理依賴,畢竟此方式提供的依賴、插件管理更多,更適合使用。
- spring-boot-dependencies:若在使用的時(shí)候,工程不想指定父工程,或者必須使用公司的父工程,可以通過(guò) dependencyManagement 引入此依賴管理。
使用 spring-boot-dependencies,相比較 spring-boot-starter-parent 的時(shí)候特別注意要加上 spring-boot-maven-plugin ,如下:
org.springframework.boot spring-boot-maven-plugin org.springframework.boot spring-boot-maven-plugin ${springboot.version}至于 spring-boot-starter-parent 的其他額外指定的 jar,按需添加。
實(shí)際使用
在工程中使用的時(shí)候,所有的二方、三方 jar 都應(yīng)該統(tǒng)一管理,除了 spring-boot 提供的依賴,我們還有很多 jar 需要管理,如:mysql 驅(qū)動(dòng)包、mybatis 包、各種工具包或者公司內(nèi)的二方包等。因此,最好使用一個(gè)單獨(dú)的模塊來(lái)構(gòu)建自己的 dependencies 或 parent。
總結(jié)
以上是生活随笔為你收集整理的maven 公共模块依赖_「spring-boot 源码解析」spring-boot 依赖管理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 小心数据丢失!618矿盘出货:避坑指南来
- 下一篇: 【转】3.2SharePoint服务器端