大厂Java项目如何进行Maven多模块管理 - 掘金

什么是多模块管理?

多模块管理简单地理解就是一个 Java 工程项目中不止有一个 pom.xml 文件,会在不同的目录中有多个这样的文件,进而实现 Maven 的多模块管理

为什么要使用多模块管理?

随着业务的增长,代码会出现以下问题:

  • 不同业务之间的代码互相耦合,难以区分且快速定位问题
  • 增加开发成本,入手难度增高
  • 开发界线模糊,不易定位到具体负责人
  • 对于有特殊需求的模块无法拆解,比如:上传 maven 仓库只需要部分代码即可,但由于只有 1 个模块,不得不全部上传

故而拆分模块之后,可以避免上述问题

模块拆分方案

一般有以下两种拆分方案

  • 按照结构进行拆分

    • project
      • project-service
      • project-controller
      • project-dao
  • 按照业务进行拆分

    • project
      • project-order
      • project-account
      • project-pay

实际项目结构

这里我们使用按照结构拆分的方法进行拆分

help-student-common:放一些所有模块都会用到的共用的部分 help-student-dao:放主要的dao层代码 help-student-entity:放实体类代码 help-student-service:放主要的业务逻辑代码 help-student-web:放web端的代码

image.png

需要注意的是父工程中pom.xml文件的配置,其决定了父子模块之间的依赖关系

1、help-student的pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <packaging>pom</packaging>
  6. <modules>
  7. <module>help-student-web</module>
  8. <module>help-student-entity</module>
  9. <module>help-student-dao</module>
  10. <module>help-student-service</module>
  11. <module>help-student-common</module>
  12. </modules>
  13. <parent>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-parent</artifactId>
  16. <version>2.6.2</version>
  17. <relativePath/> <!-- lookup parent from repository -->
  18. </parent>
  19. <groupId>com.ctguyxr</groupId>
  20. <artifactId>help-student</artifactId>
  21. <version>0.0.1-SNAPSHOT</version>
  22. <name>help-student</name>
  23. <description>help-student</description>
  24. <properties>
  25. <java.version>1.8</java.version>
  26. </properties>
  27. <dependencyManagement>
  28. <dependencies>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-data-jpa</artifactId>
  32. <version>2.6.2</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-web</artifactId>
  37. <version>2.6.2</version>
  38. </dependency>
  39. <dependency>
  40. <groupId>mysql</groupId>
  41. <artifactId>mysql-connector-java</artifactId>
  42. <scope>runtime</scope>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.projectlombok</groupId>
  46. <artifactId>lombok</artifactId>
  47. <optional>true</optional>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.springframework.boot</groupId>
  51. <artifactId>spring-boot-starter-test</artifactId>
  52. <scope>test</scope>
  53. </dependency>
  54. <dependency>
  55. <groupId>com.ctguyxr</groupId>
  56. <artifactId>help-student-service</artifactId>
  57. <version>0.0.1-SNAPSHOT</version>
  58. </dependency>
  59. <dependency>
  60. <groupId>com.ctguyxr</groupId>
  61. <artifactId>help-student-common</artifactId>
  62. <version>0.0.1-SNAPSHOT</version>
  63. </dependency>
  64. <dependency>
  65. <groupId>com.ctguyxr</groupId>
  66. <artifactId>help-student-web</artifactId>
  67. <version>0.0.1-SNAPSHOT</version>
  68. </dependency>
  69. </dependencies>
  70. </dependencyManagement>
  71. <build>
  72. <plugins>
  73. </plugins>
  74. </build>
  75. </project>

2、help-student-web的pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>help-student</artifactId>
  7. <groupId>com.ctguyxr</groupId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>help-student-web</artifactId>
  12. <packaging>jar</packaging>
  13. <properties>
  14. <maven.compiler.source>8</maven.compiler.source>
  15. <maven.compiler.target>8</maven.compiler.target>
  16. </properties>
  17. <dependencies>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-web</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-test</artifactId>
  25. <version>2.6.2</version>
  26. <scope>test</scope>
  27. </dependency>
  28. <dependency>
  29. <groupId>com.ctguyxr</groupId>
  30. <artifactId>help-student-entity</artifactId>
  31. <version>0.0.1-SNAPSHOT</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>com.ctguyxr</groupId>
  35. <artifactId>help-student-dao</artifactId>
  36. <version>0.0.1-SNAPSHOT</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>com.ctguyxr</groupId>
  40. <artifactId>help-student-service</artifactId>
  41. <version>0.0.1-SNAPSHOT</version>
  42. </dependency>
  43. </dependencies>
  44. <!--因为主启动类在这个包下,所以需要加上打包插件-->
  45. <build>
  46. <plugins>
  47. <plugin>
  48. <groupId>org.springframework.boot</groupId>
  49. <artifactId>spring-boot-maven-plugin</artifactId>
  50. <version>2.6.2</version>
  51. </plugin>
  52. </plugins>
  53. </build>
  54. </project>

