前言

通常SpringBoot新建项目,默认是集成了Maven,然后所有内容都在一个主模块中。
如果项目架构稍微复杂一点,就需要用到Maven多模块。
本文简单概述一下,新建一个SpringBoot Maven 多模块项目的流程。
具体内容还需要参考你具体的项目中的实际需求,
我这里只按照大多数通常的情况为准创建一个基础款demo
另外模块都只基于Maven的默认模板进行创建,
需要SpringBoot是用手动新建文件的方式来实现。

环境:
- Java 1.8
- IntelliJ IDEA 2020.01
- Maven 3.3.9
- SpringBoot 2.2.6.RELEASE
- Mybatis Plus 3.3.1



折腾

第一步 新建一个maven项目

1 新建项目 Create New Project

2 选择Maven 然后直接下一步

3 根据需要修改配置,然后Finish即可

4 直接删除不需要的文件夹和文件,如 src文件夹


第二步 新建子模块

右击项目 -> New -> Module

同样选择Maven 直接下一步 完成

重复以上步骤,建立所有需要的子模块
并删除不需要的文件或文件夹
例如test文件夹


第三步 配置Maven

主项目 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. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.demo</groupId>
  7. <artifactId>modules-demo</artifactId>
  8. <packaging>pom</packaging>
  9. <version>1.0.0</version>
  10. <name>modules-demo</name>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.2.6.RELEASE</version>
  15. </parent>
  16. <modules>
  17. <module>module-api</module>
  18. <module>module-admin</module>
  19. <module>module-common</module>
  20. </modules>
  21. <properties>
  22. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  23. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  24. <java.version>1.8</java.version>
  25. <mysql.version>8.0.19</mysql.version>
  26. <fastjson.version>1.2.68</fastjson.version>
  27. <mybatisplus.spring.boot.version>3.3.1</mybatisplus.spring.boot.version>
  28. <mybatisplus.generator.version>3.3.1</mybatisplus.generator.version>
  29. <freemarker.version>2.3.30</freemarker.version>
  30. </properties>
  31. <dependencies>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-test</artifactId>
  35. <scope>test</scope>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-starter-web</artifactId>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-starter-aop</artifactId>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.springframework</groupId>
  47. <artifactId>spring-context-support</artifactId>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.springframework.boot</groupId>
  51. <artifactId>spring-boot-starter-data-redis</artifactId>
  52. </dependency>
  53. <dependency>
  54. <groupId>org.springframework.boot</groupId>
  55. <artifactId>spring-boot-configuration-processor</artifactId>
  56. <optional>true</optional>
  57. </dependency>
  58. <dependency>
  59. <groupId>mysql</groupId>
  60. <artifactId>mysql-connector-java</artifactId>
  61. <version>${mysql.version}</version>
  62. </dependency>
  63. <dependency>
  64. <groupId>com.alibaba</groupId>
  65. <artifactId>fastjson</artifactId>
  66. <version>${fastjson.version}</version>
  67. </dependency>
  68. <dependency>
  69. <groupId>com.baomidou</groupId>
  70. <artifactId>mybatis-plus-boot-starter</artifactId>
  71. <version>${mybatisplus.spring.boot.version}</version>
  72. </dependency>
  73. <dependency>
  74. <groupId>com.baomidou</groupId>
  75. <artifactId>mybatis-plus-generator</artifactId>
  76. <version>${mybatisplus.generator.version}</version>
  77. </dependency>
  78. <dependency>
  79. <groupId>org.freemarker</groupId>
  80. <artifactId>freemarker</artifactId>
  81. <version>${freemarker.version}</version>
  82. </dependency>
  83. </dependencies>
  84. <repositories>
  85. <repository>
  86. <id>public</id>
  87. <name>aliyun nexus</name>
  88. <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  89. <releases>
  90. <enabled>true</enabled>
  91. </releases>
  92. </repository>
  93. </repositories>
  94. <pluginRepositories>
  95. <pluginRepository>
  96. <id>public</id>
  97. <name>aliyun nexus</name>
  98. <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  99. <releases>
  100. <enabled>true</enabled>
  101. </releases>
  102. <snapshots>
  103. <enabled>false</enabled>
  104. </snapshots>
  105. </pluginRepository>
  106. </pluginRepositories>
  107. </project>

工具性质 子项目 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>modules-demo</artifactId>
  7. <groupId>com.demo</groupId>
  8. <version>1.0.0</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>module-common</artifactId>
  12. <packaging>jar</packaging>
  13. <build>
  14. <finalName>${project.artifactId}</finalName>
  15. </build>
  16. </project>

