You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何固定Maven构建时的timestamp属性,避免其在构建中更新?

Maven构建中固定app.version时间戳的解决方案

在将基于Maven的Java项目从v3.3.9迁移到v3.6.1后,原配置通过maven.build.timestamp设置app.version属性:

<properties>
    <app.version>${maven.build.timestamp}</app.version>
    <maven.build.timestamp.format>yyyyMMddHHmm</maven.build.timestamp.format>
</properties>

由于构建时长超过一分钟,${app.version}会在构建过程中被重新计算,导致版本号前后不一致(比如从202309080807变为202309080810)。调整时间戳格式为yyyyMMddHH仅能临时规避非整点构建的情况,以下是几种彻底固定该属性的方案:

方案1:用build-helper-maven-plugin在初始化阶段生成固定时间戳

借助build-helper插件在项目初始化阶段生成一次性时间戳,后续所有引用都使用这个固定值。配置如下:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>3.3.0</version>
            <executions>
                <execution>
                    <id>timestamp-property</id>
                    <phase>initialize</phase>
                    <goals>
                        <goal>timestamp-property</goal>
                    </goals>
                    <configuration>
                        <name>fixed.app.version</name>
                        <pattern>yyyyMMddHHmm</pattern>
                        <timeZone>UTC</timeZone> <!-- 可选,指定时区避免环境差异 -->
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

之后把项目中所有引用${app.version}的地方替换为${fixed.app.version},该值只会在initialize阶段计算一次,全程保持不变。

方案2:命令行传入固定时间戳

构建时手动指定时间戳参数,强制整个构建过程使用同一个版本号,适合CI/CD流水线场景:

mvn clean install -Dapp.version=$(date +%Y%m%d%H%M)

流水线启动时生成一次时间戳并传入,彻底避免版本号中途变化。

方案3:结合antrun与properties插件预生成版本号

通过maven-antrun-plugin在初始化阶段将当前时间戳写入properties文件,再用properties插件读取该固定值:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>generate-version</id>
                    <phase>initialize</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <tstamp>
                                <format property="fixed.timestamp" pattern="yyyyMMddHHmm"/>
                            </tstamp>
                            <echo file="version.properties">app.version=${fixed.timestamp}</echo>
                        </tasks>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.1.0</version>
            <executions>
                <execution>
                    <id>read-version</id>
                    <phase>initialize</phase>
                    <goals>
                        <goal>read-project-properties</goal>
                    </goals>
                    <configuration>
                        <files>
                            <file>version.properties</file>
                        </files>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

该方式会在构建最开始生成一次时间戳并持久化到文件,后续所有阶段读取的都是这个固定值。

内容的提问来源于stack exchange,提问作者Nicolas C

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 16:25:13