这篇是 maven 项目管理的第二篇, 讲解使用 maven 进行多个项目管理, 之前有一篇是 maven 的基础知识.

SpringBoot系列: Eclipse+Maven环境准备

一个完整的解决方案通常都会包含多个项目, 这些项目往往会有一些公用的依赖, 比如都依赖 SpringBoot, 各个项目之间也有依赖关系. 显然如果在每个项目都设置这些信息, 并不是很好, 一个明显的缺点是, 当要统一升级某个基础 jar 包, 所有项目 pom.xml 都需要更新.

对此 Maven 有很好的解决思路, 即创建 aggregate project(或 multi-module project). maven multi-module 包含一个 parent 项目和多个子项目, 所以从逻辑上讲它们是有层次关系的. 但 Eclipse 中每个项目都是直接挂在 workspace 下的, 也就是说在文件系统上是没有层级关系的, 所以我们可以在 parent 项目名称上做点文章, 体现出它与众不同, 比如起名为 myproject-parent .

下面是个 Eclipse 项目关系图:
Workspace
├─ myproject-parent (通常仅仅包含一个 pom.xml 文件)
├─ myproject-webui
├─ myproject-api
└─ myproject-jobs

======================================
Parent 项目 pom.xml 一般包含的节点
======================================
0. packaging 节点, 对于 parent 项目, packaging 属性值为 pom
1. parent 节点: 对于 SpringBoot 项目, parent artifactId 应该为 spring-boot-starter-parent, 指定版本.
2. dependencyManagement/dependencies/dependency 节点, 经常使用 dependencyManagement 作为 jar 包版本仲裁, 在 dependencyManagement 下声明的依赖, 并不会被实际引入, 只有 dependencies/dependency 节点下的 jar 包才会被引入.
3. modules 节点, 设定本 parent 项目包含哪些 sub module 项目, 推荐在使用 profiles 节点下定义子模块, 而不是直接在 modules 节点下设定子模块.
4. properties 节点, 设定将所有项目共有的属性设置在这里.
5. build/plugins/plugin 节点: 将公用的插件放到这里, 比如 spring-boot-maven-plugin
6. dependencies/dependency 节点, 更推荐使用 dependencyManagement/dependencies/dependency.
7. profiles 节点, 这个下面在详细讲解.

----------------------------------
dependencyManagement 仲裁 jar 包版本
----------------------------------
Parent 项目中, 统一了 spring-core 版本为 4.3.5.RELEASE.

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>

在子项目中, 需要引入 spring-core 时就不必再指定版本号.

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
</dependencies>

如果某个子项目中, 需要引入一个其他版本的 spring-core 时, 在 dependencies 节点下直接指定想要的版本即可.

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
</dependencies>

----------------------------------
使用 profiles
----------------------------------
通常一个较大规模的 Parent 项目会包含好几个 sub module 项目, 各个子项目之前可能有依赖, 可能没有依赖, 为了缩短编译时间, 最好设定不同的 profile , 不同 profile 中包含的不同的 sub-module 组合.

  <profiles>
<profile>
<id>backend</id>
<modules>
<module>../myproject-api</module>
<module>../myproject-webui</module>
</modules>
</profile>
<profile>
<id>all</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>../myproject-api</module>
<module>../myproject-webui</module>
<module>../myproject-jobs</module>
</modules>
</profile>
</profiles>

上面定义了两个 profile, 分别是 backend 和 all, maven 支持按 profile 进行编译.

仅编译 backend 相关的子模块:
mvn clean package -Pbackend

编译缺省的 profile:
mvn clean package

======================================
子项目中指定 parent pom 的位置
======================================

<parent>
<groupId>com.harry</groupId>
<artifactId>myproject-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../myproject-parent/pom.xml</relativePath>
</parent>

======================================
参考
======================================
https://www.smartics.eu/confluence/display/BLOG/2013/07/22/Using+Aggregate+and+Parent+POMs
https://www.baeldung.com/maven

