Java 从 pom 获取应用程序版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21045007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:10:09  来源:igfitidea点击:

Getting application version from pom

javaspringmavenversioning

提问by marek.kapowicki

I have a rest endpoint used to return information about application (so far only app version) But so far this info is hardcoded, and it's pretty easy to forget to change it. I will be better to retrieve app version from pom or manifest file. Is there any project that brings such functionality?

我有一个用于返回有关应用程序信息的休息端点(到目前为止只有应用程序版本)但到目前为止,此信息是硬编码的,很容易忘记更改它。我会更好地从 pom 或清单文件中检索应用程序版本。有没有带来这种功能的项目?

采纳答案by MariuszS

There is amazing project named Appinfo, please use it and enjoy! (It has an ugly page, I know - but it works :)

有一个名为Appinfo 的惊人项目,请使用它并享受它!(它有一个丑陋的页面,我知道 - 但它有效:)

AppInfo allows to automatically feed your application with a current version number, build date or build number.

AppInfo 允许自动为您的应用程序提供当前版本号、构建日期或构建号。



Also excellent Spring Boot Actuatorprovides feature named Info Endpointwhich can publish version information to web or REST.

同样出色的Spring Boot Actuator提供名为Info Endpoint 的功能,可以将版本信息发布到 Web 或 REST。

By default the Actuator adds an /info endpoint to the main server. It contains the commit and timestamp information from git.properties (if that file exists) and also any properties it finds in the environment with prefix "info".

默认情况下,执行器会向主服务器添加一个 /info 端点。它包含来自 git.properties(如果该文件存在)的提交和时间戳信息以及它在环境中找到的前缀为“info”的任何属性。

回答by MrD

You could use the resource filteringof maven or something like the maven-substitute-plugin.

您可以使用maven的资源过滤或类似maven-substitute-plugin.

回答by Grim

You better use build-in manifest.

您最好使用内置清单。

new Manifest(Application.class.getResourceAsStream("/META-INF/manifest.mf"))

For the concrete impl-version:

对于具体的 impl 版本:

new Manifest(Application.class.getResourceAsStream("/META-INF/manifest.mf"))
            .getMainAttributes()
            .get(Attributes.Name.IMPLEMENTATION_VERSION)

Using maven do not forget to create the manifest using:

使用 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>

回答by AlexO

Spring Boot can refer to the project version defined in pom.xml and expose it via REST using Actuator:

Spring Boot 可以引用 pom.xml 中定义的项目版本,并使用Actuator通过 REST 公开它:

# application.properties
endpoints.info.enabled=true
[email protected]@

Then accessing the /info URL (e.g. http://localhost:8080/info) will return:

然后访问 /info URL(例如http://localhost:8080/info)将返回:

{"app": {"version": "<major.minor.incremental>"}}

See also: spring boot/spring web app embedded version number

另见:spring boot/spring web app内嵌版本号