3、模块之间的依赖关系

👏从零开始搭建SpringBoot+Jpa多模块项目 - 图2

4、help-student-dao的pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>help-student</artifactId>
  7. <groupId>com.ctguyxr</groupId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <packaging>jar</packaging>
  12. <artifactId>help-student-dao</artifactId>
  13. <properties>
  14. <maven.compiler.source>8</maven.compiler.source>
  15. <maven.compiler.target>8</maven.compiler.target>
  16. </properties>
  17. <dependencies>
  18. <dependency>
  19. <groupId>com.ctguyxr</groupId>
  20. <artifactId>help-student-entity</artifactId>
  21. <version>0.0.1-SNAPSHOT</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>mysql</groupId>
  25. <artifactId>mysql-connector-java</artifactId>
  26. <version>8.0.27</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-data-jpa</artifactId>
  31. </dependency>
  32. </dependencies>
  33. <build>
  34. <plugins>
  35. </plugins>
  36. </build>
  37. </project>

5、help-student-service的pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>help-student</artifactId>
  7. <groupId>com.ctguyxr</groupId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <packaging>jar</packaging>
  12. <artifactId>help-student-service</artifactId>
  13. <properties>
  14. <maven.compiler.source>8</maven.compiler.source>
  15. <maven.compiler.target>8</maven.compiler.target>
  16. </properties>
  17. <dependencies>
  18. <dependency>
  19. <groupId>com.ctguyxr</groupId>
  20. <artifactId>help-student-entity</artifactId>
  21. <version>0.0.1-SNAPSHOT</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>com.ctguyxr</groupId>
  25. <artifactId>help-student-dao</artifactId>
  26. <version>0.0.1-SNAPSHOT</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-data-jpa</artifactId>
  31. </dependency>
  32. </dependencies>
  33. <build>
  34. <plugins>
  35. </plugins>
  36. </build>
  37. </project>

6、help-student-entity的pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>help-student</artifactId>
  7. <groupId>com.ctguyxr</groupId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>help-student-entity</artifactId>
  12. <packaging>jar</packaging>
  13. <properties>
  14. <maven.compiler.source>8</maven.compiler.source>
  15. <maven.compiler.target>8</maven.compiler.target>
  16. </properties>
  17. <dependencies>
  18. <dependency>
  19. <groupId>org.projectlombok</groupId>
  20. <artifactId>lombok</artifactId>
  21. <version>1.18.22</version>
  22. <optional>true</optional>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-data-jpa</artifactId>
  27. </dependency>
  28. </dependencies>
  29. <build>
  30. <plugins>
  31. </plugins>
  32. </build>
  33. </project>

项目如何启动

Springboot配置扫描其它模块路径的方法

http://www.javashuo.com/article/p-momaobrc-hc.html

springboot项目打包时提示“程序包xxx不存在,找不到符号” - 夏冬青 - 博客园
SpringBoot默认只会扫描当前包以及当前包下的所有组件,我们多模块的编写的时候就需要配置扫描包的路径,指明需要去哪些包下面扫描我们的组件
需要用到以下几个注解:

  • @EnableJpaRepositorie 用来扫描和发现指定包以及其子包中Repository组件的定义

Springboot应用中@EntityScan和@EnableJpaRepositories的用法 - 星朝 - 博客园

  • @EntityScan 配置实体类的扫描路径,扫描指定包和其子包中所有Entity的定义
  • @ComponentScan 配置组件的扫描路径

由于是多模块开发,启动类和我们的其他组件并不在一个模块中,所以需要进行配置


  1. package com.ctguyxr.web;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.autoconfigure.domain.EntityScan;
  5. import org.springframework.context.annotation.ComponentScan;
  6. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  7. /**
  8. * Created By Intellij IDEA
  9. *
  10. * @author Xinrui Yu
  11. * @date 2021/12/23 20:03 星期四
  12. */
  13. @EnableJpaRepositories(basePackages = {"com.ctguyxr.dao"})
  14. @EntityScan(basePackages = {"com.ctguyxr.entity"})
  15. @ComponentScan(basePackages = {"com.ctguyxr.service.impl","com.ctguyxr.web.controller"})
  16. @SpringBootApplication
  17. public class WebApplication {
  18. public static void main(String[] args) {
  19. SpringApplication.run(WebApplication.class,args);
  20. }
  21. }

在启动类对应的模块下编写SpringBoot的配置文件application.properties,配置好数据库和JPA的相关配置信息。创建好测试用的controller
运行,测试:
image.png
测试接口,接口也能正常响应,说明多模块项目已经搭建成功!!
image.png

打包的时候需要指定启动主类,在pom.xml中配置我们的mainClass即可
maven打包,配置中指定主类 - lshan - 博客园

MyBatis+SpringBoot搭建流程

SpringBoot 多模块构建 - 掘金