Reputation: 15791
I keep my version info in my POM:
<version>2.0.0</version>
I want this number exposed into:
Is there an easy (automatic) way to do this, or can be done programatically?
Upvotes: 3
Views: 3035
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
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