随笔 - 441  文章 - 4  评论 - 84  阅读 - 109万 

1.概述

在使用maven开发的时候,我们会使用到多模块开发,比如我们开发一些starter,我们可以在一个项目下开发多个starter,如果每个模块都有一个版本,我们需要对每个版本进行调整。这样会很麻烦。所以我们需要实现多个模块实现版本统一,在引用的时候实现统一版本就可以了。

2.实现方法

在maven 中有一个特殊的变量,revision这个变量可以在多个模块下实现继承。

  • 父模块配置

比如我们创建一个父模块。

<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.redxun</groupId>
    <artifactId>jpaas-commons</artifactId>
    <description>通用组件</description>
    <packaging>pom</packaging>
    <version>${revision}</version>

    <properties>
        <revision>20250104-RELEASE</revision>
	</properties>
	<modules>
        <module>jpaas-redis-spring-boot-starter</module>
        <module>jpaas-common-spring-boot-starter</module>
        <module>jpaas-db-spring-boot-starter</module>
	</modules>
	
	<build>
        <plugins>
            <!-- 添加flatten-maven-plugin插件 -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>flatten-maven-plugin</artifactId>
                <version>1.3.0</version>
                <inherited>true</inherited>
                <executions>
                    <execution>
                        <id>flatten</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>flatten</goal>
                        </goals>
                        <configuration>
                            <!-- 避免IDE将 .flattened-pom.xml 自动识别为功能模块 -->
                            <updatePomFile>true</updatePomFile>
                            <flattenMode>resolveCiFriendliesOnly</flattenMode>
                            <pomElements>
                                <parent>expand</parent>
                                <distributionManagement>remove</distributionManagement>
                                <repositories>remove</repositories>
                            </pomElements>
                        </configuration>
                    </execution>
                    <execution>
                        <id>flatten.clean</id>
                        <phase>clean</phase>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

上面的插件是需要配置的,他的作用是在发布的时候,将${revision} 变量使用属性进行替换。

我们可以看到,在本地仓库我们可以看到 这个 ${revision} 被替换成了实际的版本,包括子模块的版本。

  • 子模块配置
<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.redxun</groupId>
        <artifactId>jpaas-commons</artifactId>
        <version>${revision}</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <artifactId>jpaas-common-core</artifactId>
    <name>jpaas-common-core-${version}</name>
    <description>公共通用组件</description>
    <packaging>jar</packaging>

</project>

我们在子模块中,不需要再pom.xml 指定版本,另外groupId也可以不指定。

需要注意的问题

在配置parent 包的时候,版本不能使用快照的形式。

image

如果版本号配置为 快照版会报上面的错误。

解决办法时,需要将 parent 发包,可以只发布parent pom文件,parent 需要指定版本,不能使用快照版。

posted on   自由港  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2015-01-04 ACTIVITI 表结构数据分析
点击右上角即可分享
微信分享提示