1.先建一个父工程
2.删除src修改父工程的pom
由于该项目是作为父工程,所以是不要src文件夹的,直接删除。然后打开pom文件,做一些配置。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 将springboot作为父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.tfp</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>
<name>test</name>
<!--父工程的packaging方式必须是pom-->
<packaging>pom</packaging>
<modules>
<module>common</module>
<module>model</module>
<module>dao</module>
<module>service</module>
<module>api</module>
</modules>
<!--自定义环境变量-->
<properties>
<main-version>1.0.0</main-version>
<java.version>1.8</java.version>
<!-- 设置maven版本,否则pom文件报错,原因就是maven版本和springboot版本不一致 -->
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<!-- 打包时跳过test测试 -->
<skipTests>true</skipTests>
<mybatis-plus-version>3.1.0</mybatis-plus-version>
<druid-version>1.1.16</druid-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<!--dependencyManagement这里是作版本控制,相当于只是对依赖的定义,并不是真的依赖这些模块,而是在子模块依赖相对应的依赖时,能够不用写版本号-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>cn.tfp.test</groupId>
<artifactId>api</artifactId>
<version>${main-version}</version>
</dependency>
<dependency>
<groupId>cn.tfp.test</groupId>
<artifactId>service</artifactId>
<version>${main-version}</version>
</dependency>
<dependency>
<groupId>cn.tfp.test</groupId>
<artifactId>model</artifactId>
<version>${main-version}</version>
</dependency>
<dependency>
<groupId>cn.tfp.test</groupId>
<artifactId>dao</artifactId>
<version>${main-version}</version>
</dependency>
<dependency>
<groupId>cn.tfp.test</groupId>
<artifactId>common</artifactId>
<version>${main-version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus-version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid-version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!--依赖,子模块会继承这些依赖-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions><!-- 去掉springboot默认配置 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency> <!-- 引入log4j2依赖 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
注意点: 1.父工程的packaging必须是pom 2.父工程中不用写build标签 3.为了方便,我直接在父工程中引入了spring-boot-starter-web
3.搭建子模块
new module 然后选择springboot的快捷创建方式。
删除无用的文件mvn、.gitignore、daoiml、mvnw、mvnw.cmd文件只留下pom.xml
删除除了web模块以外其它模块中的Applicatin启动项,和resources目录下的application.properties配置文件
删除后大致是这样的:
修改子模块的pom文件
1.修改parent标签,使他指向父工程:
<parent>
<groupId>cn.tfp</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
2.修改自身的坐标version,main-version在父工程中定义了,所以可以直接使用,所有的子模块都是用main-version,以便版本统一:
<groupId>cn.tfp.test</groupId>
<artifactId>common</artifactId>
<version>${main-version}</version>
3.删除properties标签(会继承父工程的,所以这里不需要写了)
4.添加packaging标签,定义打包方式为jar,除了api这个web模块为war,其他均是jar包:
<packaging>jar</packaging>
5.删除build标签,除了api模块,其他均不需要该标签打包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.tfp</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.tfp.test</groupId>
<artifactId>common</artifactId>
<version>${main-version}</version>
<name>common</name>
<packaging>jar</packaging>
<dependencies>
</dependencies>
</project>
参考文章:https://blog.csdn.net/weixin_38312502/article/details/103050786