本文内容旨在获取后端应用打包编译的信息,更多的信息可以自行定义;
文中示例可以获取打包用户电脑的用户名、打包时间、应用名、应用版本号。
1. maven 依赖 pom 配置
如果是多模块,在项目主配置中设置即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| <?xml version="1.0" encoding="UTF-8" ?> <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"> <properties> <resource.delimiter>@</resource.delimiter> <maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format> <app.package.timestamp>${maven.build.timestamp}</app.package.timestamp> <app.package.user>${user.name}</app.package.user> <app.package.name>${project.name}</app.package.name> <app.package.version>${project.version}</app.package.version> </properties>
<bean> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </bean> </project>
|
这里定义了四个变量(时间、用户名、应用名、版本号),
设置变量替换的分隔符为 @(与 spring 默认的 ${} 加以区分),
并在 resource 中启用变量替换功能。
2. 应用配置文件设置参数
这里建议直接在 application.properties 或 application.yml 文件中设置,
以避免因为不同环境的配置而无法生效,同时这个可以不必在生产环境的配置中出现。
如果是 properties 格式,配置参数如
如果是 yml 格式,配置参数如
1 2 3 4 5 6
| app: build: time: @app.package.timestamp@ user: @app.package.user@ name: @app.package.name@ version: @app.package.version@
|
这样我们已经定义了四个变量对应的参数,可以通过代码来获取当前运行环境的编译信息了。
3. 代码获取编译信息
在 SpringBoot 中如果没有特殊需求,一般是直接使用 @Value 注入即可获取。
若有其他需求可以通过 configuration 来注入某个类以获取,此处我们以 @Value 作示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| import com.alibaba.fastjson2.JSONObject; import com.tmsio.restful.bean.RespMsg; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter;
@Slf4j @RestController @CrossOrigin(origins = "*", maxAge = 3600) @RequestMapping("/apiCenter/commonApi") public class BizCommonApiController { @Value("${app.build.time:2024-01-01 10:10:10}") private String buildTime; @Value("${app.build.user:rancho}") private String buildUser; @Value("${app.build.name:appName}") private String buildName; @Value("${app.build.version:V1.0}") private String buildVersion;
@GetMapping("/getSvrBuildInfo") public RespMsg<JSONObject> getSvrBuildInfo() { JSONObject buildInfo = new JSONObject(); buildInfo.put("buildName", buildName); buildInfo.put("buildVersion", buildVersion); buildInfo.put("buildUser", buildUser); buildInfo.put("buildTime", LocalDateTime.parse(buildTime).plusHours(8) .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); return RespMsg.success(buildInfo); } }
|
此时当我们通过 maven 进行打包操作,配置文件中定义的变量久会实时根据项目来修改,然后只需要在应用启动之后访问定义的接口即可获取编译信息。
注意这里默认的 maven 打包生成时间是 UTC(格林威治时间)格式的,所以我们手动给他加 8 个小时,这样即使此处接口数据用以呈现给用户端查看也没问题。
如此处访问 GET http://127.0.0.1:8080/apiCenter/commonApi/getSvrBuildInfo 可以看到访问结果为
1 2 3 4 5 6 7 8 9 10 11 12
| { "code": 200, "message": "working~", "data": { "buildVersion": "2024.01.10001", "buildName": "appServer", "buildTime": "2024-01-19 22:20:41", "buildUser": "Rancho" }, "timestamp": 1705674214104, "success": true }
|
实际使用中我们还可以扩展性地增加一些运行时等信息,具体可以自行发挥~
另外在多模块服务中可以放在通用模块,这样就不需要每个业务包都做这个 controller 的操作
最后,考虑到安全性,请谨慎决定是否将用户名等信息暴露在应用配置中。