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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
您可能感兴趣的文章:
如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!
网友评论