maven使用${revision}管理多模块版本

在maven多模块项目中,可配合插件flatten-maven-plugin及${revision}属性进行全局版本号管理。

pom.xml配置

配置flatten-maven-plugin插件

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>flatten-maven-plugin</artifactId>
            <version>1.2.1</version>
            <configuration>
                <!-- 避免IDE将 .flattened-pom.xml 自动识别为功能模块 -->
                <flattenedPomFilename>pom-xml-flattened</flattenedPomFilename>
                <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>

配置父pom

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.luo.demo</groupId>
    <artifactId>maven-package-demo</artifactId>
    <packaging>pom</packaging>
    <version>${revision}</version>
    <modules>
        <module>module-1</module>
        <module>module-2</module>
    </modules>

    <name>maven-package-demo</name>

    <properties>
        <revision>1.0.0-SNAPSHOT</revision>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!--
            推荐使用${project.version}管理子模块依赖版本(兼容不使用${revision}的模式),
            亦可直接使用${revision}。
            -->
            <dependency>
                <groupId>com.luo.demo</groupId>
                <artifactId>module-1-1</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.luo.demo</groupId>
                <artifactId>module-1-2</artifactId>
                <version>${project.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>flatten-maven-plugin</artifactId>
                ...
            </plugin>        
        </plugins>
       
    </build>
</project>

配置子模块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">
    <parent>
        <artifactId>maven-package-demo</artifactId>
        <groupId>com.luo.demo</groupId>
        <version>${revision}</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>module-1</artifactId>

</project>

注:
父子模块需遵循父子目录层次。

关于子模块中parent.relativePath使用

1、默认值,不设置relativePath即为默认值,等价于<relativePath>../pom.xml</relativePath>

  • 即优先查找上层目录../pom.xml
  • 然后查找本地仓库
  • 最后查找远程仓库
  • 推荐自定义的开发项目遵循此种方式(即父子模块需遵循父子目录层次,且保持parent.relativePath的默认值)

2、空值<relativePath/>,即跳过本地文件目录查找

  • 直接查找本地仓库
  • 最后查找远程仓库
  • 适用于父依赖为第三方公有仓库中的依赖,如spring-boot-starter-parent

3、其他值,可根据目录层次自行定义(推荐使用相对目录层次),如<relativePath>../module-1/pom.xml</relativePath>

不可混合使用${revision}和明确字符串版本号

若出现父子模块版本号混合使用${revision}和明确字符串形式如1.0.0.-SNAPSHOT,在mvn package会出现类似如下错误:

[FATAL] Non-resolvable parent POM for com.luo.demo:module-1:[unknown-version]:
Could not find artifact com.luo.demo:maven-package-demo:pom:1.0.0-SNAPSHOT and ‘parent.relativePath’ points at wrong local POM @ line 5, column 13

若使用插件flatten-maven-plugin及${revision},切记要全局(父pom及所有子pom)都使用${revision}来管理版本号(即<version>${revision}</version>

### Maven 多模块项目中的模块间通信最佳实践 #### 1. 使用依赖管理来定义模块关系 在一个多模块Maven 项目中,父 POM 文件用于集中管理和协调各个子模块之间的版本控制和依赖关系。通过这种方式可以确保所有子模块使用一致的库版本。 ```xml <dependencyManagement> <dependencies> <!-- 定义公共依赖 --> <dependency> <groupId>com.example</groupId> <artifactId>common-utils</artifactId> <version>${project.version}</version> </dependency> </dependencies> </dependencyManagement> ``` #### 2. 明确声明内部依赖 当一个模块需要访问另一个模块的功能时,在该模块的 `pom.xml` 中显式地添加对目标模块作为编译期或运行时依赖项。这有助于构建工具理解不同部分之间存在的联系并正确处理它们[^1]。 ```xml <!-- 子模块A pom.xml --> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>module-b</artifactId> <version>${revision}</version> </dependency> </dependencies> ``` #### 3. 接口分离模式 (Interface Segregation Principle) 遵循接口隔离原则设计API层,使得高层组件不直接依赖于低层组件;相反二者都应依赖抽象接口。这样做的好处是可以减少耦合度,并允许更灵活的服务替换机制[^2]。 #### 4. API 文档与契约测试 为了保证跨团队协作顺畅以及维护长期稳定性,应该为公开暴露出来的服务编写详细的文档说明其行为规范。同时引入契约测试框架如 Pact 或者 Spring Cloud Contract 来验证消费者和服务提供者的交互是否符合预期[^3]。 #### 5. 配置中心统一管理配置文件 对于分布式系统的环境变量设置可以通过外部化的方式来进行集中管控。Spring Boot 提供了 Config Server 功能支持从远程仓库加载属性值,从而简化部署流程的同时也提高了安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值