I would like to extract the version code from maven within my java app. I created a application.properties file with a property which calls ${project.version} and added the following to the pom file
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
, very similar to this question - Maven ${project.version} doesn't show up in java
However the difference here is I am using spring boot. When I run the application the output is "${project.version}" and in the target/application.properties file, I can see that the property has not been updated with the version number.
Any ideas?
UPDATE - files and code I am using
application.properties file
application.version=${project.version}
getting the property
public String getVersionTest() {
String resourceName = "application.properties"; // could also be a constant
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Properties props = new Properties();
try(InputStream resourceStream = loader.getResourceAsStream(resourceName)) {
props.load(resourceStream);
} catch (IOException e) {
e.printStackTrace();
}
return props.getProperty("application.version");
}
System.out.println("version number = " + kiosk.getVersionTest());
output = ${project.version}
From spring boot documentation
since the default config files accept Spring style placeholders (${…}) the Maven filtering is changed to use @..@ placeholders (you can override that with a Maven property resource.delimiter).
You have two options to achieve the desired behaviour : Either use the @..@ syntax to specify place holder. E.g.
[email protected]@
or revert back the resource filter delimiter by using property ${resource.delimiter}
as mentioned in ryanp's answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With