SpringBoot 读取Maven ProjectVersion

src/main/resources/application.properties中添加
version=@project.version@

注意红色部分为@project.version@,而非${project.version}占位符,主要是为了避免与Spring语法冲突

配置好之后,可以通过@Value注解的方式直接获取

@Value("${version}")
private String version;

也可以Properties代码加载

final Properties properties = new Properties();    
properties.load(this.getClass().getClassLoader().getResourceAsStream("application.properties")); return properties.getProperty("version");

推荐@Value注解方式
https://stackoverflow.com/questions/26551439/getting-maven-project-version-and-artifact-id-from-pom-while-running-in-eclipse/26573884#26573884
https://stackoverflow.com/questions/37490162/get-maven-properties-at-build-time#37490162

原文地址:https://www.cnblogs.com/a-du/p/12166358.html