SpringBoot获取pom文件中的版本号,已验证

一、pom文件中配置

在pom文件中配置plguin和resource.

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.test</groupId>
    <artifactId>csp-station-tcp</artifactId>
    <!--注意:该version即为代码要获取的版本号-->
    <version>1.1.1.1000</version>
    <name>tcp-service</name>
    <description>设备接入服务</description>
    
    <!--注意:build中的plugin和resource必须要配置-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <delimiters>
                        <delimiter>@</delimiter>
                    </delimiters>
                    <useDefaultDelimiters>false</useDefaultDelimiters>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
       </resources>
    </build>
</project>

二、yml文件配置

yml文件中采用@@获取版本号

#yml文件中配置
project:
  version: @project.version@

三、代码获取

示例代码验证

@RefreshScope
@RestController
@RequestMapping(value="/config")
public class NacosConfig {
    @Value(value = "${project.version}")
    private String version;

    @GetMapping(value = "/get")
    public String getConfig(){
        return "version: "+ version;
    }

}

猜你喜欢

转载自blog.csdn.net/mawei7510/article/details/126658636