这篇是 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. nginx主配置文件详解

    #定义Nginx运行的用户和用户组user www www; #nginx进程数,建议设置为等于CPU总核心数.worker_processes 8; #全局错误日志定义类型,[ debug | in ...

  2. 产品设计-后台管理权限设计RBAC

    最近在做OA系统,设计到不同的员工会拥有不同权限对OA进行操作,总结了一下整体的设计 做权限的分配就是为了更好的管理不同类别的员工,如人事部可以看到普通员工的薪酬,可以查看全部员工的考勤数据请假等,而 ...

  3. c++中 . 和 -> 的区别是什么?

    主要用于访问类的成员,->主要用于类类型的指针访问类的成员,而.运算符,主要用于类类型的对象访问类的成员. 例如: class A { public :int a } A ma; A *p=&a ...

  4. final等关键字和代码块

    一.final关键字 其作用 1.final除构造方法外均可修饰 2.修饰类:被final修饰的类是无法被继承的. 3.修饰方法,可被继承,但是无法被重写 4.修饰变量使其为常量 5.修饰引用型变量, ...

  5. 22 python 初学(类,面向对象)

    python: 函数式 + 面向对象 函数式可以做所有的事,是否合适? 面向对象: 一.定义: 函数: def + 函数名(参数) 面向对象: class  -> 名字叫 Bar 类 def   ...

  6. ENABLE_DDL_LOGGING 参数使用 监控对象的DDL(在alter 日志记录DDL语句)

    启用 DDL 日志记录 功能--支持动态调整 alter system set enable_ddl_logging=true; alter system set enable_ddl_logging ...

  7. (六)List All Indices

    Now let’s take a peek at our indices: 现在让我们来看看我们的指数: GET /_cat/indices?v And the response: health st ...

  8. android 图片上传图片 报Socket: Broken pipe

    上传图片的时候报如下错误: 上传失败的原因是服务器限制了文件上传的大小.让服务端改一下配置文件就好了

  9. Lepus搭建企业级数据库全方位监控系统

    前言 Lepus(天兔)数据库企业监控系统是一套由专业DBA针对互联网企业开发的一款专业.强大的企业数据库监控管理系统,企业通过Lepus可以对数据库的实时健康和各种性能指标进行全方位的监控.目前已经 ...

  10. 06-JavaScript的流控制语句

    06-JavaScript的流控制语句 JavaScript的流控制语句主要分为三大类: 顺序控制:因为JS是一门解释性语言,所以从上至下按顺序依次执行 分支控制:主要分为if条件语句和swith开关 ...