我正在尝试读取 pom.xml 的值并将其注入(inject) application.properties 中。但我无法实现这一点。 在我的 pom.xml 中,我的属性值如下所示:
<properties>
<app.mobile.db.host>
${env.APP_MOBILE_DB_HOST}
</app.mobile.db.host>
...
</properties>
并且,APP_MOBILE_DB_HOST 来自.m2 文件夹的settings.xml。 我还在 pom.xml 中添加了以下几行。
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
....
在 application.properties 中我正在访问
app.host= @app.mobile.db.host@
在 Spring boot 中,我使用 @ConfigurationProperties 从 application.properties 读取值。 但是当我打印它时,它只打印@app.mobile.db.host@。 我哪里错了?请帮忙。
最佳答案
@
仅在您使用 spring-boot-starter-parent
时才有效。另外,如果您希望该值来自 settings.xml
,则不应在 pom.xml
中重新定义它。最后,env.x
语法用于环境变量;从 pom 中删除该行并将 application.properties
更改为 app.host = @APP_MOBILE_DB_HOST@
应该可以解决问题。如果您绝对必须为 APP_MOBILE_DB_HOST
创建别名,请在 pom.xml
中像 app.host=${APP_MOBILE_DB_HOST}
一样定义它,尽管我不确定 Maven 是否多次传递属性解析。
通过运行 mvn resources:resources -DAPP_MOBILE_DB_HOST=test
并检查 target/classes/application.properties
来验证您的更改。您不需要运行该应用程序。
关于java - 在 application.properties 中使用 pom.xml 的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45624531/