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 

猜你喜欢

转载自www.cnblogs.com/harrychinese/p/SpringBoot_maven_multi-module.html
今日推荐