java - 使用 Gradle 的弹簧配置文件
问题描述
我正在运行一个包含三个不同属性文件(application.properties、application-prod.properties、application-uat.properties)的 spring boot 项目。我使用的是 Maven,如果我没有指定任何参数,它将选择默认值属性文件,即 application.properties。这是相关文件的外观。
应用程序属性
#Set active profile
spring.profiles.active=@activatedProperties@
#MongoDB configuration
spring.data.mongodb.host=${MONGODB_HOST:localhost}
spring.data.mongodb.port=${MONGODB_PORT:27017}
spring.data.mongodb.database=database
spring.data.mongodb.username=username
spring.data.mongodb.password=password
pom.xml
<profiles>
<profile>
<id>UAT</id>
<properties>
<activatedProperties>uat</activatedProperties>
</properties>
</profile>
<profile>
<id>PROD</id>
<properties>
<activatedProperties>prod</activatedProperties>
</properties>
</profile>
</profiles>
如何使用 Gradle 实现相同的目标?如果我没有指定任何争论,我如何让 Gradle 选择默认值(application.properties)?
解决方案
我认为此链接对您有用...
https://docs.gradle.org/current/userguide/migrating_from_maven.html#migmvn:profiles_and_properties
样本:
if (!hasProperty('buildProfile')) ext.buildProfile = 'default'
apply from: "profile-${buildProfile}.gradle"
task greeting {
doLast {
println message
}
}
#profile-default.gradle
ext.message = 'foobar'
#profile-test.gradle
ext.message = 'testing 1 2 3'
#profile-prod.gradle
ext.message = 'Hello, world!'
此链接也很有用:
推荐阅读
- javascript - 如何从外部访问 Vue 3 方法
- azure-ad-b2c - 用户登录后如何在 Blazor 服务器端应用程序中检索用户声明?
- angular - 如何在 Angular 中访问 SQLite executesql 的返回值
- python - 来自 Python 中的测试目录的 ImportError
- python - 如果我的登录用户名和密码在我的登录窗口中正确,如何在 Tkinter 中打开一个新窗口?
- sql - 如何对 sql db2-400 中的列值进行排序?
- javascript - 如果它在回调中,Chai 期望不会失败测试
- authentication - 我已将我的 DNN9 站点设置为需要电子邮件地址作为登录名,但现在我无法再以超级用户身份登录 - 超级用户登录名是主机
- paypal - “您的付款已被拒绝。请重试。” Magento 2.3.2
- webview - 如何让 flutter_inappwebview 在暂停时加载带有视频的 youtube 页面?