15

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?

Charles
  • 50,943
  • 13
  • 104
  • 142
marek.kapowicki
  • 674
  • 2
  • 5
  • 17
  • Do you mean to say version of the build ? If yes then simply add 1.0.0 in the POM – Kick Jan 10 '14 at 13:05
  • 2
    I think he means how does he then read that version at runtime... http://stackoverflow.com/questions/2712970/how-to-get-maven-artifact-version-at-runtime – Ben Thurley Jan 10 '14 at 13:08
  • Yes I would like to read current version (value of tag) from pom and use it in java code. And I want to do it in simple and elegant way – marek.kapowicki Jan 10 '14 at 13:19
  • 1
    Just take a look [here](http://blog.soebes.de/blog/2014/01/02/version-information-into-your-appas-with-maven/). – khmarbaise Jan 10 '14 at 13:56

4 Answers4

15

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

AlexO
  • 1,311
  • 12
  • 18
13

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>
Grim
  • 1,938
  • 10
  • 56
  • 123
8

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".

MariuszS
  • 30,646
  • 12
  • 114
  • 155
2

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

MrD
  • 1,255
  • 1
  • 10
  • 24
  • Yes, but howto integrate filtering with REST? :) – MariuszS Jan 10 '14 at 13:36
  • When building the project with Maven, the requested version-information will go into some file (again it is somehow "hardcoded", but set by the resource-preprocessor of maven before building the project) - like a Properties-file or directly in Java-class, ... The application can use this information then to return it on REST-request. Am I missing something? – MrD Jan 10 '14 at 13:44
  • This is true, but there are tools for doing this even easier. – MariuszS Jan 10 '14 at 13:45
  • 1
    Ok, it doesn't sound too difficult to do this "manually". But it sounds interesting. Thanks for the hint. – MrD Jan 10 '14 at 13:46