Persimmonium
Persimmonium

Reputation: 15791

Spring boot: is there a way to show the POM version number in some endpoint easily?

I keep my version info in my POM:

<version>2.0.0</version>

I want this number exposed into:

  1. one of the standard endpoints (ideally /info )
  2. a custom one

Is there an easy (automatic) way to do this, or can be done programatically?

Upvotes: 3

Views: 3035

Answers (2)

Wendel
Wendel

Reputation: 2977

Maven pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>wendelsilverio</groupId>
  <artifactId>hello-maven</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Hello</name>
</project>

You can be done programatically internal into created jar:

public String getVersion() {
    String version = null;

    // try to load from maven properties first
    try {
        Properties p = new Properties();
        InputStream is = PomVersionMain.class
                .getResourceAsStream("/META-INF/maven/wendelsilverio/hello-maven/pom.properties");
        if (is != null) {
            p.load(is);
            version = p.getProperty("version", "");
        }
    } catch (Exception e) {
        // ignore
    }

    // fallback to using Java API
    if (version == null) {
        Package aPackage = PomVersionMain.class.getPackage();
        if (aPackage != null) {
            version = aPackage.getImplementationVersion();
            if (version == null) {
                version = aPackage.getSpecificationVersion();
            }
        }
    }

    if (version == null) {
        // we could not compute the version so use a blank
        version = "Version could not compute";
    }

    return version;
}

Upvotes: 1

Darren Forsythe
Darren Forsythe

Reputation: 11411

The spring-boot-maven-plugin allows generation of the POM co-ordinates and additional properties you may want the Actuator to provide.

https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/build-info.html

Maven goal info,

https://docs.spring.io/spring-boot/docs/current/maven-plugin/build-info-mojo.html

spring-boot:build-info

Upvotes: 3

Related Questions