mar*_*cki 11 java versioning spring maven
我有一个休息端点用于返回有关应用程序的信息(到目前为止只有应用程序版本)但到目前为止这个信息是硬编码的,并且很容易忘记更改它.我最好从pom或manifest文件中检索app版本.有没有带来这种功能的项目?
Pet*_*der 10
您最好使用内置清单.
new Manifest(Application.class.getResourceAsStream("/META-INF/manifest.mf"))
Run Code Online (Sandbox Code Playgroud)
对于具体的impl版本:
new Manifest(Application.class.getResourceAsStream("/META-INF/manifest.mf"))
.getMainAttributes()
.get(Attributes.Name.IMPLEMENTATION_VERSION)
Run Code Online (Sandbox Code Playgroud)
使用maven不要忘记使用以下方法创建清单:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
Spring Boot可以参考pom.xml中定义的项目版本,并使用Actuator通过REST公开它:
# application.properties
endpoints.info.enabled=true
info.app.version=@project.version@
Run Code Online (Sandbox Code Playgroud)
然后访问/ info URL(例如http:// localhost:8080/info)将返回:
{"app": {"version": "<major.minor.incremental>"}}
Run Code Online (Sandbox Code Playgroud)
另请参阅:spring boot/spring web app嵌入版本号
有一个名为Appinfo 的惊人项目,请使用它并享受它!(它有一个丑陋的页面,我知道 - 但它有效:)
AppInfo 允许自动为您的应用程序提供当前版本号、构建日期或构建号。
同样出色的Spring Boot Actuator提供名为Info Endpoint 的功能,可以将版本信息发布到 Web 或 REST。
默认情况下,执行器会向主服务器添加一个 /info 端点。它包含来自 git.properties(如果该文件存在)的提交和时间戳信息,以及它在环境中找到的前缀为“info”的任何属性。