How to get the version of the Microservice programmatically

Hi,

is there a way to programmatically retrieve of the version of a Java Microservice? It is specified in the “version” field in the cumulocity.json but how can I “read” that in the same microservice’s Java code? The microservice is using the MS SDK 1020.155.0.
Do we need to use the ApplicationApi or are there better ways (using some Spring magic) to inject its value?

Thanks,
Michael

Certainly using the application API will return the application manifest that contains version…

You can use currentApplication to get the application id.

For accessing the current application (as returned by the Application API of Cumulocity) from within the Java code of a Microservice, see the code snippet at Get Application ID or Managed Object ID of Cumulocity Microservice | Answer 4 . Reading the application version based on this is straightforward: getCurrentApplication().getManifest().getProperty("version").

Here are two solutions without using the Application API or any Cumulocity API. The solutions use Maven (and Spring in the second case) for making the application version accessible from within the application’s code:

  1. Using the properties-maven-plugin for generating a properties file, which can be accessed programmatically in the Java code: https://www.baeldung.com/java-accessing-maven-properties
  2. Using the build-info goal of the spring-boot-maven-plugin. This generates the file build-info.properties, which can be accessed with a Bean of type BuildProperties: https://www.baeldung.com/spring-boot-build-properties#using-buildproperties

While the first solution is more generic, the latter one is more tailored to Spring.

Thanks a lot for the quick answers!

Unfortunately I couldnt get one of the two suggestions using Maven/Spring to work …

But the idea using the ApplicationApi and to read the version from the manifest works well - and it even does not require an additional permission for the MS so that is very easy to use.

Thanks!
Michael

If you have created your microservices using the archetype, you can directly access the “microservice.version” environment variable. This variable is written to the property file during the build process. Therefore, you do not need to access Cumulocity to retrieve this information; the microservice should be self-aware of its version. The reason why i did this was also the start banner which contains the microservice version / Cumulcoty java sdk version etc.

1 Like

OK, after some more debugging and trail & error, the issue with the failing approach using Maven/Spring’s “Automatic Property Expansion” directly was due to the project being a multi-module project. The <filtering>true</filtering> needs to be applied in the correct sub-module, of course.
So, thanks a lot @Christian_Winter1 & @Alexander_Pester !

1 Like