SpringBoot系列: Maven多项目管理
这篇是 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多项目管理的更多相关文章
- maven(项目管理工具系列 maven 总结二)
♣maven是什么? ♣maven下载.安装 ♣了解maven仓库 ♣eclipse配置maven ♣创建maven项目 ♣把maven项目转化为web项目 1.maven是什么? Maven是一个项 ...
- Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件
前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...
- SpringBoot系列——Spring-Data-JPA
前言 jpa是ORM映射框架,更多详情,请戳:apring-data-jpa官网:http://spring.io/projects/spring-data-jpa,以及一篇优秀的博客:https:/ ...
- SpringBoot系列——Security + Layui实现一套权限管理后台模板
前言 Spring Security官网:https://spring.io/projects/spring-security Spring Security是一个功能强大且高度可定制的身份验证和访问 ...
- SpringBoot系列教程之Bean加载顺序之错误使用姿势辟谣
在网上查询 Bean 的加载顺序时,看到了大量的文章中使用@Order注解的方式来控制 bean 的加载顺序,不知道写这些的博文的同学自己有没有实际的验证过,本文希望通过指出这些错误的使用姿势,让观文 ...
- Springboot 系列(十五)如何编写自己的 Springboot starter
1. 前言 Springboot 中的自动配置确实方便,减少了我们开发上的复杂性,那么自动配置原理是什么呢?之前我也写过了一篇文章进行了分析. Springboot 系列(三)Spring Boot ...
- SpringBoot系列之i18n集成教程
目录 1.环境搭建 2.resource bundle资源配置 3.LocaleResolver类 4.I18n配置类 5.Thymeleaf集成 SpringBoot系统之i18n国际化语言集成教程 ...
- SpringBoot系列之集成Thymeleaf用法手册
目录 1.模板引擎 2.Thymeleaf简介 2.1).Thymeleaf定义 2.2).适用模板 3.重要知识点 3.1).th:text和th:utext 3.2).标准表达式 3.3).Thy ...
- SpringBoot系列之集成jsp模板引擎
目录 1.模板引擎简介 2.环境准备 4.源码原理简介 SpringBoot系列之集成jsp模板引擎 @ 1.模板引擎简介 引用百度百科的模板引擎解释: 模板引擎(这里特指用于Web开发的模板引擎)是 ...
随机推荐
- HBase源码实战:CreateRandomStoreFile
/* * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agre ...
- No FileSystem for scheme: hdfs问题
通过FileSystem.get(conf)初始化的时候,要通过静态加载来实现,其加载类的方法代码如下: private static FileSystem createFileSystem(URI ...
- 在Linux命令行中以图形化窗口打开文件夹
Linux 系统中也有类似的命令.Ubuntu 发行版的命令行中,我们可以使用 nautilus 命令来打开指定目录的图形化窗口界面.类似下面命令这样使用: nautilus /home/testPr ...
- php 表单提交大量数据发生丢失的解决方法
最近在项目中,出现一个奇怪的现象,有一个大form里面有上千个input,提交的时候,老是发现post过来的数据不完整,一开始还怀疑是html 表单名称有冲突,排除掉了.然后,网上找了一堆,php.i ...
- WiFi广告强推的基本技术原理和一些相关问题
WiFi推原理(转) 本文地址:http://jb.tongxinmao.com/Article/Detail/id/412 WiFi广告强推的基本技术原理和一些相关问题 WiFi广告推送原理就是利用 ...
- python之json模块
#!/usr/bin/python # -*- coding: UTF- -*- ''' )序列化相关 json pickle (序列化是指将) 序列化是指将一个字符串转换成基础数据类型或者基础数据类 ...
- Linux内存管理 (8)malloc
专题:Linux内存管理专题 关键词:malloc.brk.VMA.VM_LOCK.normal page.special page. 每章问答: malloc()函数是C函数库封装的一个核心函数,对 ...
- 如何基于Winform开发框架或混合框架基础上进行项目的快速开发
在开发项目的时候,我们为了提高速度和质量,往往不是白手起家,需要基于一定的基础上进行项目的快速开发,这样可以利用整个框架的生态基础模块,以及成熟统一的开发方式,可以极大提高我们开发的效率.本篇随笔就是 ...
- idea免费破解
1.下载破解补丁. https://pan.baidu.com/s/1pWCr_HIHURSAbGvvo70wKA 密码:pxkv 2.下载idea网址: https://www.jetbrain ...
- SSH服务器拒绝了密码,请再试一次
使用Xshell连接ubuntu后,出现: SSH服务器拒绝了密码,请再试一次! 输入: cd /etc/ssh/ 继续: vim sshd_config 若此时提示没有安装vim,那我们安装以下: ...