详解SpringBoot获得Maven-pom中版本号和编译时间戳
作者:zhaodongxx 时间:2022-11-27 19:45:27
这篇文章首先介绍了在SpringBoot中如何获得项目的编译时间和版本号,并向外提供接口,然后介绍了介绍了新版maven获得时间戳时区错误的解决方案,最后介绍了把时间戳加到包名的两种方法。
SpringBoot项目获得编译时间戳和版本号,然后提供接口大概分为以下步骤:
在pom文件中获取maven的编译时间戳
配置pom,用maven中的属性渲染资源文件application.yml
提供接口
Maven中获得编译时间
在 pom文件properties 中添加两个属性
<properties>
<!--maven.build.timestamp保存了maven编译时间戳-->
<timestamp>${maven.build.timestamp}</timestamp>
<!--指定时间格式-->
<maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
</properties>
用Maven属性渲染application.yml
在pom文件build中配置
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
在application.yml中配置
不能使用${}
app:
version: @project.version@
build:
time: @timestamp@
提供接口
创建AppController,提供接口 Get /app
package cn.yshow.modules.sys.controller;
import cn.yshow.common.utils.restResult.RestResult;
import cn.yshow.common.utils.restResult.ResultGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* @author zhaod zhaodongxx@outlook.com
* @version v1.0
* @since 2018/12/16 16:19
*/
@Slf4j
@RestController
@RequestMapping("/app")
public class AppController {
/**
* 当前版本
*/
@Value("${app.version}")
private String version;
/**
* 打包时间
*/
@Value("${app.build.time}")
private String buildTime;
@GetMapping
public RestResult uploadImg() {
Map<String,String> ret = new HashMap<>();
ret.put("version",version);
ret.put("buildTime",buildTime);
//RestResult是我封装的返回对象
return ResultGenerator.genSuccessResult(ret);
}
}
测试
用YAPI测试如下
maven.build.timestamp时区错误解决方案
在Maven 3.2.2+中, maven.build.timestamp已被重新定义,显示UTC中的时间,比中国时间慢8个小时。可以使用插件build-helper-maven-plugin获得本时区的时间
在plugins块添加插件,这个配置与官网不一样,按照官网的配置方式会报错
<project>
<build>
<!--指定包名 -->
<finalName>
${project.artifactId}-${project.version}-${build.time}
</finalName>
<plugins>
<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>
</execution>
</executions>
<configuration>
<name>build.time</name>
<pattern>yyyy-MM-dd HH:mm</pattern>
<timeZone>GMT+8</timeZone>
</configuration>
</plugin>
</plugins>
</build>
</project>
经过上述处理后,属性${build.time}已经代表GMT-8时区的时间
application.yml配置如下
app:
version: @project.version@
build:
time: @build.time@
IDEA 编译项目不会调用 maven 生命周期,导致安装的 plugin 不被执行。虽然 maven 自带变量可以被替换,但是自定义变量却不会被替换。
可以将 IDEA 的 Build 与 maven goal 绑定起来,在 build 之前执行 maven goal,IDEA 右侧 Maven Projects -> LifeCycle -> compile 勾选 Execute After Build和Execute After Rebuild ,如下图
把时间戳加到包名
两种方法不要重复,否则
方法一:把时间戳加到版本号
<project>
<versioin>
0.0.5.${build.time}
</version>
</project>
方法二:把时间戳直接加到包名
<project>
<build>
<finalName>
${project.artifactId}-${project.version}-${build.time}
</finalName>
</build>
</project>
参考资料
maven 打包时间戳问题
来源:https://blog.csdn.net/dora_310/article/details/85038475
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Javaweb基础入门requse原理与使用
![](https://img.aspxhome.com/file/2023/6/77096_0s.png)
Mybatis插件之自动生成不使用默认的驼峰式操作
Mybatis查询多条记录并返回List集合的方法
Java 基于tcp协议实现文件上传
sqlite数据库的介绍与java操作sqlite的实例讲解
Java案例使用比较排序器comparator实现成绩排序
java中this的n种使用方法
![](https://img.aspxhome.com/file/2023/3/62063_0s.png)
关于统计数字问题的算法
如何解决修改StaticText的字体
![](https://img.aspxhome.com/file/2023/9/66699_0s.png)
一文搞懂SpringBoot如何利用@Async实现异步调用
![](https://img.aspxhome.com/file/2023/6/63846_0s.png)
Androd 勇闯高阶性能优化之布局优化篇
![](https://img.aspxhome.com/file/2023/8/87278_0s.png)
C#实体对象序列化成Json并让字段的首字母小写的两种解决方法
spring框架cacheAnnotation缓存注释声明解析
![](https://img.aspxhome.com/file/2023/3/65323_0s.png)
Scala小程序详解及实例代码
Java创建树形结构算法实例代码
Java提取2个集合中的相同和不同元素代码示例
深入讲解我们说的CAS自旋锁到底是什么
C#中Linq的去重方式Distinct详解
![](https://img.aspxhome.com/file/2023/0/86970_0s.png)
java开发ServiceLoader实现机制及SPI应用
![](https://img.aspxhome.com/file/2023/8/69728_0s.png)
在IDEA里gradle配置和使用的方法步骤
![](https://img.aspxhome.com/file/2023/1/59851_0s.png)