maven父子工程多模块如何管理统一的版本号?

1.为什么要统一管理?

maven父子工程多模块,每个模块还都可以独立存在,子模块往往通常希望和父工程保持一样的版本,如果每个工程单独定义版本号,后期变更打包也非常麻烦,如何维护一个全局的版本号呢?

2.如何解决呢?

复制代码
Maven官方文档说:自 Maven 3.5.0-beta-1 开始,可以使用 ${revision}, ${sha1} and/or ${changelist} 这样的变量作为版本占位符。

即在maven多模块项目中,可配合插件flatten-maven-plugin${revision}属性来实现全局版本统一管理。

父工程

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-parent</artifactId>
        <version>2.7.18</version>
    </parent>
    <groupId>com.xxx.project</groupId>
    <artifactId>xxx-parent</artifactId>
    <packaging>pom</packaging>
    <version>${revision}</version>
    <modules>
        <module>module1</module>
        <module>module2</module>
        <module>module3</module>
    </modules>

    <properties>
        <!-- globe version,if you can update the version for all project -->
        <revision>1.1.1</revision>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <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>
                            <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>

子模块

<?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>xxx-parent</artifactId>
        <groupId>com.xxx.project</groupId>
        <version>${revision}</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>module3</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>

        <dependency>
            <groupId>com.xxx.project</groupId>
            <artifactId>module1</artifactId>
            <version>${revision}</version>
        </dependency>
    </dependencies>


</project>

编译

mvn clean package

基于以上操作,每次版本号变更,只需要修改父模块POM文件中的revision即可

3.引用

相关推荐
GoppViper24 分钟前
golang学习笔记24——golang微服务中配置管理问题的深度剖析
笔记·后端·学习·微服务·golang·配置管理
景天科技苑25 分钟前
【Go】Go语言中延迟函数、函数数据的类型、匿名函数、闭包等高阶函数用法与应用实战
后端·golang·回调函数·defer·匿名函数·闭包·go函数数据类型
技术无疆35 分钟前
ButterKnife:Android视图绑定的简化专家
android·java·android studio·android-studio·androidx·butterknife·视图绑定
ZachOn1y1 小时前
Java 入门指南:JVM(Java虚拟机)垃圾回收机制 —— 垃圾收集器
java·jvm·后端·java-ee·团队开发·个人开发
天上掉下来个程小白1 小时前
请求响应-05.请求-日期参数&JSON参数
spring boot·json
攸攸太上1 小时前
Java通配符的作用
java·学习·通配符
齐 飞1 小时前
使用jackson将xml和对象、List相互转换
xml·java·spring boot·后端·list
liwulin05061 小时前
java-在ANTLR中BaseListner的方法和词法规则的关系0.5.0
java·开发语言
SofterICer1 小时前
PE-PINCodes 规则
java
归去来兮★2 小时前
c++面向对象
java·开发语言·c++