Getting application version from pom

房东的猫 提交于 2019-11-30 06:56:08

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

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


Also excellent Spring Boot Actuator provides feature named Info Endpoint which can publish version information to web or 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".

You better use build-in manifest.

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

For the concrete impl-version:

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:

<?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>

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

# application.properties
endpoints.info.enabled=true
info.app.version=@project.version@

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

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!