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

pom文件


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.3.2.RELEASEversion>
        <relativePath/>
    parent>

    <artifactId>demo-versionartifactId>
    <version>1.0.0version>
    <description>获取pom的版本description>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>


project>

修改application.yml配置

编译构建后,application.yml真实内容(注意version变成了真实值):

project:
  version: @project.version@

【注意:是@@,不是${}

测试

@RestController
public class TestController {
    @Value("${project.version}")
    public String version;

    @GetMapping("version")
    public String getVersion() {
        return version;
    }
}

通过 @Value 注入获取配置信息

浏览输入: http://localhost:8080/version
spring boot-获得Maven-pom中版本号_第1张图片
成功获取到版本号

你可能感兴趣的:(java,spring,boot,maven,java)