SpringBoot系列: Maven多项目管理的更多相关文章

  1. maven(项目管理工具系列 maven 总结二)

    ♣maven是什么? ♣maven下载.安装 ♣了解maven仓库 ♣eclipse配置maven ♣创建maven项目 ♣把maven项目转化为web项目 1.maven是什么? Maven是一个项 ...

  2. Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件

    前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...

  3. SpringBoot系列——Spring-Data-JPA

    前言 jpa是ORM映射框架,更多详情,请戳:apring-data-jpa官网:http://spring.io/projects/spring-data-jpa,以及一篇优秀的博客:https:/ ...

  4. SpringBoot系列——Security + Layui实现一套权限管理后台模板

    前言 Spring Security官网:https://spring.io/projects/spring-security Spring Security是一个功能强大且高度可定制的身份验证和访问 ...

  5. SpringBoot系列教程之Bean加载顺序之错误使用姿势辟谣

    在网上查询 Bean 的加载顺序时,看到了大量的文章中使用@Order注解的方式来控制 bean 的加载顺序,不知道写这些的博文的同学自己有没有实际的验证过,本文希望通过指出这些错误的使用姿势,让观文 ...

  6. Springboot 系列(十五)如何编写自己的 Springboot starter

    1. 前言 Springboot 中的自动配置确实方便,减少了我们开发上的复杂性,那么自动配置原理是什么呢?之前我也写过了一篇文章进行了分析. Springboot 系列(三)Spring Boot ...

  7. SpringBoot系列之i18n集成教程

    目录 1.环境搭建 2.resource bundle资源配置 3.LocaleResolver类 4.I18n配置类 5.Thymeleaf集成 SpringBoot系统之i18n国际化语言集成教程 ...

  8. SpringBoot系列之集成Thymeleaf用法手册

    目录 1.模板引擎 2.Thymeleaf简介 2.1).Thymeleaf定义 2.2).适用模板 3.重要知识点 3.1).th:text和th:utext 3.2).标准表达式 3.3).Thy ...

  9. SpringBoot系列之集成jsp模板引擎

    目录 1.模板引擎简介 2.环境准备 4.源码原理简介 SpringBoot系列之集成jsp模板引擎 @ 1.模板引擎简介 引用百度百科的模板引擎解释: 模板引擎(这里特指用于Web开发的模板引擎)是 ...

随机推荐

  1. HybridStart发布v1.0测试版

    HybridStart是一款多webview模式的混合应用前端开发框架,本来只是作者自用的一套混合应用开发模板,为了进一步提高混合应用开发效率,近期着重在框架高通用性和易用性方面做了较大改进,比如将U ...

  2. Failed to start /etc/rc.d/rc.local Compatibility

    查看/var/log/message Jun :: root systemd: Started Network Manager. Jun :: root systemd: Starting LSB: ...

  3. MySQL之记录相关操作

    一 介绍 MySQL数据操作: DML ======================================================== 在MySQL管理软件中,可以通过SQL语句中的 ...

  4. 【题解】P2324 [SCOI2005]骑士精神

    ·有关IDA* 是带有估值函数的迭代加深搜索,表现出出色的效率. 估值函数可以简单的定义为「已经到位的骑士的个数」. 然后就是普通的迭代加深了. 算法酷炫不一定赢,搜索好才是成功. ——Loli Co ...

  5. python小白——进阶之路——day2天-———容器类型数据(list,set ,tuple,dict,str)

    #容器类型数据 : list tuple # ###列表的特性:可获取,可修改,有序 # 声明一个空列表 listvar = [] print(listvar,type(listvar)) # (1) ...

  6. C# — 调用dll出现试图加载不正确格式的程序问题

    今天在调用百度dll包时,运行项目出现了如下警告: 修改:鼠标右击项目名称----选择属性----生成-----平台目标-----X64(由于我调用的是X64的dll包,所以这里选择X64,网上许多说 ...

  7. 程序员买房指南——LZ的三次买房和一次卖房经历

    引言 买房,一直是程序员群体绕不开的一个话题,尤其是到了一定年纪和人生阶段以后,买房这件事会变得越来越迫切. 为什么LZ一上来就说,买房是程序员绕不开的一个话题? 其实原因很简单,由于程序员这个职业的 ...

  8. Git命令集

    安装 Window https://gitforwindows.org/ MAC http://sourceforge.net/projects/git-osx-installer/ git conf ...

  9. jenkins集成python时出现"Non-ASCII character '\xe6' in file"错误解决方法

    我的问题: 使用python3.5,在Linux环境下手动执行python文件时不报错,但是用jenkins自动执行时就报"Non-ASCII character '\xe6' in fil ...

  10. 妙解Servlet四大域对象

    pageContext pageContext作用域为page(页面执行期). request request是表示一个请求,只要发出一个请求就会创建一个request,它的作用域仅在当前请求中有效. ...