创建父项目 mall
先创建一个"父项目"mall,再在这个父项目中创建多个子项目
最终mall的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
? <modelVersion>4.0.0</modelVersion>
? <parent>
? ? ? <groupId>org.springframework.boot</groupId>
? ? ? <artifactId>spring-boot-starter-parent</artifactId>
? ? ? <version>2.5.4</version>
? ? ? <relativePath/> <!-- lookup parent from repository -->
? </parent>
? <groupId>com.taobao</groupId>
? <artifactId>mall</artifactId>
? <version>0.0.1-SNAPSHOT</version>
? <name>mall</name>
? <description>Demo project for Spring Boot</description>
? <!-- ? 当前项目会以一个pom文件的形式被子项目继承 ? -->
? <packaging>pom</packaging>
</project>
创建子项目
创建mall-stock项目
在父项目的pom文件中,编写子项目的存在
<!-- 表示当前项目是一个父项目,以pom文件的形式,供子项目继承 -->
<packaging>pom</packaging>
<!-- 当前父项目包含的所有模块 -->
<modules>
? <module>mall-stock</module>
</modules>
子项目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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
? <modelVersion>4.0.0</modelVersion>
? <parent>
? ? ? <groupId>com.taobao</groupId>
? ? ? <artifactId>mall</artifactId>
? ? ? <version>0.0.1-SNAPSHOT</version>
? ? ? <relativePath/> <!-- lookup parent from repository -->
? </parent>
? <groupId>com.taobao</groupId>
? <artifactId>mall-stock</artifactId>
? <version>0.0.1-SNAPSHOT</version>
? <name>mall-stock</name>
? <description>Demo project for Spring Boot</description>
? <dependencies>
? ? ? <dependency>
? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? <artifactId>spring-boot-starter</artifactId>
? ? ? </dependency>
? </dependencies>
</project>
父子相认完成
这样当前子项目就可以读取父项目中的pom文件信息了
父项目管理依赖版本
这样做可以统一所有子项目的版本,在更新版本时,只需要修改父项目中定义的版本号即可
父项目的pom文件添加如下内容
<!-- 定义父项目需要的版本号参数 ? -->
<properties>
? <mybatis.version>2.2.2</mybatis.version>
</properties>
<!-- ? dependencyManagement不是添加依赖,而是确定子项目使用依赖时的版本,也称锁版本 -->
<dependencyManagement>
? <dependencies>
? ? ? <dependency>
? ? ? ? ? <groupId>org.mybatis.spring.boot</groupId>
? ? ? ? ? <artifactId>mybatis-spring-boot-starter</artifactId>
? ? ? ? ? <version>${mybatis.version}</version>
? ? ? </dependency>
? </dependencies>
</dependencyManagement>
子项目中如果需要mybatis的依赖只需要添加如下内容即可,无需再指定版本号
<dependency>
? <groupId>org.mybatis.spring.boot</groupId>
? <artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
父项目完整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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.9</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.taobao</groupId>
<artifactId>mall</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mall</name>
<description>Demo project for Spring Boot</description>
<!-- 表示当前项目是一个父项目,以pom文件的形式,供子项目继承 -->
<packaging>pom</packaging>
<!-- 当前父项目包含的所有模块,module就是模块的意思 -->
<modules>
<module>mall-stock</module>
</modules>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.3</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.2.RELEASE</spring-cloud-alibaba.version>
<spring-boot.version>2.5.4</spring-boot.version>
<spring-boot-configuration-processor.version>2.3.0.RELEASE</spring-boot-configuration-processor.version>
<spring-security-jwt.version>1.0.10.RELEASE</spring-security-jwt.version>
<mybatis-spring-boot.version>2.2.0</mybatis-spring-boot.version>
<mybaits-plus.version>3.4.1</mybaits-plus.version>
<pagehelper-spring-boot.version>1.4.0</pagehelper-spring-boot.version>
<mysql.version>8.0.26</mysql.version>
<lombok.version>1.18.20</lombok.version>
<knife4j-spring-boot.version>2.0.9</knife4j-spring-boot.version>
<spring-rabbit-test.version>2.3.10</spring-rabbit-test.version>
<spring-security-test.version>5.5.2</spring-security-test.version>
<fastjson.version>1.2.45</fastjson.version>
<druid.version>1.1.20</druid.version>
<jjwt.version>0.9.0</jjwt.version>
<seata-server.version>1.4.2</seata-server.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<!-- 依赖管理 -->
<dependencyManagement>
<dependencies>
<!--seata-all-->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>${seata-server.version}</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
<scope>runtime</scope>
</dependency>
<!-- Alibaba Druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- MyBatis Spring Boot:数据访问层MyBatis编程 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot.version}</version>
</dependency>
<!-- MyBatis Plus Spring Boot:MyBatis增强 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybaits-plus.version}</version>
</dependency>
<!-- MyBatis Plus Generator:代码生成器 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>${mybaits-plus.version}</version>
</dependency>
<!-- PageHelper Spring Boot:MyBatis分页 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pagehelper-spring-boot.version}</version>
</dependency>
<!-- Spring Boot:基础框架 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- Spring Boot Web:WEB应用 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- Spring Boot Freemarker:MyBaits Plus Generator的辅助项 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- Spring Boot Validation:验证请求参数 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- Spring Boot Security:认证授权 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- Spring Boot Oauth2:认证授权 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- Spring Boot配置处理器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${spring-boot-configuration-processor.version}</version>
</dependency>
<!-- Spring Security JWT -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-jwt</artifactId>
<version>${spring-security-jwt.version}</version>
</dependency>
<!-- Knife4j Spring Boot:在线API -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>${knife4j-spring-boot.version}</version>
</dependency>
<!-- Spring Boot Data Redis:缓存 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- Spring Boot Data MongoDB:缓存 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- Spring Boot Data Elasticsearch:文档搜索 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- Spring Boot AMQP:消息队列 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- Spring Boot Actuator:健康监测 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- Spring Cloud家族 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Spring Cloud Alibaba -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Alibaba FastJson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- JJWT -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>${jjwt.version}</version>
</dependency>
<!-- Spring Boot Test:测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
<scope>test</scope>
</dependency>
<!-- Spring Rabbit Test:消息队列测试 -->
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<version>${spring-rabbit-test.version}</version>
<scope>test</scope>
</dependency>
<!-- Spring Security Test:Security测试 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>${spring-security-test.version}</version>
<scope>test</scope>
</dependency>
<!--seata整合springboot-->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>${seata-server.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
创建通用模块(项目)commons
父子相认
<module>mall-commons</module>
子项目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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
? <modelVersion>4.0.0</modelVersion>
? <parent>
? ? ? <groupId>com.taobao/groupId>
? ? ? <artifactId>mall</artifactId>
? ? ? <version>0.0.1-SNAPSHOT</version>
? ? ? <relativePath/> <!-- lookup parent from repository -->
? </parent>
? <groupId>com.taobao</groupId>
? <artifactId>mall-commons</artifactId>
? <version>0.0.1-SNAPSHOT</version>
? <name>mall-commons</name>
? <description>Demo project for Spring Boot</description>
? <dependencies>
? ? ? <!--在线api文档-->
? ? ? <dependency>
? ? ? ? ? <groupId>com.github.xiaoymin</groupId>
? ? ? ? ? <artifactId>knife4j-spring-boot-starter</artifactId>
? ? ? </dependency>
? ? ? <!-- Spring Boot Web:WEB应用 -->
? ? ? <dependency>
? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? <artifactId>spring-boot-starter-web</artifactId>
? ? ? ? ? <exclusions>
? ? ? ? ? ? ? <exclusion>
? ? ? ? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? ? ? ? ? <artifactId>spring-boot-starter</artifactId>
? ? ? ? ? ? ? </exclusion>
? ? ? ? ? ? ? <exclusion>
? ? ? ? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? ? ? ? ? <artifactId>spring-boot-starter-json</artifactId>
? ? ? ? ? ? ? </exclusion>
? ? ? ? ? ? ? <exclusion>
? ? ? ? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? ? ? ? ? <artifactId>spring-boot-starter-tomcat</artifactId>
? ? ? ? ? ? ? </exclusion>
? ? ? ? ? </exclusions>
? ? ? </dependency>
? </dependencies>
?
</project>
当前模块只是编写通用类和代码
|