问题

maven 工程,依赖管理是非常基本又非常重要的功能,现在的工程越来越庞大,依赖越来越多,各种二方包、三方包太多太多,依赖冲突处理起来真是让人头疼,经常需要涉及到多个地方需要调整。

微信公众号:逸飞兮(专注于java知识领域的源码分析,从源码中理解框架/工具原理、验证CS专业知识)

解决方案

使用统一的依赖管理模块来管理工程中的 所有 依赖。

spring-boot 工程常使用 spring-boot-dependencies、spring-boot-starter-parent 管理工程依赖。

spring-boot 的最上级工程是 spring-boot-build,以下开始一步一步深入了解 spring-boot 依赖解决方案。

spring-boot 中的方案

spring-boot-build

spring-boot 的最上层工程,指定了 maven profiles、maven repositories、maven pluginRepositories、maven build pluginManagement。

  • profiles:中包含代码风格检查、代码风格 format;更方便导入 eclipse;maven 仓库
  • repositories:允许在开发过程中导入快照和里程碑 BOM。这个部分在 install/deploy 期间被 flatten 插件删除。包含 maven 中央仓库、spring 快照仓库、spring 里程碑仓库
  • pluginRepositories:插件仓库,包含 maven 中央仓库、spring 快照仓库、spring 里程碑仓库
  • pluginManagement:构建插件管理,这个插件的配置只用于存储 Eclipse m2e 设置,它对 Maven 构建本身没有影响。

spring-boot-dependencies

dependencies 的父工程是 spring-boot-build ,不包含代码,只用 pom 来管理依赖,pom.xml 如下:

org.springframework.boot spring-boot-build ${revision}../..spring-boot-dependenciespom

从 pom 中可以看出,spring-boot-dependencies 中除了引入了(3 个)插件,更多的是做版本的管理。

其中,引入的插件是:

  • flatten-maven-plugin:对 pom 精简插件
  • xml-maven-plugin:1. 根据 schema 验证 XML 文件;2. 使用 XSLT 样式转换 XML 文件
  • build-helper-maven-plugin:指定多个源码目录

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 插件对于 spring-boot 工程非常重要,会把 maven 打包成的 jar 重新打包成可执行 jar。

spring-boot-starter-parent(重要)

既然有了 spring-boot-dependencies 这么丰富的依赖、插件版本管理,那么还搞一个 spring-boot-starter-parent 呢?

spring-boot-starter-parent 的父工程是 spring-boot-dependencies ,不包含代码,只用 pom 来管理依赖,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}

特性

  • 默认编译版本:Java 1.8
  • 源码编码:UTF-8
  • 继承自 spring-boot-dependencies 的 dependencyManagement
  • spring-boot-maven-plugin 的 goal 设置为 repackage
  • maven 资源过滤(application*.yml、application*.yaml、application*.properties 等)、插件配置
  • 资源分隔符:“@”,在 application*.yml 中使用@来引用 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 将更改为使用@…@占位符。(可以通过设置名为 resource.delimiter 的 maven 属性来覆盖该属性。)

spring-boot-parent

org.springframework.boot spring-boot-dependencies ${revision}../spring-boot-dependenciesspring-boot-parentpom

dependencyManagement

包含两个部分:

  • 内部未发布的 spring-boot 依赖
  • 附加的 Spring 引导依赖项 (对用户无效)

因此,这里所加入的依赖管理,用户不需要关心,很好,省心。

dependencies

公共的依赖,主要是一些 测试 依赖,如:junit、hamcrest、mockito、spring-test,还有断言依赖:assertj。

plugins

添加了 spring-boot 公用的一些插件,如:maven-compiler-plugin、maven-jar-plugin、maven-war-plugin、maven-source-plugin 等

profiles

用户基本不用关心。省略

选择

spring-boot-dependencies 和 spring-boot-starter-parent、 spring-boot-parent 都提供了依赖管理的功能,那我们在开发的过程中,到底使用哪个呢?

  • spring-boot-parent :目的不是提供给用户使用的,使用 spring-boot 开源项目用于管理 spring-boot-project 整个大工程中的除了 spring-boot-starters (提供给我们使用的各个开箱即用的三方包) 的其他模块的。
  • spring-boot-starter-parent:我们通过 Spring Initializr 构建一个 spring-boot 项目的时候,官方默认是让我们使用的 spring-boot-starter-parent ,大致可以认为官方建议使用此方式管理依赖,毕竟此方式提供的依赖、插件管理更多,更适合使用。
  • spring-boot-dependencies:若在使用的时候,工程不想指定父工程,或者必须使用公司的父工程,可以通过 dependencyManagement 引入此依赖管理。

使用 spring-boot-dependencies,相比较 spring-boot-starter-parent 的时候特别注意要加上 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,按需添加。

实际使用

在工程中使用的时候,所有的二方、三方 jar 都应该统一管理,除了 spring-boot 提供的依赖,我们还有很多 jar 需要管理,如:mysql 驱动包、mybatis 包、各种工具包或者公司内的二方包等。因此,最好使用一个单独的模块来构建自己的 dependencies 或 parent。

