ninja.bhaSiv
ninja.bhaSiv

Reputation: 95

Using property values of pom.xml in application.properties

I am trying to read values of pom.xml and inject it into application.properties. But I am not able to achieve this. In my pom.xml, I have the property value like below:

<properties>
     <app.mobile.db.host>
        ${env.APP_MOBILE_DB_HOST}
    </app.mobile.db.host>
...
</properties>

And, APP_MOBILE_DB_HOST is coming from settings.xml of .m2 folder. I added the following lines in pom.xml also.

<build>
    <resources>
      <resource>
        <filtering>true</filtering>
        <directory>src/main/resources</directory>
      </resource>
    </resources>
....

In application.properties I am accessing like

app.host= @app.mobile.db.host@

And in Spring boot I am using @ConfigurationProperties to read the value from application.properties. But when I print it, it just prints @app.mobile.db.host@. Where I am going wrong? Kindly help.

Upvotes: 2

Views: 4237

Answers (1)

Abhijit Sarkar
Abhijit Sarkar

Reputation: 24518

@ only works if you're using spring-boot-starter-parent. Also, if you want the value to come from settings.xml, you shouldn't redefine it in your pom.xml. Lastly, env.x syntax is for environment variables; removing that line from your pom and changing application.properties to app.host = @APP_MOBILE_DB_HOST@ should solve the issue. If you absolutely must create an alias for APP_MOBILE_DB_HOST, define it like app.host=${APP_MOBILE_DB_HOST} in the pom.xml, although I'm not sure if Maven does multiple passes for property resolution.

Verify your changes by running mvn resources:resources -DAPP_MOBILE_DB_HOST=test and inspecting the target/classes/application.properties. You don't need to run the application.

Upvotes: 2

Related Questions