一、前言
使用Maven父子工程来控制微服务整体的统一版本管理是一种高效且常见的做法。在微服务架构中,通常会有多个独立的服务,每个服务都可以看作是一个独立的Maven项目。
然而,为了保持这些服务之间的版本一致性,并简化版本管理工作,可以使用Maven父子工程来统一管理这些服务的版本号,在Maven父子工程中,通过父项目来统一管理所有子项目的版本号。这样可以确保所有子项目都使用相同的版本号,从而避免版本冲突和不一致的问题。
下面是使用Maven构建微服务项目的具体示例:
二、代码示例
1. 创建父工程
1.1 父工程仅仅是一个普通的maven工程就行,我们直接创建普通的maven工程,不引入任何jar包。
1.2. 删除src,父工程不需要这个
1.3. 配置父pom.xml
1.4. 声明父工程的打包方式为pom, 使用dependencyManagement管理依赖,properties管理依赖包的版本号
dependencyManagement作用:dependencyManagement用于在父工程的pom文件中声明依赖,但不引入这些依赖。这意味着,在子工程中,当需要使用到这些声明的依赖时,可以不再指定版本号,从而实现了依赖版本号的统一管理。
properties作用:properties标签用于在pom.xml文件中集中定义所有依赖的版本号或其他常量值。这样做的好处是,当版本号或其他常量值需要更改时,只需要在properties标签中进行修改,而无需在每个依赖项中逐一更改。
1.5. 父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>
<groupId>org.example</groupId>
<artifactId>pz</artifactId>
<version>${revision}</version>
<packaging>pom</packaging>
<modules>
<module>user</module>
<module>order</module>
</modules>
<properties>
<revision>1.1.0<revision>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<flatten-maven-plugin.version>1.3.0</flatten-maven-plugin.version>
<lombok.version>1.18.20</lombok.version>
<spring-boot.version>2.4.1</spring-boot.version>
<mybatis-spring-boot.version>2.2.1</mybatis-spring-boot.version>
</properties>
<!--这个标签,表示只传递版本号,不传递依赖-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot.version}</version>
</dependency>
<dependency>
<!--SpringBoot的依赖配置-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<!--表示传递版本号-->
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<version>${flatten-maven-plugin.version}</version>
<configuration>
<updatePomFile>true</updatePomFile>
<flattenMode>resolveCiFriendliesOnly</flattenMode>
</configuration>
<executions>
<execution>
<id>flatten</id>
<phase>process-resources</phase>
<goals>
<goal>flatten</goal>
</goals>
</execution>
<execution>
<id>flatten.clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<!-- 下载jar仓库地址 -->
<repositories>
<repository>
<id>public</id>
<name>aliyun nexus</name>
<url>https://maven.aliyun.com/repository/public/</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<!-- 下载插件仓库地址 -->
<pluginRepositories>
<pluginRepository>
<id>public</id>
<name>aliyun nexus</name>
<url>https://maven.aliyun.com/repository/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
1.6. 如果需要统一配置自己项目的版本号,也就是我们的项目版本号,比如项目版本号随着时间的推移需要升级从1.0.0到2.0.0时代,可以使用flatten-maven-plugin
增加几个关键配置
<version>${revision}</version>
<flatten-maven-plugin.version>1.3.0</flatten-maven-plugin.version>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<version>${flatten-maven-plugin.version}</version>
<configuration>
<updatePomFile>true</updatePomFile>
<flattenMode>resolveCiFriendliesOnly</flattenMode>
</configuration>
<executions>
<execution>
<id>flatten</id>
<phase>process-resources</phase>
<goals>
<goal>flatten</goal>
</goals>
</execution>
<execution>
<id>flatten.clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
2. 子工程创建
2.1. 创建2个服务,user服务,order服务
2.2. 子工程pom.xml配置
order服务
<?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">
<parent>
<artifactId>pz</artifactId>
<groupId>org.example</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>order</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
user服务
<?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>
<artifactId>pz</artifactId>
<groupId>org.example</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>user</artifactId>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
标签用于指定父模块的基础目录,它是相对于父模块的基础目录而言的。在多模块项目中,每个模块会有一个pom.xml文件,relativePath标签用于在子模块的pom.xml文件中指定父模块的pom.xml文件的位置。
在子项目中使用revision,达到管理项目统一版本号的目的。
2.3. jar包展示,能看到版本都是统一的
2.4 revision处的版本号与父项目的版本号保持一致,当我们微服务模块较多的时候,在父模块中统一管理。
三、总结
相信大家对于如何组织微服务项目的代码已经有一定的思路了。
在 Maven 项目中,${revision} 通常用于版本管理,尤其是在多模块项目或依赖管理中。它代表一个动态的版本号,可以在项目的不同部分或不同模块之间共享。通过使用 ${revision},你可以确保项目中不同模块之间的版本一致性,而无需在每个模块或依赖中手动指定相同的版本号。
使用Maven中的父子项目结构(也称为多模块项目),为所有子模块提供统一的依赖版本。