[英]Using property values of pom.xml in application.properties
I am trying to read values of pom.xml and inject it into application.properties. 我正在尝试读取pom.xml的值并将其注入application.properties。 But I am not able to achieve this. 但我无法实现这一目标。 In my pom.xml, I have the property value like below: 在我的pom.xml中,我具有如下所示的属性值:
<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. 并且,APP_MOBILE_DB_HOST来自.m2文件夹的settings.xml。 I added the following lines in pom.xml also. 我也在pom.xml中添加了以下几行。
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
....
In application.properties I am accessing like 在application.properties我正在访问像
app.host= @app.mobile.db.host@
And in Spring boot I am using @ConfigurationProperties to read the value from application.properties. 在Spring Boot中,我使用@ConfigurationProperties从application.properties中读取值。 But when I print it, it just prints @app.mobile.db.host@. 但是当我打印它时,它只打印@ app.mobile.db.host @。 Where I am going wrong? 我要去哪里错了? Kindly help. 请帮助。
@
only works if you're using spring-boot-starter-parent
. @
仅在使用spring-boot-starter-parent
。 Also, if you want the value to come from settings.xml
, you shouldn't redefine it in your pom.xml
. 另外,如果您希望该值来自settings.xml
,则不应在pom.xml
重新定义它。 Lastly, env.x
syntax is for environment variables; 最后, env.x
语法用于环境变量; removing that line from your pom and changing application.properties
to app.host = @APP_MOBILE_DB_HOST@
should solve the issue. 从pom中删除该行并将application.properties
更改为app.host = @APP_MOBILE_DB_HOST@
应该可以解决此问题。 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. 如果您绝对必须为APP_MOBILE_DB_HOST
创建别名, APP_MOBILE_DB_HOST
像在pom.xml
app.host=${APP_MOBILE_DB_HOST}
其定义为app.host=${APP_MOBILE_DB_HOST}
,尽管我不确定Maven是否进行多次app.host=${APP_MOBILE_DB_HOST}
以进行属性解析。
Verify your changes by running mvn resources:resources -DAPP_MOBILE_DB_HOST=test
and inspecting the target/classes/application.properties
. 通过运行mvn resources:resources -DAPP_MOBILE_DB_HOST=test
并检查target/classes/application.properties
验证更改。 You don't need to run the application. 您不需要运行该应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.