SpringBoot读取pom.xml中的version变量

一、背景需求

基于Maven的SpringBoot微服务发布后,在后端管理页面,管理员希望看到各服务模块的当前版本号,因此在需要在Java代码中读取到pom.xml中的version版本信息,通过API接口向管理页面提供版本数据。

二、实现方案

1、maven构建时,将pom.xml中的变量,写入到SpringBoot的yml配置文件中。

bootstrap.yml中,添加从pom.xml文件中读取变量的配置:

spring:
  application:
    # @变量名@ 读取pom.xml中的值
    version: @project.version@

pom.xml中添加resource资源文件过滤,在编译构建的时候,将yml中引用pom.xml的变量,替换成真实的值。

<project>
    <!-- pom中定义的project.version -->
    <version>1.8.0-SNAPSHOT</version>

    <build>        
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <!-- 配置资源文件 -->
                    <include>**/*.yml</include>
                </includes>
                <!-- 启用过滤器,过滤器会解析需要过滤的的资源文件,将其中的变量替换成真实的值 -->
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>

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

spring:
  application:
    # @变量名@ 读取pom.xml中的值
    version: 1.8.0-SNAPSHOT

2、在Java代码中注入SpringBoot变量。

@Component
public class PomConfigTest{
    private static final Logger log = LoggerFactory.getLogger(PomConfigTest.class);
    
    @Value("${spring.application.version}")
    private String applicationVersion;

    public PomConfigTest() {
        log.info("当前版本 applicationVersion: {}", applicationVersion);
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要使用Java读取tar.gz压缩包的内容,您可以使用Apache Commons Compress库。以下是一个示例代码,演示如何读取tar.gz文件的内容: ```java import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; import java.io.*; public class TarGzReader { public static void main(String[] args) { String filePath = "path/to/your/file.tar.gz"; try { FileInputStream fis = new FileInputStream(filePath); GzipCompressorInputStream gzis = new GzipCompressorInputStream(fis); TarArchiveInputStream taris = new TarArchiveInputStream(gzis); TarArchiveEntry entry; while ((entry = taris.getNextTarEntry()) != null) { if (entry.isFile()) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = taris.read(buffer)) != -1) { baos.write(buffer, 0, bytesRead); } String fileName = entry.getName(); byte[] fileContent = baos.toByteArray(); // 在这里处理文件内容 System.out.println("File Name: " + fileName); System.out.println("File Content: " + new String(fileContent)); } } taris.close(); gzis.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 请确保您已经将Apache Commons Compress库添加到您的Java项目。您可以在Maven项目将以下依赖项添加到您的`pom.xml`文件: ```xml <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version>1.21</version> </dependency> ``` 在上面的示例,替换`filePath`变量为您的tar.gz文件的实际路径。代码将打开压缩文件,并逐个读取的文件条目,然后可以对每个文件的内容进行处理。这里只是简单地打印了文件名和文件内容,您可以根据自己的需求进行进一步处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值