maven 公共模块依赖_「spring-boot 源码解析」spring-boot 依赖管理相关推荐

  1. spring boot 源码解析23-actuate使用及EndPoint解析

    前言 spring boot 中有个很诱人的组件–actuator,可以对spring boot应用做监控,只需在pom文件中加入如下配置即可: <dependency><group ...

  2. 人人都能看懂的Spring源码解析,Spring如何解决循环依赖

    人人都能看懂的Spring源码解析,Spring如何解决循环依赖 原理解析 什么是循环依赖 循环依赖会有什么问题? 如何解决循环依赖 问题的根本原因 如何解决 为什么需要三级缓存? Spring的三级 ...

  3. Spring AOP源码解析——专治你不会看源码的坏毛病!

    昨天有个大牛说我啰嗦,眼光比较细碎,看不到重点.太他爷爷的有道理了!要说看人品,还是女孩子强一些. 原来记得看到一个男孩子的抱怨,说怎么两人刚刚开始在一起,女孩子在心里就已经和他过完了一辈子.哥哥们, ...

  4. 源码解析:Spring源码解析笔记(五)接口设计总览

    本文由colodoo(纸伞)整理 QQ 425343603 Java学习交流群(717726984) Spring解析笔记 启动过程部分已经完成,对启动过程源码有兴趣的朋友可以作为参考文章. 源码解析 ...

  5. 2 Keil自带的8051汇编boot源码解析注释

    本仓库相关网址: CSDN文章地址(待添加) Gitee工程和源码地址 - https://gitee.com/langcai1943/8051-from-boot-to-application 相关 ...

  6. erlang下lists模块sort(排序)方法源码解析(二)

    上接erlang下lists模块sort(排序)方法源码解析(一),到目前为止,list列表已经被分割成N个列表,而且每个列表的元素是有序的(从大到小) 下面我们重点来看看mergel和rmergel ...

  7. spring事务源码解析

    前言 在spring jdbcTemplate 事务,各种诡异,包你醍醐灌顶!最后遗留了一个问题:spring是怎么样保证事务一致性的? 当然,spring事务内容挺多的,如果都要讲的话要花很长时间, ...

  8. Spring AOP源码解析-拦截器链的执行过程

    一.简介 在前面的两篇文章中,分别介绍了 Spring AOP 是如何为目标 bean 筛选合适的通知器,以及如何创建代理对象的过程.现在得到了 bean 的代理对象,且通知也以合适的方式插在了目标方 ...

  9. jQuery源码解析(架构与依赖模块)

    jQuery设计理念 引用百科的介绍: jQuery是继prototype之后又一个优秀的Javascript框架.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1. ...

  10. jQuery源码解析(架构与依赖模块)第一章 理解架构

    1-1 jQuery设计理念 引用百科的介绍: jQuery是继prototype之后又一个优秀的Javascript框架.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, F ...

最新文章

  1. WPF中的动画——(五)路径动画
  2. spring学习(29):xml配置规范
  3. html动态散花代码,IOS实现签到特效(散花效果)的实例代码
  4. html文件打开是搜索导航,GitHub - bituplink/OneHtmlNav: 单文件html的网络导航页面,简约并不简单...
  5. 2019牛客暑期多校训练营(第七场)-C Governing sand
  6. mysql c函数大全_Mysql 函数大全
  7. Ubuntu中使用dnw工具:没有找到/dev/secbulk0
  8. idea谷歌翻译插件translation失效问题(可行)
  9. (转)架构师害怕程序员知道的十项技能
  10. 5.8G雷达感应模块,微波雷达传感器技术,人体存在感应雷达
  11. HTML做一个传统节日端午节 带设计报告4500字
  12. rabbit的安装配置过程
  13. 每周分享第 47 期
  14. 用华为ENSP做一个关于防火墙的小实验-华为eNSP怎样调出右侧接口列表
  15. redis设计与实现 笔记(一)
  16. XGBoost python调参示例
  17. python图片转换成文字_Python输出汉字字库及将文字转换为图片的方法
  18. 2020 年最佳 Open Exchange 开发者和应用程序!
  19. 北京针对CFA持证人有什么优惠条件
  20. 辞退了一位简历造假的程序员

热门文章

  1. java定时任务详解
  2. luogu P1659 养猪 dp 好理解
  3. 【4.0】jdbcTemplate
  4. [Bugku][Web][CTF] 16-29 write up
  5. MySQL数据库面试题
  6. 服务器水厂物资管理系统,水处理管理系统及水处理管理服务器 Water management systems and water treatment management server...
  7. set集合判断集合中是否有无元素_一文了解 JavaScript 中的 Set(集合)- 对 Set 的扩展...
  8. webview 防止js注入_天台县js聚合物水泥防水涂料的作用
  9. 家用电脑配置_游戏搬砖必看教程,游戏工作室电脑如何配置
  10. mysql workbench中文设置_使用Workbench完成流体压力渗透分析