Maven聚合项目一键生成版本号

目的:实现一键生成日期格式版本号,并打包,最后还原版本号.

首先,讲过程

我们需要有一个packaging类型为pom的pom.xml文件即我们的parent项目pom文件

<packaging>pom</packaging>

在这个parent项目中配置好groupId,artifactId,version以及properties,prerequisites,dependencies.

还有一个重要的配置项是modules.加入了这个项目之后执行maven版本号更新时才会同时去更新子模块的版本号.

<modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>

    <groupId>com.hhj.demo</groupId>
    <artifactId>hhj-demo</artifactId>
    <version>1.0</version>

    <modules>
        <module>hhj-common</module>
        <module>hhj-srv</module>
        <module>hhj-business</module>
    </modules> 

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>

接着,我们执行:

mvn versions:set -DnewVersion=0.0.2-SNAPSHOT

会发现在父模块和子模块下面都生成maven的版本控制文件pom.xml.versionsBackup(姑且这么叫吧,虽然看起来就是一个文件备份).

这里.我通过设置idea将*.versionsBackup文件隐藏掉.

同时,父模块和子模块的verion都更新为了0.0.2-SNAPSHOT.

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hhj.demo</groupId>
    <artifactId>hhj-srv</artifactId>
    <version>0.0.2-SNAPSHOT</version>

到这里已经完成了子模块的版本号更新.

如果还不行,那就再加一个命令吧

mvn versions:update-child-modules

完成!

最后,为了方便我将其做出bat文件.

实现一键生成日期格式版本号,并打包,最后还原版本号.

三个bat文件分别:

1version.bat生成日期格式版本号

@echo off  
set localdir=%~dp0
set hh=%time:~0,2%
if /i %hh% LSS 10 (set hh=0%time:~1,1%)
mvn versions:set -DnewVersion=1.0.%Date:~0,4%%Date:~5,2%%Date:~8,2%.%hh%
mvn versions:update-child-modules
mvn versions:commit
pause

3install.bat生成日期格式版本号,并打包,最后还原版本号

@echo off  
 
set localdir=%~dp0
call %~dp0/1version.bat
call mvn clean install -Dmaven.test.skip=true
call %~dp0/5version-default.bat
pause

5version-default.bat还原版本号

@echo off  
set localdir=%~dp0
mvn versions:set -DnewVersion=1.0
mvn versions:update-child-modules
mvn versions:commit
pause

将三个bat文件放置项目根目录下,双击执行3install.bat最终效果

:

经验分享 程序员 微信小程序 职场和发展