现在的位置: 首页 > 编程语言 > 正文

maven打包时间戳问题

2020年02月13日 编程语言 ⁄ 共 1156字 ⁄ 字号 评论关闭

maven 打包时,可以生成时间戳属性,利用该属性来组装包名。

<version>5.0.${build.time}B</version><properties> <maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format></properties>

对资源文件开启 filtering 功能,资源文件中的 POM 变量在打包过程中会被自动替换。在 java 代码中访问 properties 资源文件,就可以取到版本号。

<resource> <directory>src/main/resources</directory> <filtering>true</filtering></resource>

但是,这个时间戳属性有个问题,生成的时间 ${maven.build.timestamp} 是 UTC 时间,无法修改时区。网上找到一个办法,可以用插件解决这个问题。

<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.8</version> <executions> <execution> <id>timestamp-property</id> <goals> <goal>timestamp-property</goal> </goals> <configuration> <name>build.time</name> <pattern>yyyyMMdd'.'HHmm</pattern> <timeZone>GMT+8</timeZone> </configuration> </execution> </executions></plugin>

该插件生成的属性 ${build.time} 已经是本地时区。

但是,在 IDEA 中, Build Projects ,再 Build Artifacts... ,属性文件中的 ${project.version} 并没有被替换。

摸索了一阵,发现 IDEA Build Projects 并不会调用 maven 生命周期,导致 plugin 不被执行。虽然 maven 自带变量可以被替换,但是自定义变量却不会被替换。

使用工具按钮的 maven install 打包,资源中的变量可以被正常替换。

还有一个方法,可以将 IDEA 的 Build 与 maven goal 绑定起来:

图1 绑定 maven goal

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

本文标题: maven 打包时间戳问题

以上就上有关maven打包时间戳问题的相关介绍,要了解更多maven,打包时间戳,maven,时间戳内容请登录学步园。

抱歉!评论已关闭.