1.先建一个父工程

image.png

2.删除src修改父工程的pom

由于该项目是作为父工程,所以是不要src文件夹的,直接删除。然后打开pom文件,做一些配置。

  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. <!-- 将springboot作为父工程 -->
  7. <parent>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-parent</artifactId>
  10. <version>2.1.8.RELEASE</version>
  11. <relativePath/> <!-- lookup parent from repository -->
  12. </parent>
  13. <groupId>cn.tfp</groupId>
  14. <artifactId>test</artifactId>
  15. <version>1.0.0</version>
  16. <name>test</name>
  17. <!--父工程的packaging方式必须是pom-->
  18. <packaging>pom</packaging>
  19. <modules>
  20. <module>common</module>
  21. <module>model</module>
  22. <module>dao</module>
  23. <module>service</module>
  24. <module>api</module>
  25. </modules>
  26. <!--自定义环境变量-->
  27. <properties>
  28. <main-version>1.0.0</main-version>
  29. <java.version>1.8</java.version>
  30. <!-- 设置maven版本,否则pom文件报错,原因就是maven版本和springboot版本不一致 -->
  31. <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
  32. <!-- 打包时跳过test测试 -->
  33. <skipTests>true</skipTests>
  34. <mybatis-plus-version>3.1.0</mybatis-plus-version>
  35. <druid-version>1.1.16</druid-version>
  36. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  37. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  38. </properties>
  39. <!--dependencyManagement这里是作版本控制,相当于只是对依赖的定义,并不是真的依赖这些模块,而是在子模块依赖相对应的依赖时,能够不用写版本号-->
  40. <dependencyManagement>
  41. <dependencies>
  42. <dependency>
  43. <groupId>cn.tfp.test</groupId>
  44. <artifactId>api</artifactId>
  45. <version>${main-version}</version>
  46. </dependency>
  47. <dependency>
  48. <groupId>cn.tfp.test</groupId>
  49. <artifactId>service</artifactId>
  50. <version>${main-version}</version>
  51. </dependency>
  52. <dependency>
  53. <groupId>cn.tfp.test</groupId>
  54. <artifactId>model</artifactId>
  55. <version>${main-version}</version>
  56. </dependency>
  57. <dependency>
  58. <groupId>cn.tfp.test</groupId>
  59. <artifactId>dao</artifactId>
  60. <version>${main-version}</version>
  61. </dependency>
  62. <dependency>
  63. <groupId>cn.tfp.test</groupId>
  64. <artifactId>common</artifactId>
  65. <version>${main-version}</version>
  66. </dependency>
  67. <dependency>
  68. <groupId>com.baomidou</groupId>
  69. <artifactId>mybatis-plus-boot-starter</artifactId>
  70. <version>${mybatis-plus-version}</version>
  71. </dependency>
  72. <dependency>
  73. <groupId>com.alibaba</groupId>
  74. <artifactId>druid</artifactId>
  75. <version>${druid-version}</version>
  76. </dependency>
  77. </dependencies>
  78. </dependencyManagement>
  79. <!--依赖,子模块会继承这些依赖-->
  80. <dependencies>
  81. <dependency>
  82. <groupId>org.springframework.boot</groupId>
  83. <artifactId>spring-boot-starter-web</artifactId>
  84. <exclusions><!-- 去掉springboot默认配置 -->
  85. <exclusion>
  86. <groupId>org.springframework.boot</groupId>
  87. <artifactId>spring-boot-starter-logging</artifactId>
  88. </exclusion>
  89. </exclusions>
  90. </dependency>
  91. <dependency> <!-- 引入log4j2依赖 -->
  92. <groupId>org.springframework.boot</groupId>
  93. <artifactId>spring-boot-starter-log4j2</artifactId>
  94. </dependency>
  95. <dependency>
  96. <groupId>org.springframework.boot</groupId>
  97. <artifactId>spring-boot-devtools</artifactId>
  98. <scope>runtime</scope>
  99. <optional>true</optional>
  100. </dependency>
  101. <dependency>
  102. <groupId>org.springframework.boot</groupId>
  103. <artifactId>spring-boot-starter-test</artifactId>
  104. <scope>test</scope>
  105. </dependency>
  106. <dependency>
  107. <groupId>org.projectlombok</groupId>
  108. <artifactId>lombok</artifactId>
  109. <optional>true</optional>
  110. </dependency>
  111. </dependencies>
  112. </project>

注意点: 1.父工程的packaging必须是pom 2.父工程中不用写build标签 3.为了方便,我直接在父工程中引入了spring-boot-starter-web

3.搭建子模块

new module 然后选择image.pngspringboot的快捷创建方式。
删除无用的文件mvn、.gitignore、daoiml、mvnw、mvnw.cmd文件只留下pom.xml
删除除了web模块以外其它模块中的Applicatin启动项,和resources目录下的application.properties配置文件
删除后大致是这样的:
image.png
修改子模块的pom文件
1.修改parent标签,使他指向父工程:

  1. <parent>
  2. <groupId>cn.tfp</groupId>
  3. <artifactId>test</artifactId>
  4. <version>1.0.0</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>

2.修改自身的坐标version,main-version在父工程中定义了,所以可以直接使用,所有的子模块都是用main-version,以便版本统一:

  1. <groupId>cn.tfp.test</groupId>
  2. <artifactId>common</artifactId>
  3. <version>${main-version}</version>

3.删除properties标签(会继承父工程的,所以这里不需要写了)

4.添加packaging标签,定义打包方式为jar,除了api这个web模块为war,其他均是jar包:

  1. <packaging>jar</packaging>

5.删除build标签,除了api模块,其他均不需要该标签打包

  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. <parent>
  6. <groupId>cn.tfp</groupId>
  7. <artifactId>test</artifactId>
  8. <version>1.0.0</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>cn.tfp.test</groupId>
  12. <artifactId>common</artifactId>
  13. <version>${main-version}</version>
  14. <name>common</name>
  15. <packaging>jar</packaging>
  16. <dependencies>
  17. </dependencies>
  18. </project>

参考文章:https://blog.csdn.net/weixin_38312502/article/details/103050786