spring boot-获得Maven-pom中版本号

本文介绍如何在SpringBoot项目中通过Maven配置获取项目的版本号和编译时间戳,并展示了如何在application.properties文件中设置以及如何通过@Value注解在Java类中读取这些信息。

SpringBoot获得Maven-pom中版本号和编译时间戳

SpringBoot获得Maven-pom中版本号和编译时间戳详细步骤
参考URL: https://blog.youkuaiyun.com/weixin_40618648/article/details/109530733

  1. 在pom文件中添加

    <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>
    
  2. application.properties中添加
    application.properties中添加:【注意:是@@,不是${}】

    app.version = @project.version@
    app.build.time = @timestamp@
    

    注意: timestamp 就是前面pom properties中及配置的 timestamp标签 获取的时间戳!

  3. @Value注入获取配置信息


@Configuration
@Slf4j
public class BeanPostConfig implements BeanPostProcessor {

    /**
     * 当前版本
     */
    @Value("${app.version}")
    private String serviceVersion;
    /**
     * 打包时间
     */
    @Value("${app.build.time}")
    private String serviceBuildDate;


    public void projectInfo(){
        StringBuffer projectInfo = new StringBuffer();
        projectInfo.append("\n=================project=================\n");
        projectInfo.append(String.format("\nservice version:%s\n",serviceVersion));
        projectInfo.append(String.format("\nservice build date:%s\n",serviceBuildDate));
        projectInfo.append("\n=================project=================\n");
        LoggerUtil.info(projectInfo.toString());
    }

}

或controller层返回

    /**
     * 当前版本
     */
    @Value("${app.version}")
    private String serviceVersion;
    /**
     * 打包时间
     */
    @Value("${app.build.time}")
    private String serviceBuildDate;


    @RequestMapping("/index/versionMore")
    public RespData versionMore() {

        Map<String,String> ret = new HashMap<>(16);
        ret.put("version",serviceVersion);
        ret.put("buildTime",serviceBuildDate);

        return RespDataUtil.buildSuccess(ret);
    }
### spring-boot-maven-plugin 支持的版本列表 `spring-boot-maven-plugin` 是 Spring Boot 提供的一个 Maven 插件,主要用于支持 Spring Boot 项目的构建打包操作。该插件的版本通常与对应的 Spring Boot 版本保持一致,因此其支持的版本列表可以依据官方发布的 Spring Boot 版本来推断。 以下是 `spring-boot-maven-plugin` 的一些常见版本及其对应关系: #### 官方支持的主要版本 | **Spring Boot Version** | **Maven Plugin Version** | |-------------------------|--------------------------| | 3.x | 3.x | | 2.7.x | 2.7.x | | 2.6.x | 2.6.x | | 2.5.x | 2.5.x | | 2.4.x | 2.4.x | | 2.3.x | 2.3.x | 这些版本的具体功能支持特性可以通过查看 [Spring Boot Release Notes](https://github.com/spring-projects/spring-boot/releases) 获取更多信息[^1]。 #### 如何确认当前使用的版本? 当在项目中使用 `spring-boot-starter-parent` 作为父 POM 时,`spring-boot-maven-plugin` 的版本会被自动继承并设置为与 Spring Boot 版本相同。例如,在以下配置中: ```xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.9</version> </parent> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> ``` 这里无需显式声明插件版本,因为它会默认采用父 POM 中定义的 Spring Boot 版本号[^2]。 #### 如果遇到版本冲突怎么办? 如果手动指定了插件版本(如 `<version>`),可能会因为不兼容而导致编译失败。例如,某些旧版 JDK 可能无法加载由较新 Java 编译器生成的类文件。这种情况下需要升级 JDK 或调整插件版本以匹配环境需求[^4]。 #### 示例:如何解决版本问题 假设项目运行于 JDK 8 上,而尝试使用了 `spring-boot-maven-plugin:3.0.0`,则可能触发如下错误: ``` Unable to load the mojo 'build-info' in the plugin 'org.springframework.boot:spring-boot-maven-plugin:3.0.0' due to an API incompatibility... ``` 解决方案之一是降级到适合 JDK 8 的插件版本,比如 `2.7.x` 系列。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西京刀客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值