Maven实战案例:搭建微服务Maven工程框架
5.1 项目需求和结构分析
需求案例:搭建一个电商平台项目,该平台包括用户服务、订单服务、通用工具模块等。
项目架构:
- 用户服务:负责处理用户相关的逻辑,例如用户信息的管理、用户注册、登录等。
- 订单服务:负责处理订单相关的逻辑,例如订单的创建、订单支付、退货、订单查看等。
- 通用模块:负责存储其他服务需要通用工具类,其他服务依赖此模块。
服务依赖:
- 用户服务 (1.0.1)
- spring-context 6.0.6
- spring-core 6.0.6
- spring-beans 6.0.6
- jackson-databind / jackson-core / jackson-annotations 2.15.0
- 订单服务 (1.0.1)
- shiro-core 1.10.1
- spring-context 6.0.6
- spring-core 6.0.6
- spring-beans 6.0.6
- 通用模块 (1.0.1)
- commons-io 2.11.0
5.2项目搭建和统一构建
- 父模块搭建 (micro-shop)
-
创建父工程
-
pom.xml配置文章来源:https://www.toymoban.com/news/detail-805760.html
-
<?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>
<groupId>com.atguigu</groupId>
<artifactId>micro-shop</artifactId>
<version>1.0.1</version>
<!-- 父工程不打包,所以选择pom值-->
<packaging>pom</packaging>
<properties>
<spring.version>6.0.6</spring.version>
<jackson.version>2.15.0</jackson.version>
<shiro.version>1.10.1</shiro.version>
<commons.version>2.11.0</commons.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- 依赖管理 -->
<dependencyManagement>
<dependencies>
<!-- spring-context会依赖传递core/beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- jackson-databind会依赖传递core/annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- shiro-core -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>${shiro.version}</version>
</dependency>
<!-- commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- 父工程添加依赖,会自动传递给所有子工程,不推荐! -->
</dependencies>
<!-- 统一更新子工程打包插件-->
<build>
<!-- jdk17 和 war包版本插件不匹配 -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
</plugins>
</build>
</project>
- 通用模块 (common-service)
-
创建模块
-
pom.xml配置
-
<?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>
<parent>
<groupId>com.atguigu</groupId>
<artifactId>micro-shop</artifactId>
<version>1.0.1</version>
</parent>
<artifactId>common-service</artifactId>
<!-- 打包方式默认就是jar! -->
<packaging>jar</packaging>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 声明commons-io,继承父工程版本 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
</dependencies>
</project>
- 用户模块 (user-service)
-
创建模块
-
pom.xml配置
-
<?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>
<parent>
<groupId>com.atguigu</groupId>
<artifactId>micro-shop</artifactId>
<version>1.0.1</version>
</parent>
<artifactId>user-service</artifactId>
<packaging>war</packaging>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 添加spring-context 自动传递 core / beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<!-- 添加jackson-databind 自动传递 core / annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
</project>
- 订单模块 (order-service)
- 创建模块
文章来源地址https://www.toymoban.com/news/detail-805760.html
- pom.xml
<?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>
<parent>
<groupId>com.atguigu</groupId>
<artifactId>micro-shop</artifactId>
<version>1.0.1</version>
</parent>
<artifactId>order-service</artifactId>
<packaging>war</packaging>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 继承父工程依赖版本 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<!-- 继承父工程依赖版本 -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
</dependency>
</dependencies>
</project>
总结
核心点 | 掌握目标 |
安装 | maven安装、环境变量、maven配置文件修改 |
工程创建 | gavp属性理解、JavaSE/EE工程创建、项目结构 |
依赖管理 | 依赖添加、依赖传递、版本提取、导入依赖错误解决 |
构建管理 | 构建过程、构建场景、构建周期等 |
继承和聚合 | 理解继承和聚合作用、继承语法和实践、聚合语法和实践 |
到了这里,关于高效构建Java应用:Maven入门和进阶(五)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!