接口性质 子项目 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>modules-demo</artifactId>
  7. <groupId>com.demo</groupId>
  8. <version>1.0.0</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>module-admin</artifactId>
  12. <build>
  13. <finalName>${project.artifactId}</finalName>
  14. <plugins>
  15. <plugin>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-maven-plugin</artifactId>
  18. </plugin>
  19. <plugin>
  20. <groupId>org.apache.maven.plugins</groupId>
  21. <artifactId>maven-surefire-plugin</artifactId>
  22. <configuration>
  23. <skipTests>true</skipTests>
  24. </configuration>
  25. </plugin>
  26. </plugins>
  27. </build>
  28. </project>

打包时,工具类的会先生成jar包,并引入到接口类中,接口类再生成war包,可以放Tomcat下运行


第四步 配置SrpingBoot

接口类加入启动类和配置文件

手动创建启动类
ModuleAdminApplication.java

  1. package com.demo.modules.admin;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. @SpringBootApplication
  6. @MapperScan(basePackages = "com.demo.modules.admin.mapper")
  7. public class ModuleAdminApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(com.demo.modules.admin.ModuleAdminApplication.class, args);
  10. }
  11. }

ServletInitializer.java

  1. package com.demo.modules.admin;
  2. import org.springframework.boot.builder.SpringApplicationBuilder;
  3. import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
  4. public class ServletInitializer extends SpringBootServletInitializer {
  5. @Override
  6. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  7. return application.sources(ModuleAdminApplication.class);
  8. }
  9. }

配置文件
application.yml

  1. spring:
  2. profiles:
  3. active: dev
  4. jackson:
  5. time-zone: GMT+8
  6. serialization:
  7. write-dates-as-timestamps: true

application-dev.yml

  1. # 开发环境
  2. server:
  3. port: 10000
  4. servlet:
  5. context-path: /modules/admin/dev
  6. spring:
  7. servlet:
  8. multipart:
  9. max-file-size: 100MB
  10. max-request-size: 100MB
  11. datasource:
  12. driver-class-name: com.mysql.cj.jdbc.Driver
  13. url: jdbc:mysql://localhost:3306/modules?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
  14. username: root
  15. password: root
  16. redis:
  17. database: 9
  18. mybatis-plus:
  19. mapper-locations: classpath*:/mapper/*.xml
  20. type-aliases-package: com.demo.modules.admin.serivce
  21. global-config:
  22. db-config:
  23. id-type: auto
  24. logic-delete-value: 1
  25. logic-not-delete-value: 0
  26. insert-strategy: not_null
  27. update-strategy: not_null
  28. select-strategy: not_null
  29. banner: false
  30. configuration:
  31. map-underscore-to-camel-case: true
  32. cache-enabled: true
  33. call-setters-on-nulls: true
  34. logging:
  35. file: logs/modules.admin.dev.log
  36. level.com: debug

application-prod.yml

  1. # 生产环境
  2. server:
  3. port: 10000
  4. servlet:
  5. context-path: /modules/admin/prod
  6. spring:
  7. jmx:
  8. enabled: false
  9. servlet:
  10. multipart:
  11. max-file-size: 20MB
  12. max-request-size: 20MB
  13. datasource:
  14. driver-class-name: com.mysql.cj.jdbc.Driver
  15. url: jdbc:mysql://mysql:3306/modules?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
  16. username: root
  17. password: root
  18. redis:
  19. database: 10
  20. host: redis
  21. port: 6379
  22. timeout: 15s
  23. jedis:
  24. pool:
  25. max-active: 1000
  26. max-wait: -1ms
  27. min-idle: 4
  28. max-idle: 20
  29. mybatis-plus:
  30. mapper-locations: classpath*:/mapper/*.xml
  31. type-aliases-package: com.demo.modules.admin.serivce
  32. global-config:
  33. db-config:
  34. id-type: auto
  35. logic-delete-value: 1
  36. logic-not-delete-value: 0
  37. insert-strategy: not_null
  38. update-strategy: not_null
  39. select-strategy: not_null
  40. banner: false
  41. configuration:
  42. map-underscore-to-camel-case: true
  43. cache-enabled: true
  44. call-setters-on-nulls: true
  45. logging:
  46. file: logs/modules.admin.prod.log
  47. level.com: warn


第五步 Maven 打包生成war


直接用Idea自带的可视化即可。
先去settings中配置好maven目录

在Maven面板中找到根项目,
选择clean
再选择install
大功告成!



END

最终效果
Gitee: https://gitee.com/tczmh/modules-demo