这篇是 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. SpringIOC和AOP简单概述

    Spring学习:主要学习两大块IOC   AOP 一.IOC   IOC:控制反转(也可以叫做依赖注入)的基本认识:   当某个Java对象(调用者)需要调用另一个Java对象(被依赖对象)的方法时 ...

  2. c# 正则验证

    1.验证百分数 bool tempBool = Regex.IsMatch(str, @"[1-9]{0,1}[0-9](\\.[0-9])?%");

  3. Python学习--Python运算符

    什么是运算符? 举个简单的例子 4 + 5 = 9 . 例子中,4 和 5 被称为操作数,"+" 称为运算符. Python语言支持以下类型的运算符: 算数运算符 比较(关系)运算 ...

  4. ansible 与 Jinja2的结合

    1.文件架构 [root@master template]# tree . ├── jinj2_test.yml ├── meta ├── tasks ├── templates │   └── te ...

  5. SkylineGlobe TerraExplorer for Web 7.1.0版本 接口示例

    在SkylineGlobe TerraExplorer for Web 7.1.0版本(俗称H5免插件版本)中,如何使用SGWorld接口的三维视域分析方法呢? 请参考下面的示例: 通过下面的代码大家 ...

  6. iOS开发基础-UITableView基本属性

    设置 UITableView 中 cell 的背景颜色. 示例1:通过 backgroundView 设置. UIView *view1 = [[UIView alloc] init]; view1. ...

  7. .net core2.1 三层中使用Autofac代替原来Ioc

    首先,现有的三层项目的结构 其中  Repository public interface IPersonRepository { string Eat(); } public class Perso ...

  8. 互怼、IPO、雷潮、寒冬,2018 互联网圈的那些事儿

    有了人的地方,就会有江湖. 有江湖的地方,就会有门派. 有门派的地方,就会有纷争. 有纷争的地方,就会有兴衰. 2018年马上就要离我们远去了,迎接我们的将会是新的一年——2019年.在整个过去的20 ...

  9. Laravel 和 Spring Boot 两个框架比较创业篇(一:开发效率)

    我个人是比较不喜欢去正儿八经的比较两个框架的,这样没有意义,不过欲善其事先利其器! 技术是相通的,但是在某个特定的领域的某个阶段肯定有相对最适合的一个工具! 这里比较不是从技术角度比较,而是从公司技术 ...

  10. DOTween 相关API效果

    1,首先看一遍完整Tween路径 2,操作 DoPlay->DoRestart,DoRestart是从调用时刻重新开始开始执行Tween 3,操作 DoPlay->DoReWind,DoR ...