Getting application version from pom

后端 未结 4 1886
天涯浪人
天涯浪人 2020-12-29 11:30

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

相关标签:
4条回答
  • 2020-12-29 11:46

    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

    0 讨论(0)
  • 2020-12-29 11:48

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

    0 讨论(0)
  • 2020-12-29 11:58

    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>
    
    0 讨论(0)
  • 2020-12-29 11:59

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

    0 讨论(0)
提交回复
热议问题