Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven ${project.version} not working

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}
like image 988
James King Avatar asked Mar 15 '17 13:03

James King


1 Answers

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.

like image 197
skadya Avatar answered Nov 09 '22 05:11

skadya